# Writing a function that requires 1 argument, when using it julia thinks I put 2 in

**URL:** https://discourse.julialang.org/t/writing-a-function-that-requires-1-argument-when-using-it-julia-thinks-i-put-2-in/57213
**Category:** New to Julia
**Created:** [March 15, 2021, 4:40pm UTC](https://discourse.julialang.org/t/writing-a-function-that-requires-1-argument-when-using-it-julia-thinks-i-put-2-in/57213 "2021-03-15T16:40:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sonicrules1234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sonicrules1234/32/22938_2.png) [@sonicrules1234](https://discourse.julialang.org/u/sonicrules1234)
#### Post date: [March 15, 2021, 4:40pm UTC](https://discourse.julialang.org/t/writing-a-function-that-requires-1-argument-when-using-it-julia-thinks-i-put-2-in/57213/1 "2021-03-15T16:40:06Z")

</div>

I have this in greet.jl

```julia
module greetings
export greet
function greet(greeting::String)
    if greeting == "hello" | greeting == "Hello"
        returnmsg = "Good morning"
    elseif greeting == "goodbye"
        returnmsg = "Good night."
    else 
        returnmsg = "What's up"
    end
    return returnmsg
end
end

```

I go into the repl and get an error, which looks like julia thinks I am giving it two arguments.

```julia
julia> include("greet.jl")
Main.greetings

julia> greetings.greet("hello")
ERROR: MethodError: no method matching |(::String, ::String)
Closest candidates are:
  |(::Any, ::Any, ::Any, ::Any...) at operators.jl:538
Stacktrace:
 [1] greet(::String) at /home/westly/greet.jl:4
 [2] top-level scope at REPL[2]:1
 [3] run_repl(::REPL.AbstractREPL, ::Any) at /build/julia/src/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288

```

What am I doing wrong here?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 15, 2021, 4:42pm UTC](https://discourse.julialang.org/t/writing-a-function-that-requires-1-argument-when-using-it-julia-thinks-i-put-2-in/57213/2 "2021-03-15T16:42:08Z")

</div>

The “or” operator you are probably expecting is `||` (not a single `|`).

That is another type of “or” operator, which one rarely uses.

---

<div class="post-metadata">

### Author: ![sonicrules1234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sonicrules1234/32/22938_2.png) [@sonicrules1234](https://discourse.julialang.org/u/sonicrules1234)
#### Post date: [March 15, 2021, 4:43pm UTC](https://discourse.julialang.org/t/writing-a-function-that-requires-1-argument-when-using-it-julia-thinks-i-put-2-in/57213/3 "2021-03-15T16:43:21Z")

</div>

Thanks, that worked.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [March 15, 2021, 4:43pm UTC](https://discourse.julialang.org/t/writing-a-function-that-requires-1-argument-when-using-it-julia-thinks-i-put-2-in/57213/4 "2021-03-15T16:43:30Z")

</div>

Take a look at [Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity)

The `|` operator has higher precedence than `==`, so you’re doing `"hello" | greeting` - this is fixed by adding parentheses to the expression:  
`(greeting == "hello") | (greeting == "Hello")`

---

<div class="post-metadata">

### Author: ![sonicrules1234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sonicrules1234/32/22938_2.png) [@sonicrules1234](https://discourse.julialang.org/u/sonicrules1234)
#### Post date: [March 15, 2021, 4:44pm UTC](https://discourse.julialang.org/t/writing-a-function-that-requires-1-argument-when-using-it-julia-thinks-i-put-2-in/57213/5 "2021-03-15T16:44:04Z")

</div>

Thanks for the explanation.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 15, 2021, 4:46pm UTC](https://discourse.julialang.org/t/writing-a-function-that-requires-1-argument-when-using-it-julia-thinks-i-put-2-in/57213/6 "2021-03-15T16:46:13Z")

</div>

> [@sonicrules1234](#):
>
> `greeting == "hello" | greeting == "Hello"`

This is parsed as `greeting == ("hello" | greeting) == "Hello"`, since `|` has [higher precedence](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity) than `==`. You would have to write `(greeting == "hello") | (greeting == "Hello")` to use `|`.

As @lmiq commented, what you want is `greeting == "hello" || greeting == "Hello"`: `||` is “logical or”, which is usually what is employed in boolean expressions (and has lower precedence than `==`). `|` is a “bitwise or” operation, and is usually for bit arithmetic.

(They also differ in that `||` is [short-circuiting](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation), which is typically what you want in logical operations, and `|` is not.)
