# Find out number of function arguments or if passed at all

**URL:** <https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376>\
**Category:** New to Julia\
**Tags:** functions\
**Created:** [December 20, 2021, 3:25pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376 "2021-12-20T15:25:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![itchy00](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/itchy00/32/30442_2.png) [@itchy00](https://discourse.julialang.org/u/itchy00)\
**Post date:** [December 20, 2021, 3:25pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376/1 "2021-12-20T15:25:26Z")

</div>

Hi

(This could be relevant for all kinds of functions in general, but I’ll give a specific example here).  
so I’m quite new to julia and currently trying to write a custom package/module with some data loading and processing functionalities for a very specific type of files.  
I know a bit about positional and keyword arguments and defaults.

I want to write a function that does some processing on files in a folder specified (or not!!!) by the user.  
For functions that work on files I like to do sth like:

```julia
function onFile(filename::String="")
    if filename == ""
        filename = "chosen_file.dat" # Show the user a file dialog to select a file
    end
    println("Do something with file ", filename)
end

```

to give the user the option to provide a filename directly or open a dialog when the argument is not given. Giving an empty string as filename is certainly invalid and behaviour is as expected.

No assume a function is supposed to do something on a folder (process all files in folder or sth like that). In this case

```julia
function onFolder(foldername::String="")
    if foldername == ""
        foldername = "chosen_folder" # Show the user a file dialog to select a folder
        # But WAIT! Giving "" is perfectly fine for selecting the current folder!
    end
    println("Do something on folder ", foldername)
end

```

So that doesn’t work because “” is a valid option. And even if the function is called with no argument whatsoever, it should still process the current folder (like, e.g., `readdir()` does). Of course I could define something invalid here like “?” or so.

But what would be a nice way in good programming style to actually check if a positional argument was passed at all (and not define a rediculous default case)? I could omit the `::String` typing and use `nothing` as default but then I must manually check if a string was passed…

Coming from MATLAB there is the _varargin_ concept. Is there something comparable in julia?  
So in short… How do I check if the caller passed any arguments at all?

Thx a lot!!!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 20, 2021, 3:30pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376/2 "2021-12-20T15:30:37Z")

</div>

Just define two methods?

```julia
julia> onFolder() = println("Do something on folder chosen_folder")
onFolder (generic function with 1 method)

julia> onFolder(foldername) = println("Do something on folder $foldername")
onFolder (generic function with 2 methods)

julia> onFolder()
Do something on folder chosen_folder

julia> onFolder("other folder")
Do something on folder other folder

```

---

<div class="post-metadata">

**Author:** ![itchy00](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/itchy00/32/30442_2.png) [@itchy00](https://discourse.julialang.org/u/itchy00)\
**Post date:** [December 20, 2021, 3:34pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376/3 "2021-12-20T15:34:05Z")

</div>

Wow okay!  
Thanks for the super quick reply!! 😀

Maybe that is just the good julia programming style I am looking for!

Still curious anyway if there is a built-in way to check for the number of passed arguments?  
(Although your solution sounds like perfectly fine, of course)

Or is this just good style to write multiple methods and then those methods just automatically “know” how many arguments were given…? 🤔 Yeah I think I like that concept 🙂

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [December 20, 2021, 4:01pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376/4 "2021-12-20T16:01:11Z")

</div>

An alternative solution is to use keyword arguments with `nothing` as a sentinel value\* (although this is not exactly analogous to Matlab’s `nargin`):

```julia
function onFile(;filename=nothing)
    if isnothing(filename)
        filename = "chosen_file.dat"
    end
    println("Do something with file ", filename)
end

```

In my experience, the need for `nargin` in Matlab is generally caused by the lack of multiple dispatch forcing manual creation of different methods within the function body. The combination of multiple dispatch and keyword arguments with sentinel or default values is a much more flexible and convenient solution.

(Also, in the very specific case of an indeterminate number of arguments of the same type, [`Varargs functions`](https://docs.julialang.org/en/v1/manual/functions/#Varargs-Functions) are a thing.)

\*Edit: There’s nothing wrong with using `nothing` as a sentinel value, it’s a very idiomatic pattern for Julia code, especially for keyword arguments.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 20, 2021, 4:12pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376/5 "2021-12-20T16:12:12Z")

</div>

> [@itchy00](#):
>
> Still curious anyway if there is a built-in way to check for the number of passed arguments?

This [other thread](https://discourse.julialang.org/t/the-input-number-of-one-function/15449/3) might be of interest.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 20, 2021, 5:25pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376/6 "2021-12-20T17:25:42Z")

</div>

Instead of passing the empty string, you can directly define your default argument value:

```julia
function onFile(filename::String=choose_user_file())
    println("Do something with file ", filename)
end

```

---

<div class="post-metadata">

**Author:** ![itchy00](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/itchy00/32/30442_2.png) [@itchy00](https://discourse.julialang.org/u/itchy00)\
**Post date:** [December 20, 2021, 6:26pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376/7 "2021-12-20T18:26:03Z")

</div>

Yes, thanks.  
I’ve actually been using this approach frequently and find it quite nice as well.  
But then I stumbled across the conflict with the strong typing… If I want to force `filename::String` I cannot assign `nothing` as default.  
(Not that it is super important for me to have that typing enforced in the particular case. But that’s what got me thinking about alternate solutions…)

---

<div class="post-metadata">

**Author:** ![itchy00](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/itchy00/32/30442_2.png) [@itchy00](https://discourse.julialang.org/u/itchy00)\
**Post date:** [December 20, 2021, 6:28pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376/8 "2021-12-20T18:28:50Z")

</div>

> Instead of passing the empty string, you can directly define your default argument value:

Cool 🙂

I really like this idea of directly calling a function with proper return type as default value. I think this will be my favorite from now on 👍

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [December 20, 2021, 6:35pm UTC](https://discourse.julialang.org/t/find-out-number-of-function-arguments-or-if-passed-at-all/73376/9 "2021-12-20T18:35:32Z")

</div>

For simplicity, I had omitted the type requirements, but a `Union` is the solution for maintain a strict type parameter:

```julia
function onFile(;filename::Union{Nothing,String}=nothing)
    if isnothing(filename)
        filename = "chosen_file.dat"
    end
    println("Do something with file ", filename)
end

```
