Ch 13 - Case Study: Data Structure Flashcards
Think Julia
How to create an optional parameter for a function?
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.
Think Julia
Where must optional parameter be in parameter list?
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.