27rohitb.github.io

Functions

Key points:



General Syntax:

function <name>(<arguments> ; <keyword args>)
  #
  # function body
  #
  return <var>    #this is OPTIONAL, returns last in by default
end

Calling a function:

<function name>(<args>, <keyword>=<arg>)

Smaller lambda type function:

<func_name>(<arguments>) = <function body>

Example:

c(r) = 2*pi*r

Anonymous function:

map(<var> -> <function_body>, <vector to be calculated>)

Example:

map(x -> 2 * pi * r, [1,3,5])

Other stuff:

Type decleration:

Make a filter on the input by explicitly declaring the type.

function circumference(r::Float64)

Arguments:

Default argument:

function U(m::real, h=100)

Keyword argument:

function U(m,h,;g)

#calling it as:
U(1,1,g=1)

Variable argument:

The ... is a variable argument, i.e, it can accept any number of parameters.

function sum(x, y...)