Open In App

Recursion in Julia

Last Updated : 22 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Julia is a dynamic, high-level programming language with high performance and speed that is used to perform operations in scientific computing. It is great for computational complex problems. It is an open-source language so all source code is available easily online.

It is as easy to use as Python but it has much faster execution compared to R and Python. Julia is a general-purpose language and can be used for various tasks such as statistical computations, data analysis, web development, game development and more.

Ways to run Julia: 

  • Through a .jl file in an IDE
  • Command by command in Julia REPL(Read Evaluate Print Loop)

As Julia is a lot similar to other mostly used languages syntactically, it is easier to code and learn Julia. Julia programs are to be saved in a file with .jl extension.

Simple Julia Program:

Simple program to print Hello Geeks, just type the following code after the start of the interpreter.

Julia




# Julia code to print Hello Geeks
  
# print function
print("Hello Geeks!")


Before we see Recursion in Julia, we need to know what actually recursion is.

What is Recursion?

It is a process in which a function calls itself either directly or indirectly. It is a technique that helps us to solve complex problems easily with elegant code. Classical problems of recursion include the Towers of Hanoi.

The idea of recursion is to represent a problem in terms of smaller problems, and add some conditions to stop it at some base levels.

Mathematical Interpretation 

An object possesses recursive behaviour if it can be defined by the following properties:

  • Base Case- it is a terminating scenario that does not use recursion further to form an answer
  • Recursive Step- steps that reduce following cases towards the base case

Julia – Recursion 

Let us see how recursion works in Julia language with the help of examples.

Example 1: 

In this example, we are finding the multiplication of two numbers. Say num1*num2. It is the same as adding num1 to itself repeated num2 times. For example – 5*4 is the same as 5+5+5+5. We will code this problem in both ways iterative and recursive.

Iterative Code:

Julia




function mult_fn(num1, num2)  
    res = 0
  # iterative approach
  for i in 1:num2
    res += num1
  end
  return res
end
    
# Function call
ans = mult_fn(10, 11)
println(ans)


Output:

Recursive Code:

Julia




function mult_fn(num1, num2)
    # Base Case
    if num2 == 1
       return num1
    
    else
       # recursive call
       return num1 + mult_fn(num1, num2 - 1)
  end
end
    
# Function call
ans = mult_fn(10, 11)
println(ans)


Output: 

Example 2:

In this example, we are finding the factorial of a number. n!=n*(n-1)*(n-2) . . . . *1. Let’s say the number is 5. 5!= 5*4*3*2*1=120. In Julia factorial can be found out in three ways – using the built-in factorial function, another way is the iterative way and the third one is the recursive way.

Using Built-In function-  

Julia




# using built-in function
ans = factorial(5)
println(ans)


Output:

Iterative Code-

Julia




function fact_fn(num)  
    res = 1
  # iterative approach
  for i in 1:num
    res *= i
  end
  return res
end
    
# Function call
ans = fact_fn(10)
println(ans)


Output – 

Recursive Code-

Julia




function fact_fn(num)
    # Base Case
    if num == 1
       return 1
    
    else
       # recursive call
       return num*fact_fn(num - 1)
  end
end
    
# Function call
ans = fact_fn(5)
println(ans)


Output- 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads