Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

julia

What are the functions in Julia?

Anusheh Zohair Mustafeez

Functions in Julia are objects that map tuples of argument values to one or more return values. The basic syntax for functions in Julia is quite similar to that of Python, however, instead of the def keyword, Julia uses the function keyword. One more addition is that Julia uses the end keyword to mark the end of a function’s body. A simple example of this syntax is:

# Basic function syntax
function product(x, y)
        println("Product calculated!")
        return x * y
    end
print(product(3,4))
null

Assignment form

We can also write the above function in a more compact form called, assignment form. The above function in assignment form is shown below:

# Assignment form
product(x,y) = x * y
print(product(3,4))

Functions as objects

Since Julia considers functions to be objects, if the function is accessed without parenthesis, the function object is retrieved and can then be passed around like other values. For example, we can make a new function multiply. This has the same functionality as product using the = operator:

# Function name without parenthesis, refers to the function object
multiply = product 

Return keyword

As in other languages, the return keyword is the last instruction evaluated in a function. Thus, any lines in the function body after the return statement will not be executed. For instance, “Product calculated!” will not be printed in the example below:

# The print statement after return will not be executed
function product(x, y)
        println("Product calculated!")
        return x*y
    end
print(product(3,4))

Return type

If one thing is for certain about a function’s return type, it is that Julia allows the declaring return type to use the :: operator. For example, suppose our product function will always return an Int8. In this case, we can write it as follows:

# Declaring return type
function product(x,y)::Int
            print("Product calculated!")
            return x * y
        end

Implicit returning

An interesting feature in Julia is that it implicitly returns the last evaluated expression even if no return statement is in place. So, our product function will yield the same output as before with the syntax below:

# Implicitly returning last expression
function product(x,y)
        println("Product calculated!")
        x * y
    end
print(product(3,4))

Returning nothing

Although implicit returning may be useful in some cases, there are situations when one requires a function that does not return anything. In this situation, one can return a singleton object of type nothing, as shown in the snippet below:

# Returning nothing ensures product is not implicitly returned
function printproduct(x, y)
        p = x * y
        println("Product: $p")
        return nothing
    end
printproduct(3,4)

Returning a tuple

Julia also allows the return of more than one value in the form of tuples. To do so, return values are listed with commas separating them, as shown below:

## returning incremented values of x and as a tuple
function increment(x,y)
        return x + 1, y + 1
    end
print(increment(1,2))

RELATED TAGS

julia

CONTRIBUTOR

Anusheh Zohair Mustafeez
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring