Functions in Julia


Functions form an integral part of any programming language because they increase code modularity, as well as making the code much more readable than unorganized nonfunction code.

  • In the tradition of programming languages in the functional approach, Julia considers functions “first-class citizens” (i.e., an entity that can implement all the operations -which are themselves functions- available to other entities).
  • This means, among other things, that Julia likes to work with functions without side effects and that you can follow the recent boom in functional programming without jumping into purely functional language.
  • Recall that functions in Julia use methods with multiple dispatch: each function can be associated with hundreds of different methods. Furthermore, you can add methods to an already existing function.
  • A list of the things covered in this chapter is provided as follows, which acts as a quick reference for you to go through:
    • Creating functions
    • Function arguments
    • Anonymous functions
    • Multiple dispatch
    • Recursion
    • Built-in functions

Creating functions

There are two ways to create a function

  • Functions in Julia are declared with the function keyword, which is then followed by the body of the function.
  • Another keyword, end, puts or marks a logical end to the function in general.
  • There are two ways to create a function
    • One-line
    • myfunction1(var) = var+1
    • Several lines
    • function myfunction2(var1, var2="Float64", var3=1)
          output1 = var1+2
          output2 = var2+4
          output3 = var3+3 # var3 is optional, by default var3=1
          return [output1 output2 output3]
      end
    • We can also have keyword argument, which can be ommitted
    • function myfunction3(var1, var2; keyword=2)
          output1 = var1+var2+keyword
      end
    • The difference between an optional argument and a keyword is that the keyword can appear in any place of the function call while the optional argument must appear in order.
  • The syntax of defining a function can be summarized as:
  • function name()
     …
     body
     …
    end
  • Program
  • julia> function greet()
         println("hello world")
         end
    greet (generic function with 1 method)
    julia> greet()
    hello world

Function Arguments

  • A function argument is a variable passed to a function in the form of an input so that it returns a specific output.
  • A very simple example of a function taking an argument is stated as follows:
  • julia> function hello(name)
     println("hello $name")
     end
    hello (generic function with 1 method)
    julia> hello("CseWorld Online")
    CseWorld Online
    

Anonymous Functions

  • Anonymous functions are shorthand notations for regular functions.
  • These are the choice of code when a function has to be used only a limited number of times, hence, it may be slightly easier and quicker to have them rather than using named functions.
  • In popular terms, they are also sometimes referred to as lambda functions.
  • In Julia, we define an anonymous function using the following syntax:
  • f -> 2f   // -> to notify that we are defining an anonymous function

Multiple Dispatch

  • In programming terms, dispatch means to send a message to a listener or a call to a function. Basically, to send a piece of data (or packet of information) to code that is ready to handle it.
  • Dispatch can be of many different types. Starting off with a few of them we have:
    • Static Dispatch
    • Dynamic Dispatch
    • Multiple Dispatch

Recursion

  • Julia functions can also make recursive calls, just like any other language.
  • Abstract functions allow for easy coding of advanced techniques such as recursion, closures, and currying. Recursion is a function that calls itself
  • function outer(a)
        b = a +2
        function inner(b)
            b = a+3
        end
        inner(b)
    end
    


You may also like: