1 of 10

-> Function� -> Defining a function� -> Calling a function� -> Importance of function

2 of 10

  • 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.

  • Defining a function

Creating/writing a function

  • Calling a function

Using the function in the main code

3 of 10

Importance of functions:

  • Functions make the code shorter

  • We can reuse the functions repeatedly in the code.

  • Reusing the function saves time and reduces error (Need not rewrite the same code multiple times)

  • Functions make the code easier to understand or read (by giving meaningful names to blocks of code)

4 of 10

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

5 of 10

# 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

6 of 10

# 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

7 of 10

Calling function with arguments

FunctionName_Space_argument name

Example: until safeFrom tiger

FunctionName_ no space and argument name with parentheses.

Example: until safeFrom(tiger)

8 of 10

Comment

  • A comment is a line in the code that is marked with the pound symbol (#) at the beginning.
  • The computer does not treat this line as an instruction. Rather, it is used by the programmers who write and read the code in order to understand one another.
  • Comments are used when we want to write notes for the code readers.

9 of 10

Debugging

  • Debugging is the process of finding and fixing errors in a program.

  • Errors in a program are called bugs or defects. They can cause the program to behave unexpectedly or not work at all.

  • Debugging helps make sure that a program is working correctly and doing what it is supposed to do.

10 of 10

Ambiguous instruction:

Instructions that can possibly have more than one meaning.