# What is the "NULL" function?

**URL:** <https://discourse.julialang.org/t/what-is-the-null-function/10313>\
**Category:** General Usage\
**Created:** [April 13, 2018, 12:38am UTC](https://discourse.julialang.org/t/what-is-the-null-function/10313 "2018-04-13T00:38:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [April 13, 2018, 12:38am UTC](https://discourse.julialang.org/t/what-is-the-null-function/10313/1 "2018-04-13T00:38:35Z")

</div>

dear julia wizards: What is the equivalent of the NULL function to use as a default assignment when typing a function as an argument? by analogy, consider this first:

```julia
julia> work(x::Float64=NaN)= ( println("entering"); (!isnan(x)) && println("your x is $x") )

julia> work(1.0) ## with argument
entering
your x is 12.2

julia> work() ## ok, my default
entering
false

```

So, I want to do the same, but passing a function and not a Float.

```julia
julia> work(f::Function)= ( println("entering"); println("applying f to 4.0: ", f(4.0)) );

julia> work(sqrt) ## with argument
entering
applying f to 4.0: 2.0

julia> work() ## this is what I want to enable. a default
   --- so, here I want to see `entering\n` and then an error

```

after `f::Function`, I need to put a default argument, something like `=nothing`. but `nothing` is not right, because `work()` no longer even prints entering.

what is the magic NULL, NADA, nothing, etc., that one would typically use here? (I could define my own pointer to a nothing function as an alternative.)

/iaw

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 13, 2018, 1:42am UTC](https://discourse.julialang.org/t/what-is-the-null-function/10313/2 "2018-04-13T01:42:20Z")

</div>

Can’t you just define

```julia
work() = error()

```

in addition to `work(f::Function)`?

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [April 13, 2018, 3:10am UTC](https://discourse.julialang.org/t/what-is-the-null-function/10313/3 "2018-04-13T03:10:06Z")

</div>

yes, in this specific context, I could do this. however, this was just an illustrative example. this is really a more general question.

but maybe “error()” is indeed a good convention function to ab-use as the null function if there is no other.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 13, 2018, 7:11am UTC](https://discourse.julialang.org/t/what-is-the-null-function/10313/4 "2018-04-13T07:11:00Z")

</div>

```julia
function work(f::Union{Function, Void} = nothing)
    println("entering")
    f === nothing && error("did not give a function") 
    println("applying f to 4.0: ", f(4.0))
end

```

```julia
julia> work()
entering
ERROR: did not give a function
Stacktrace:
 [1] work at ./REPL[4]:3 [inlined]
 [2] work() at ./REPL[4]:2

julia> work(identity)
entering
applying f to 4.0: 4.0

```

---

<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:** [April 13, 2018, 1:30pm UTC](https://discourse.julialang.org/t/what-is-the-null-function/10313/5 "2018-04-13T13:30:47Z")

</div>

> [@iwelch](#):
>
> after `f::Function`, I need to put a default argument, something like `=nothing`. but `nothing` is not right, because `work()` no longer even prints entering.

What is the default function that you want? `nothing` is not a function.

- If you want the default function to be the identity, then use `work(f::Function=identity)`.
- If you want the default function be one that _returns_ `nothing`, do `work(f::Function=x->nothing)`.
- If you want to do something completely different when the user doesn’t pass a function, define a separate `work() = ...` function.
- If you want to throw an error, don’t give any default or `work()` definition at all — by default it will throw a `MethodError`.

Passing `NULL` for missing arguments is a C idiom; although you can force this in Julia with a `Union` argument type, it’s probably better to re-think what you are doing. In many cases where you would pass a NULL function and have an explicit `if (function == NULL)` check in C you would use multiple dispatch (i.e. define different methods) in Julia or else define a default function that actually does what you want.
