Function in Julia

Function is a block of organized, reusable code that accepts input and returns output.

All the computations you wish to do needs to be declared inside the body of the function. In order to call a function, you need to add `( )` along with any parameters inside it.

Content

  1. Functions in Julia
  2. Return keyword
  3. Multi-return Functions
  4. Operators as Functions
  5. Practice Exercise

1. Introduction to Functions

Function is a block of organized, reusable code that accepts input and returns output.

All the computations you wish to do needs to be declared inside the function i.e in the body of the function. It gives output only when it is called.

You must be thinking why should I write a function rather I will just write a block of code and run it.

The idea of building a function is to put all the repeated tasks together in the body of a function. Later on, call the function in just one line, rather than writing the same code again and again.

Let’s see how to build a function.

Syntax of function

function function_name(input)
    # computations
    return ouptut
end

Let’s see the implementation. Say you have a mathematical equation, which is going to be used multiple times in your project.

f(x,y,z) = (x*y)^z

Now you wish to create a function in Julia so that you won’t have to write the code again and again.

# Create a function which serves the purpose of the above stated mathematical function
function math_formula(x,y,z)
    return (x*y)^z
end

# Call the function 
println(math_formula(3,4,2))
# Output : 
144

Alternatively, you can define the same function in more compact form, called “assignment form”.

# Create the same function in assignment form
math_formula(x,y,z) = (x*y)^z

# Call the function 
println(math_formula(3,4,2))
# Output : 
144

2. Return keyword

Return keyword is generally used at end of the function to specify the output of the function. If the return statement is not defined in Julia, the function’s output is the last variable/computation by default.

R and Python doesn’t work like this. Bit confused right? Let’s see with examples

# Create a function with return statement missing
function math_formula(x,y,z)
    output1 = (x*y)^z
    output2 = (x+y)^z
end

println(math_formula(3,4,2))
# Output :
49

By default the function has returned the last variable in body of function i.e output2 . Let’s see how to return output1

# Create a function which return statement
function math_formula(x,y,z)
    output1 = (x*y)^z
    output2 = (x+y)^z
    return output1
end

println(math_formula(3,4,2))
# Output :
144

Both the functions are defined same except the return statement. The first one returns the last variable by default but the second one returns the variable with its return keyword.

Note : Defining the return statement is a good practice in programming.

Return keyword plays a vital role when you want to define some conditional function. Let’s see it with help on an example

# Create a function to return the sign of a number wheter it's positive , negative or zero
function sign(x)
    if x > 0
        return "positive"
    elseif x == 0
        return "zero"
    else
        return "negative"
    end       
end

sign(-5)
# Output :
"negative"

3. Muliple return functions

Functions can return multiple outputs as well.

# Create a function which gives two outputs
function math_formula(x,y,z)
    output1 = (x*y)^z
    output2 = (x+y)^z
    return output1,output2
end

println(math_formula(3,4,2))
# Output :
(144, 49)

4. Operators as functions

The operators in Julia are also functions. Operators can also be used as a function. Let’s see it with an example

# Add 3 numbers with "+" operator
x = 1 + 2 + 3

# Add 3 numbers with "+" operator like a function
+(1,2,3)
# Output :
6

5. Practice Exercises

Q1. Create a function which returns nothing

function nothing_formula()
    return nothing
end

println(nothing_formula())
# Output :
nothing

Q2. Create a function which accepts the employee name, and his/her appraisal rating. Return both the name and appraisal rating. If the rating is missing in the function call, it should automatically return the rating as “Above Expectations”

function employee_evaluation(name, rating = "Above Expectations")
    output = "Appraisal Rating of "*name*" is: "*rating
end

println(employee_evaluation("Kabir"))
# Output :
Appraisal Rating of Kabir is: Above Expectations

Conclusion

So, now you should have a fair idea of how to use functions in Julia. Next, I will see you with more Data Science oriented topics in Julia.

Read more about Julia here