Ch 3 - Functions Flashcards
Think Julia
What is a: function
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.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 656-658). Kindle Edition.
Think Julia
What is a: function call
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.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 659-667). Kindle Edition.
Think Julia
What is a: parse()
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”)
32
julia > parse( Float64, “3.14159”)
3.14159
julia > parse( Int64, “Hello”)
ERROR: ArgumentError: invalid base 10 digit ‘H’ in “Hello”
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 680-682). Kindle Edition.
Think Julia
What is a: trunc()
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)
3
julia > trunc( Int64, -2.3)
-2
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 683-690). Kindle Edition.
Think Julia
What is a: float()
float converts integers to floating-point numbers:
julia > float( 32)
32.0
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 691-695). Kindle Edition.
Think Julia
What is a: string()
Finally, string converts its argument to a string:
julia > string( 32)
“32”
julia > string( 3.14159)
“3.14159”
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 695-702). Kindle Edition.
Think Julia
What is: log10()
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 are defined). log, which computes natural logarithms, is also provided:
ratio = signal_power / noise_power
decibels = 10 * log10( ratio)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 710-713). Kindle Edition.
Think Julia
What is: log()
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 are defined). log, which computes natural logarithms, is also provided:
ratio = signal_power / noise_power
decibels = 10 * log10( ratio)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 710-713). Kindle Edition.
Think Julia
What is: sin()
This next example finds the sine of radians. The name of the variable is a hint that sin and the other trigonometric functions (cos, tan, etc.) take arguments in radians:
radians = 0.7
height = sin( radians)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 719-721). Kindle Edition.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 714-718). Kindle Edition.
Think Julia
What is a: function definition
So far, we have only been using the functions that come with Julia, but it is also possible to add new functions. A function definition specifies the name of a new function and the sequence of statements that run when the function is called. Here is an example:
function printlyrics()
….println(“ I’m a lumberjack, and I’m okay.”)
….println(“ I sleep all night and I work all day.”)
end
function is a keyword that indicates that this is a function definition. The name of the function is printlyrics. The rules for function names are the same as for variable names: they can contain almost all Unicode characters (see “Characters”), but the first character can’t be a number. You can’t use a keyword as the name of a function, and you should avoid having a variable and a function with the same name.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 764-770). Kindle Edition.
Think Julia
What is the rules for a function name?
function is a keyword that indicates that this is a function definition. The name of the function is printlyrics. The rules for function names are the same as for variable names: they can contain almost all Unicode characters (see “Characters”), but the first character can’t be a number. You can’t use a keyword as the name of a function, and you should avoid having a variable and a function with the same name.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 764-770). Kindle Edition.
Think Julia
What is a: header
The first line of the function definition is called the header; the rest is called the body. The body is terminated with the keyword end, and it can contain any number of statements. For readability the body of the function should be indented.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 773-779). Kindle Edition.
Think Julia
What is a: body
The first line of the function definition is called the header; the rest is called the body. The body is terminated with the keyword end, and it can contain any number of statements. For readability the body of the function should be indented.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 773-779). Kindle Edition.
Think Julia
When is an function argument evaluated?
julia > printtwice(“ Spam “^ 4)
Spam Spam Spam Spam
Spam Spam Spam Spam
julia > printtwice( cos( π))
-1.0
-1.0
The argument is evaluated before the function is called, so in these examples the expressions “Spam “^ 4 and cos( π) are only evaluated once.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 857-863). Kindle Edition.
Think Julia
How often will a function argument be evaluated?
julia > printtwice(“ Spam “^ 4)
Spam Spam Spam Spam
Spam Spam Spam Spam
julia > printtwice( cos( π))
-1.0
-1.0
The argument is evaluated before the function is called, so in these examples the expressions “Spam “^ 4 and cos( π) are only evaluated once.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 857-863). Kindle Edition.