# Occursin strange behavior

**URL:** <https://discourse.julialang.org/t/occursin-strange-behavior/33127>\
**Category:** General Usage\
**Created:** [January 8, 2020, 11:04pm UTC](https://discourse.julialang.org/t/occursin-strange-behavior/33127 "2020-01-08T23:04:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [January 8, 2020, 11:04pm UTC](https://discourse.julialang.org/t/occursin-strange-behavior/33127/1 "2020-01-08T23:04:22Z")

</div>

```julia
function f(arg)
# Wrap a string with spaces between quotes
	if (isa(arg, AbstractString) || isa(arg, Symbol))
		out = string(arg)
		if (occursin(" ", out))
			out = "\"" * out * "\""
		end
	else
		error("Bla")
	end 
end

```

It works well when the input has blanks

```julia
julia> f("aaa bbb")
"\"aaa bbb\""

```

but returns `nothing` when it doesn’t

```julia
julia> f("aaabbb")

julia>

```

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [January 8, 2020, 11:15pm UTC](https://discourse.julialang.org/t/occursin-strange-behavior/33127/2 "2020-01-08T23:15:27Z")

</div>

What are you `occursin` expecting to do?  
It returns true/false depending on weather the the first argument is contained in the second.  
In your second call that condition is not met and  
the `if` block is not executed.  
There is no return statement.  
if `occursin == true` then the implicit return value is `out` (it is the last line executed except for `end` keywords)  
if `occursin == false` then there is no assignment as the last line. It is the `if` statement. Then `nothing` is returned instead.

I suggest adding a `return out` at the end.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [January 8, 2020, 11:24pm UTC](https://discourse.julialang.org/t/occursin-strange-behavior/33127/3 "2020-01-08T23:24:07Z")

</div>

> [@JonasIsensee](#):
>
> if `occursin == false` then there is no assignment as the last line. It is the `if` statement. Then `nothing` is returned instead.
> 
> I suggest adding a `return out` at the end.

You are right, but I was expecting that when `occursin == false` the previous assignment (`out = string(arg`) was the last one and that value should be the return value
