Chapter 3. Functions
In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can “call” the function by name.
Function Calls
We have already seen one example of a function call:
julia>println("Hello, World!")Hello, World!
The name of the function is println. The expression in parentheses is called the argument of the function.
It is common to say that a function “takes” an argument and “returns” a result. The result is also called the return value.
Julia provides functions that convert values from one type to another. The parse function takes a string and converts it to any number type, if it can, or complains otherwise:
julia>parse(Int64,"32")32julia>parse(Float64,"3.14159")3.14159julia>parse(Int64,"Hello")ERROR: ArgumentError: invalid base 10 digit 'H' in "Hello"
trunc can convert floating-point values to integers, but it doesn’t round off; it chops off the fraction part:
julia>trunc(Int64,3.99999)3julia>trunc(Int64,-2.3)-2
float converts integers to floating-point numbers:
julia>float(32)32.0
Finally, string converts its argument to a string:
julia>string(32)"32"julia>string(3.14159)"3.14159"
Math Functions
In Julia, most of the familiar mathematical functions are directly available. The following example uses log10 to compute a signal-to-noise ratio in decibels (assuming that signal_power and noise_power ...