January 2020
Intermediate to advanced
532 pages
13h 31m
English
Functions are said to be first-class when they can be assigned to variables or struct fields, passed into functions, returned from a function, and so on. They are treated as first-class citizens just like regular data types. We will now take a look at how functions can be passed around like regular data values.
Let's design a new function that can propel a spaceship to leap in a random direction for a random distance. You may recall from the beginning of this chapter that we have already defined four move functions—move_up!, move_down!, move_left!, and move_right!. Here's our strategy:
Read now
Unlock full access