Ch 3 - Functions Flashcards

1
Q

Think Julia

What is a: function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Think Julia

What is a: function call

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Think Julia

What is a: parse()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Think Julia

What is a: trunc()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Think Julia

What is a: float()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Think Julia

What is a: string()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Think Julia

What is: log10()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Think Julia

What is: log()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Think Julia

What is: sin()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Think Julia

What is a: function definition

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Think Julia

What is the rules for a function name?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Think Julia

What is a: header

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Think Julia

What is a: body

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Think Julia

When is an function argument evaluated?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Think Julia

How often will a function argument be evaluated?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Think Julia

How can you print the value -> nothing.

A

julia > result = printtwice(“ Bing”)
Bing
Bing

julia > show( result)
nothing

To print the value nothing, you have to use the function show, which is like print but can handle this special value. The value nothing is not the same as the string “nothing”. It is a special value that has its own type:

julia > typeof( nothing)
Nothing

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 944-952). Kindle Edition.