Ch 6 - Fruitful Functions Flashcards

1
Q

Think Julia

What is a void function?

A

Return Values

Calling a function generates a return value, which we usually assign to a variable or use as part of an expression:

e = exp( 1.0)
height = radius * sin( radians)

The functions we have written so far are void. Speaking casually, they have no return value; more precisely, their return value is nothing. In this chapter, we are (finally) going to write fruitful functions. The first example is area,

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1961-1971). Kindle Edition.

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

Think Julia

Is the return keyword necessary to return a value?

A

function area( radius)
….a = π * radius ^ 2
….return a
end

We have seen the return statement before, but in a fruitful function the return statement includes an expression. This statement means: “Return immediately from this function and use the following expression as a return value.” The expression can be arbitrarily complicated, so we could have written this function more concisely:

function area( radius)
….π * radius ^ 2
end

However, temporary variables like a and explicit return statements can make debugging easier.

The value returned by a function is the value of the last expression evaluated, which, by default, is the last expression in the body of the function definition.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1972-1975). Kindle Edition.

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

Think Julia

What is one reason for using temporary variables?

A

function area( radius)
….a = π * radius ^ 2
….return a
end

We have seen the return statement before, but in a fruitful function the return statement includes an expression. This statement means: “Return immediately from this function and use the following expression as a return value.” The expression can be arbitrarily complicated, so we could have written this function more concisely:

function area( radius)
….π * radius ^ 2
end

However, temporary variables like a and explicit return statements can make debugging easier.

The value returned by a function is the value of the last expression evaluated, which, by default, is the last expression in the body of the function definition.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1972-1975). Kindle Edition.

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

Think Julia

What is incremental development?

A

Incremental Development

As you write larger functions, you might find yourself spending more time debugging.

To deal with increasingly complex programs, you might want to try a process called incremental development. The goal of incremental development is to avoid long debugging sessions by adding and testing only a small amount of code at a time.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2021-2028). Kindle Edition.

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

Think Julia

How can you create subscript variables?

A

The subscript numbers are available in the Unicode character encoding (_1 TAB, _2 TAB, etc.).

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2039-2041). Kindle Edition.

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

Think Julia

What is composition?

A

Composition

As you should expect by now, you can call one function from within another. As an example, we’ll write a function that takes two points, the center of the circle and a point on the perimeter, and computes the area of the circle.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2102-2106). Kindle Edition.

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

Think Julia

What is a boolean function?

A

Boolean Functions

Functions can return Booleans, which is often convenient for hiding complicated tests inside functions. For example:

function isdivisible( x, y)
….if x % y = = 0
……..return true
….else
……..return false
….end
end

It is common to give Boolean functions names that sound like yes/ no questions; isdivisible returns either true or false to indicate whether x is divisible by y.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2133-2145). Kindle Edition.

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

Think Julia

How are boolean functions commonly named?

A

Boolean Functions

Functions can return Booleans, which is often convenient for hiding complicated tests inside functions. For example:

function isdivisible( x, y)
….if x % y = = 0
……..return true
….else
……..return false
….end
end

It is common to give Boolean functions names that sound like yes/ no questions; isdivisible returns either true or false to indicate whether x is divisible by y.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2133-2145). Kindle Edition.

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

Think Julia

What might cause a recursive function with a base case to overflow?

A

It looks like an infinite recursion. How can that be? The function has a base case— when n = = 0. But if n is not an integer, we can miss the base case and recurse forever.

In the first recursive call, the value of n is 0.5. In the next, it is -0.5. From there, it gets smaller (more negative), but it will never be 0.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2276-2280). Kindle Edition.

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

Think Julia

What is a: temporary variable

A

temporary variable

A variable used to store an intermediate value in a complex calculation.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2354-2356). Kindle Edition.

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

Think Julia

What is a: scaffolding

A

scaffolding

Code that is used during program development but is not part of the final version.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2363-2365). Kindle Edition.

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

Think Julia

What is a: guardian

A

guardian

A programming pattern that uses a conditional statement to check for and handle circumstances that might cause an error.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2365-2368). Kindle Edition.

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