Julia function keyword | Create user-defined functions in Julia

Keywords are the reserved words in Julia which have a predefined meaning to the compiler. These keywords are used to reduce the number of lines in code. Keywords in Julia can’t be used as variable names.

'function' keyword is used to create user-defined functions in Julia. These functions are reusable codes that can be called from anywhere in the code with the name assigned to the function.

Syntax:

function function_name
    Statement
    Statement
end

Note: 'end' keyword is used to mark the ending of a function. Everything between the function keyword and end keyword is considered to be the body of the function.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate
# the use of 'function' keyword
  
# Defining a function
function func()
    println("this is a function")
end
   
# Function call
func()

chevron_right


Output:

this is a function

Creating Functions with arguments

Functions in Julia also accept arguments. These functions are also created in the same way by using the 'function' keyword.

Syntax:

function function_name(argument1, argument2, ...)
    statement
    statement
end

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining a function with arguments
function add_fn(x, y)
    println(x + y)
end
  
# Calling defined function
add_fn(10, 8)

chevron_right


Output:

18
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.