# Call a function within the same file

**URL:** <https://discourse.julialang.org/t/call-a-function-within-the-same-file/865>\
**Category:** New to Julia\
**Created:** [December 11, 2016, 4:26pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865 "2016-12-11T16:26:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 11, 2016, 4:26pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/1 "2016-12-11T16:26:33Z")

</div>

I’m writing a short function and try to call it in a same file as follows.

```julia
a = 1.0:3.0
tfs(a)

function tfs(x)
 n = length(x)
 m = sum(x)/n
 s = sqrt(sum((x-m).^2/n))
 return m, s
end

```

Of course, I got an error.

```julia
ERROR: LoadError: UndefVarError: tfs not defined
 in include_from_node1(::String) at ./loading.jl:488
while loading /home/chobbes/Documents/git/smsa/test14.jl, in expression starting on line 2

```

I know there must be something naively wrong. Can anyone give me a hint? Thanks!!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 11, 2016, 4:28pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/2 "2016-12-11T16:28:22Z")

</div>

Put the function above the call?

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 11, 2016, 4:34pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/3 "2016-12-11T16:34:33Z")

</div>

OMG…That works!! I know this must be the dumbest question even in ‘First steps’…

Btw, if I define a function in a file and want to call it in terminal, what am I supposed to do to make sure that the terminal can ‘see’ that function? Thanks!!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 11, 2016, 4:44pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/4 "2016-12-11T16:44:20Z")

</div>

`include` the file…

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 11, 2016, 4:48pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/5 "2016-12-11T16:48:15Z")

</div>

Thanks!!

A quick question - why do I see the results every time I run the code? Seems that I’m not printing them…

```julia
julia> include("/home/chobbes/Documents/git/smsa/test14.jl")
(2.0,0.816496580927726)

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 11, 2016, 4:52pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/6 "2016-12-11T16:52:13Z")

</div>

The return value of the evaluation is printed (That’s what the `P` in REPL for). In this case, the result of the last expression in the included file.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 11, 2016, 5:01pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/7 "2016-12-11T17:01:22Z")

</div>

Thanks very much. I see it now.

I’m sorry, but still have another question:

If I only need the function to return `m`, but not `s`, what shall I do? Is there anything like `~` in Matlab? For example, [m, ~] = tfs(x) in Matlab. Thanks!!

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [December 11, 2016, 5:38pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/8 "2016-12-11T17:38:33Z")

</div>

You can either just index into the result, e.g. `tfs(x)[1]` or use `m, _ = tfs(x)`, where `_` is just some identifier right now, but might do the same thing as `~` in the future.

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [December 11, 2016, 5:39pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/9 "2016-12-11T17:39:24Z")

</div>

There is currently no way of only returning `m`, but you can do

```julia
m, s = tfs(x)

```

see [http://docs.julialang.org/en/release-0.5/manual/functions/#multiple-return-values](http://docs.julialang.org/en/release-0.5/manual/functions/#multiple-return-values)

To signify that I won’t be using a variable I use `_`

```julia
m, _ = tfs(x)

```

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 11, 2016, 5:49pm UTC](https://discourse.julialang.org/t/call-a-function-within-the-same-file/865/10 "2016-12-11T17:49:01Z")

</div>

pfitzseb and vchuravy - Thanks very much!!

Is there a way to determine how many output arguments a function needs to return depending on the number of output arguments being supplied at the call of the function? In Matlab, `nargout` is used and I noted that Julia doesn’t offer such a thing as indicated in the difference doc: [http://docs.julialang.org/en/latest/manual/noteworthy-differences/#noteworthy-differences-from-matlab](http://docs.julialang.org/en/latest/manual/noteworthy-differences/#noteworthy-differences-from-matlab)

> In Julia, multiple values are returned and assigned as tuples, e.g. (a, b) = (1, 2) or a, b = 1, 2. MATLAB’s nargout, which is often used in MATLAB to do optional work based on the number of returned values, does not exist in Julia. Instead, users can use optional and keyword arguments to achieve similar capabilities.

So I wonder what the ‘optional and keyword arguments’ are? And is there any workaround at all to achieve what `nargout` does in Matlab? Thanks!!
