Menu

Function in Julia

Written by Kabir | 4 min read

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

Julia
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.
python
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.
julia
# 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))
python
# Output : 
144
Alternatively, you can define the same function in more compact form, called “assignment form”.
julia
# Create the same function in assignment form
math_formula(x,y,z) = (x*y)^z

# Call the function 
println(math_formula(3,4,2))
python
# 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
julia
# 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))
python
# 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
julia
# 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))
python
# 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
julia
# 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)
python
# Output :
"negative"

3. Muliple return functions

Functions can return multiple outputs as well.
julia
# 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))
python
# 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
julia
# Add 3 numbers with "+" operator
x = 1 + 2 + 3

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

5. Practice Exercises

Q1. Create a function which returns nothing
julia
function nothing_formula()
    return nothing
end

println(nothing_formula())
python
# 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”
julia
function employee_evaluation(name, rating = "Above Expectations")
    output = "Appraisal Rating of "*name*" is: "*rating
end

println(employee_evaluation("Kabir"))
python
# 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
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Kabir
Written by
Related Course
Master Julia — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science