1.3. Functions#

  • A function is a named sequence of statements that performs a computation

  • You can call the function by its name followed by parentheses, optionally passing comma-separated expressions inside the parentheses as arguments

  • For example, we already used the println(x) function, which prints x and returns nothing

  • Julia defines a number of standard mathematical functions, e.g.

Function

Description

abs(x)

a positive value with the magnitude of x

sign(x)

indicates the sign of x, returning -1, 0, or +1

sqrt(x), √x

square root of x

cbrt(x), ∛x

cube root of x

exp(x)

natural exponential function at x

log(x)

natural logarithm of x

log(b,x)

base b logarithm of x

log2(x)

base 2 logarithm of x

log10(x)

base 10 logarithm of x

1.3.1. Trigonometric and hyperbolic functions#

  • All the standard trigonometric and hyperbolic functions are also defined:

sin    cos    tan    cot    sec    csc
sinh   cosh   tanh   coth   sech   csch
asin   acos   atan   acot   asec   acsc
asinh  acosh  atanh  acoth  asech  acsch
sinc   cosc
  • The following trigonometric functions use degrees instead of radians:

sind   cosd   tand   cotd   secd   cscd
asind  acosd  atand  acotd  asecd  acscd

1.3.2. Example: e to the pi Minus pi#

Evaluate \(e^\pi - \pi\), see if it equals 20 (it should not, but it is remarkably close):

exp(π) - π
19.999099979189474

xkcd217_e_to_the_pi_minus_pi

(from https://xkcd.com/217)

1.3.3. Example: Real roots of quadratic#

  • Use the quadratic formula

\[ r = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]

to solve the equation \(x^2 + 5x + 6 = 0\) (assuming the roots are real, more about complex numbers later)

# Solve x^2 + 5x + 6 = 0 using the quadratic formula (real arithmetics)

# Coefficients a,b,c in ax^2 + bx + c = 0
a = 1
b = 5
c = 6

# The quadratic formula
d = sqrt(b^2 - 4*a*c)
r1 = (-b - d) / 2a
r2 = (-b + d) / 2a
println("The roots are ", r1, " and ", r2)
The roots are -3.0 and -2.0

1.3.4. User-defined functions#

You can also define your own functions. This can help make your program easier to read, and eliminate repetitive code. For example, consider the following function named myfunc:

function myfunc(x,y)
    x + y
end
myfunc (generic function with 1 method)
  • This function takes two arguments, and assigns them to parameters named x and y.

  • The function evaluates the sum of x and y

  • The function returns (by default) the last expression evaluated, which in this case is the sum (use the return keyword to change this behavior)

  • The function can be called using the standard parentheses syntax

myfunc(1,2)
3

1.3.4.1. Compact “assignment” form#

  • Functions that consist of a single expression can also be defined using the following syntax:

myfunc2(x,y) = x + 2y
myfunc2(3,5)
13

1.3.4.2. Anonymous functions#

  • Yet another way to define a function, which has the benefit that it does not need to be given a name which can be convenient e.g. if it is only used temporarily:

myfunc3 = (x,y) -> x + 3y
myfunc3(3,5)

# Same thing without giving the function a name:
((x,y) -> x + 3y)(3,5)
18

1.3.4.3. Multiple return values#

  • A function can return multiple values, separated by commas:

function mynewfunc(x,y)
    out1 = x + y
    out2 = out1 * (2x + y)
    out1, out2
end
mynewfunc (generic function with 1 method)
y1, y2 = mynewfunc(2,1)
(3, 15)

1.3.5. Example: Real roots of quadratic function#

Solve the same problem as before with a user-defined function:

function real_roots_of_quadratic(a,b,c)
    # Compute the real roots of the quadratic ax^2 + bx + c = 0
    d = sqrt(b^2 - 4*a*c)
    r1 = (-b - d) / 2a
    r2 = (-b + d) / 2a
    r1, r2
end
real_roots_of_quadratic (generic function with 1 method)
real_roots_of_quadratic(1, 5, 6)
(-3.0, -2.0)
real_roots_of_quadratic(-1, 5, 6)
(6.0, -1.0)