-> Function� -> Defining a function� -> Calling a function� -> Importance of function
> A set of instructions that perform specific task.
> The computer executes (runs) the function only when we call it, meaning when we use it in the main code.
Creating/writing a function
Using the function in the main code
Importance of functions:
Defining and calling a function
# Define a function to check if a banana is rotten
isRotten = (b) ->
…….return b.rotten()
# Use the function in a condition
for b in bananas
……if not isRotten b
..................goto b
# Define a function to wait until the tiger stops playing
safeFrom = (t) ->
……..if not t.playing()
…………..return false
……...else
…………..return true
# Use the function and move to banana
until safeFrom(tiger)
……..wait()
goto banana
# Define a function to wait until the tiger stops playing
safeFrom = (t) ->
……..return not t.playing()
# Use the function and move to banana
until safeFrom tiger
……..wait()
goto banana
Calling function with arguments
FunctionName_Space_argument name
Example: until safeFrom tiger
FunctionName_ no space and argument name with parentheses.
Example: until safeFrom(tiger)
Comment
Debugging
Ambiguous instruction:
Instructions that can possibly have more than one meaning.