# Can I make a Function Call Itself?

**URL:** <https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 5, 2021, 9:41am UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067 "2021-07-05T09:41:21Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![marianoarnaiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marianoarnaiz/32/19377_2.png) [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Post date:** [July 5, 2021, 9:41am UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/1 "2021-07-05T09:41:21Z")

</div>

Hi everyone.  
This might be a REALLY silly question, but I wonder if it is possible.

I am making this function Set\_Up(). It is design to load all other functions in my code, force precompilation, load global variables, etc.

Because sometimes something goes wrong I have:

```julia
try

Do a lot of things

catch e

    printstyled(" An error ocurred.",color=:red)
    printstyled("Please run Set_Up() again.",color=:red)
    sleep(1)

end

```

I wonder if inside the catch I can tell the function to run itself again. Something like:

```julia
try

Do a lot of things

catch e

    printstyled(" An error ocurred.",color=:red)
    printstyled("Set_Up() will run again after 1 second.",color=:red)
    sleep(1)
    Set_Up()
end

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [July 5, 2021, 10:15am UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/2 "2021-07-05T10:15:54Z")

</div>

Yes. I actually implemented this for a network handshaking function a few years ago. I did also have a “retries” variable that increments on each restart, with provision to exit if too many retries. This kept the function from being stuck too long (in practice the restart hardly ever happened, without there being a network glitch).

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [July 5, 2021, 11:11am UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/3 "2021-07-05T11:11:57Z")

</div>

There should be no difficulty in practice.  
For example, the following function will run successively once being called.

```julia
function hello()
  println("hello")
  hello()
end

```

But I guess finding the reason that leads to “something goes wrong” makes more sense here.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 5, 2021, 11:19am UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/4 "2021-07-05T11:19:31Z")

</div>

This is called [recursion](https://en.wikipedia.org/wiki/Recursion_(computer_science)), an important technique in computer science. Note that each call allocates on the stack, so if you go too deep you will get a [stack overflow](https://en.wikipedia.org/wiki/Stack_overflow) (which gave its name to a famous website).

```julia
julia> counter = 0;

julia> function f()
           global counter += 1
           f()
       end;

julia> f()
ERROR: StackOverflowError:
Stacktrace:
 [1] f()
   @ Main ./REPL[12]:2
 [2] f() (repeats 79980 times)
   @ Main ./REPL[12]:3

julia> counter
130830

```

So `f` was able to call itself recursively 130830 times before the stack grew too big.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [July 5, 2021, 11:30am UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/5 "2021-07-05T11:30:21Z")

</div>

> [@sijo](#):
>
> This is called [recursion](https://en.wikipedia.org/wiki/Recursion_(computer_science)), an important technique in computer science. Note that each call allocates on the stack, so if you go too deep you will get a [stack overflow](https://en.wikipedia.org/wiki/Stack_overflow) (which gave its name to a famous website).

Well, don’t keep us in suspense forever, what is the name of this famous website?

---

<div class="post-metadata">

**Author:** ![marianoarnaiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marianoarnaiz/32/19377_2.png) [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Post date:** [July 5, 2021, 11:52am UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/6 "2021-07-05T11:52:01Z")

</div>

Hmm Hi guys.  
I tried this… but my code just gets into a loop and never actually finishes… Not sure why.  
I wonder if there is a line to kill the current execution and then call the function again.

---

<div class="post-metadata">

**Author:** ![marianoarnaiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marianoarnaiz/32/19377_2.png) [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Post date:** [July 5, 2021, 11:53am UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/7 "2021-07-05T11:53:14Z")

</div>

What goes wrong is that some libraries in the OS need to be called twice (GMT package) for them to work. This is a problem of the package … and I can’t fix it hehehe.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [July 5, 2021, 12:12pm UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/8 "2021-07-05T12:12:01Z")

</div>

> [@StevenSiew](#):
>
> Well, don’t keep us in suspense forever, what is the name of this famous website?

Can’t tell if joking… [https://stackoverflow.com/](https://stackoverflow.com/)

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [July 5, 2021, 2:46pm UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/9 "2021-07-05T14:46:55Z")

</div>

With recursion, at some point you have to return anything which is not the function call to ensure that the recursion terminates. You do this via a conditional like “if counter \> 100” and return nothing for that case.

For some kind of repeated call, you can also use retry (from HTTP if I’m not mistaken)

---

<div class="post-metadata">

**Author:** ![marianoarnaiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marianoarnaiz/32/19377_2.png) [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Post date:** [July 5, 2021, 7:05pm UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/10 "2021-07-05T19:05:14Z")

</div>

Can any one give me an example to make sure that the recursion happens only once?

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 5, 2021, 7:24pm UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/11 "2021-07-05T19:24:12Z")

</div>

```julia
function myrecursion(depth::Int,arg)
    if depth>=2
        return nothing 
    else 
        println(arg)
        @info("myrecursion was called (depth=$(depth))")
        return myrecursion(depth+1,arg)
    end
end

myrecursion(0,"foo")
0

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [July 5, 2021, 7:46pm UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/12 "2021-07-05T19:46:23Z")

</div>

3 posts were split to a new topic: [Does Julia have tail call optimization?](https://discourse.julialang.org/t/does-julia-have-tail-call-optimization/64101)

---

<div class="post-metadata">

**Author:** ![jeffreyesun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreyesun/32/13379_2.png) [@jeffreyesun](https://discourse.julialang.org/u/jeffreyesun)\
**Post date:** [July 15, 2021, 8:23pm UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/13 "2021-07-15T20:23:12Z")

</div>

I’m late to the party, but I want to point out that AFAIK the better practice would be to use a loop as opposed to recursion. Something like:

```julia
function Try_Set_Up()
  try
    # Do setup stuff
  catch e
    # Report error
    return false
  end
  return true
end

function Set_Up_Keep_Trying()
  setupsuccess = false
  while !setupsuccess
    setupsuccess = Try_Set_Up()
    if !setupsuccess
      sleep(1)
    end
  end
end

```

Or if you wanna be slick:

```julia
function Set_Up_Keep_Trying()
  while true
    Try_Set_Up() ? break : sleep(1)
  end
end

```

To limit the number of iterations:

```julia
function Set_Up_Keep_Trying(; attempts=10)
  setupsuccess = false
  for i in 1:attempts
    if Try_Set_Up()
      setupsuccess = true
      break
    else
      sleep(1)
    end
  end
  setupsuccess || println("Setup failed after $attempts attempts")
end

```

---

<div class="post-metadata">

**Author:** ![marianoarnaiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marianoarnaiz/32/19377_2.png) [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Post date:** [July 16, 2021, 7:23am UTC](https://discourse.julialang.org/t/can-i-make-a-function-call-itself/64067/14 "2021-07-16T07:23:09Z")

</div>

Great! Thank @jeffreyesun
