Ch 13 - Case Study: Data Structure Flashcards

1
Q

Think Julia

How to create an optional parameter for a function?

A

Optional Parameters

We have seen built-in functions that take optional arguments. It is possible to write programmer-defined functions with optional arguments, too. For example, here is a function that prints the most common words in a histogram:

function printmostcommon( hist, num = 10)
….t = mostcommon( hist)
….println(“ The most common words are: “)
….for (freq, word) in t[ 1: num]
……..println( word, “\ t”, freq)
….end
end

The first parameter is required; the second is optional. The default value of num is 10.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5133-5152). Kindle Edition.

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

Think Julia

Where must optional parameter be in parameter list?

A

If a function has both required and optional parameters, all the required parameters have to come first, followed by the optional ones.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5160-5161). Kindle Edition.

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