Functions in Julia
9 min read
Table of contents
- What is a Function?
- Importance of Functions
- Creating Functions in Julia
- Parameters and Arguments
- Positional, Keyword, and Optional Arguments
- Naming conventions for Functions in Julia
- More forms of functions in Julia
- Function Composition and Piping in Julia
Have you ever wondered how programming languages are able to execute complex tasks? Similar to how postmen and firemen have specific jobs, programmers use functions to break down complex tasks into smaller, more manageable parts. Let's explore the world of writing functions in Julia and learn how they make software development possible.
What is a Function?
In programming, a function can be defined as a block of code that performs a specific task. It breaks down complex programs into smaller, more manageable parts, making them easier to write, debug, maintain, and reusable throughout the codebase.
Importance of Functions
Functions are a fundamental concept in programming and are of great importance in various ways.
Code Reusability: Functions allow one to write and reuse a piece of code multiple times. It takes in specific functionality that can be called when needed. This process reduces the amount of code written, making the code easier to understand and read.
Abstraction: With functions, details of a particular operation can be abstracted. Abstraction is a method for separating users from unimportant details. Code can be made adaptable and flexible when written in functions so that the parts of a codebase can be modified without impacting the entire program.
Code Organization: Functions make it easier to manage and present code in a proper format by separating code into logical units. By using functions to organize code, one will create a clear, concise structure that makes it easier to read and understand, especially for complex programs.
Code Efficiency: Functions allow code efficiency by writing reusable code. Rather than writing the same piece of code multiple times, functions make our programs easier and faster to write, thereby increasing performance and reducing potential bugs.
Modular programming: Functions allow one to break down code into manageable parts, making the code easier to debug, isolate, and address systematically.
Creating Functions in Julia
Functions can be inbuilt or defined. In Julia, there are various inbuilt functions like:
println- To print values to the console.
typeof- To return the type of the specified argument.
rand- To return a random floating number between 0 and 1.
sort- To sort an array of elements in ascending order.
To define a custom function, the
function keyword is used. The syntax for a custom function is
function name_of_function(parameters) # syntax in here end
When a variable is created inside a function, it is local, which means that it only exists inside the function.
An example of a custom function that can be created in Julia is a function that adds two numbers and returns the result
function add_two_numbers(x::Int, y::Int) return x + y end
Calling this function with the appropriate arguments will return the sum of the two numbers. Let
x be 3 and
y be 8
The result would be
Another example of a custom function is a function that calculates the factorial of a number
function factorial_number(n::Int) if n == 0 return 1 else return n * factorial(n-1) end end
Let’s pass in a number 4 into the function
The result would be
Note: Julia has an inbuilt method to find the factorial of numbers (
Parameters and Arguments
In the programming world, especially when it comes to functions, the terms “arguments” and “parameters” are often misued.
A parameter is a placeholder for the value that would be passed into a function. It is a variable defined by the function that receives a value when the function is called.
An argument is a value passed into a function when it is called. Arguments are always assigned to parameters of the function in the order in which they are passed.
In the example of
y are the parameters while
8 are the arguments.
Positional, Keyword, and Optional Arguments
Positional arguments are passed into a function without explicitly specifying the argument name. These arguments are matched to the function parameters in the order they are passed. An example of where positional arguments are used is in the
factorial_number functions above.
Keyword arguments are optional arguments that are specified by name rather than position. They are especially helpful when working with functions with many arguments since they give the user more freedom and control over how a function works. For example:
function print_name_age(name; age=0) println("Name: $name") println("Age: $age") end
The function takes a required argument
name and an optional keyword argument
age, with a default value of
0. The function prints the name and age.
println(print_name_age("Alice")) # prints "Name: Alice" and "Age: 0" println(print_name_age("Bob", age=30)) # prints "Name: Bob" and "Age: 30"
In the first call to
print_name_age, the default age,
0 would be printed alongside with the name
Alice. While in the second call, both the name and age were specified. The default age provided was then overridden by the age provided.
Optional arguments are arguments that do not have to be provided when calling a function, and they have default values that are used if no value is provided. In the
print_name_age function above, there is a required argument
name and an optional keyword argument
age, with a default value of
Naming conventions for Functions in Julia
The naming conventions for functions in Julia follow some of the same rules in Python and standard practices for naming functions. Some of them include the following:
Function names should start with lowercase lettering, except if the function is a type constructor or a macro.
Use underscores to separate words if a function's name consists of more than one word. For example,
add_two_numbersis a better function name than
Use plural names for functions that return collections. For example, if it is a function that returns a list of names, it should be named
Use descriptive names for functions. The name should convey the purpose of the function. For example,
parse_float()is a better function name than
Following naming conventions makes code more readable and easier to understand, especially in large organizations.
More forms of functions in Julia
Furthermore, in Julia, there are other forms of functions other than the regular function. Let’s explore some of the other forms of functions in Julia.
Anonymous functions, also known as lambda functions or function literals, are a type of function that can be defined without a name. It can be seen as a convenient way to define simple functions on the fly without needing to define a proper function.
In Julia, anonymous functions are defined using the
-> operator, followed by the function body. The syntax for defining an anonymous function is:
(argument list) -> function body
Below is an example of an anonymous function that squares its arguments using the REPL
julia> f = x -> x^2
An argument would be passed in to get a result with the anonymous function. Let’s place
3 into the function
julia> f(3) 9
The result of passing
3 would be
9 as the square.
Anonymous functions can also be used as arguments to other functions, leading us to the next type of function, Higher-Order functions.
Higher-Order functions take other functions as arguments or return functions as results. They are called "higher-order" because they operate on functions, which are first-class objects in Julia. Higher-order functions can be used to write more concise and modular code and are a powerful tool for functional programming. They allow for the composition of functions, which can help to break down complex tasks into smaller, more manageable pieces.
Below is an example of a higher-order function in Julia
function apply_twice(f, x) f(f(x)) end
The above function takes a function
f and an argument
x and applies
f twice to
x. We can call this function with any function that takes a single argument, such as the
In the example above,
sqrt() is the function
16 is the argument
apply_twice() function applies
sqrt() twice to
1.4142135623730951, which is the square root of
Higher-order functions and anonymous functions are related concepts in Julia, but they are not the same. Higher-order functions operate on functions, while anonymous functions are a convenient way to define simple functions on the fly. Anonymous functions can be passed as arguments to higher-order functions, making them a useful tool for building complex programs from simple building blocks.
Function Composition and Piping in Julia
Function composition and piping are two robust features in Julia that allows for the chaining of functions, making it possible to build complex operations from simpler building blocks. It is the process of combining two or more functions to create a new function. Function composition is done using the
∘(circled dot) operator. Below is an example of how to compose two functions:
julia> f(x) = 2x julia> g(x) = x + 1 julia> h = f ∘ g
In the example above, we define two functions,
f(x) = 2x and
g(x) = x + 1. We then compose them using the
∘operator to create a new function
h(x) = f(g(x)).
Passing an argument of 3 into function
h would give the result of
julia> h(3) 8
Similarly, piping is another way of achieving chaining in Julia. Its syntax is the
|> (pipe) operator. The pipe operator takes the function result on the left-hand side and passes it as the first argument to the function on the right-hand side. Below is an example of how to use pipes to achieve chaining:
julia> f(x) = 2x julia> g(x) = x + 1 julia> 3 |> g |> f 8
Advantages of Function Composition and Piping in Julia
Function composition and piping are both powerful features in Julia that have a lot of advantages. Some of these advantages include:
Flexibility: By using function composition and piping, complex data transformations and analyses can be easily customized to meet specific needs like adding or removing functions, changing the order of functions, or modifying the arguments passed into the function.
Readability: Function composition and piping makes code clear by a clear and concise syntax for chaining together multiple functions. This can make it simpler to maintain the code over time and less intellectually difficult to interpret.
Performance: Using Julia's built-in functions for function composition and chaining allows function composition and piping to be optimized for performance. The efficiency of the code can be increased by reducing memory allocation and the number of intermediate arrays.
Functions are a fundamental building block of any programming language, and Julia is no exception. Also, functions are a powerful feature that allows one to create reusable and modular code.
Functions are a vital feature of Julia that enables one to write reusable, modular, and efficient code. By leveraging the features of Julia's function system, complex programs that are both powerful and easy to understand can be created. In this article, we learned how to use functions, including parameters, arguments, positional, keyword, optional arguments, and Julia's naming convention of functions.
We also explored concepts like anonymous functions, function composition, piping, and higher-order functions in Julia.