Preview

5 - Functions

 1. Run the program and play it. Functions always start with a "def" (define function_name) in python. What line can you spot one?

  There are no functions in this code

  Line 5

  Line 2

  Line 1

 2. Analyse the code and note that it creates three cakes. What code is needed to create a fourth cake next to the third?

  make_cake(150,0)

  There are no functions in this code

  Line 2

  make_cake(100,0)

 3. This program uses a function called triangle to create a triangle. What is happening on line 14 and 15?
Try adding more "triangle()"s to see what happens 

  These are lines are not needed, the are just writing the words "triangle()" which do nothing

  The function triangle which starts on line 6 is being" called" into execution

  These lines are reminding the program to stop

  These lines are naming the program (triangle). The second linei s not needed.

 4. Analyse the code below. What would you need to add to do this: first create a triangle, then a circle, then another triangle and then another circle.

  This cannot be done

  Write triangle() in line 25 and drawCircle(340,20,40) in line 26

  None of the above

  You would have to create another two functions starting with 'def' and called triangle2 and drawCircle2.

 5. Suppose you created a function as shown in the code below. How would you 'call' (or execute) it?
def first_function():
    print "This line is indented 4 spaces!"

  Functions cannot be 'called' - this doesn’t make any sense

  call it by typing def first_function call

  call it by typing its name and a pair of parentheses (()). e.g. first_function()

  call it by typing call first_function please

 6. One of the great things about functions is that they can…..

  Make the whole program run a lot slower so the code is easier to follow

  make the code more complex so that people are put off trying to copy it

  cause errors and viruses to become inactive or disappear

   repeat complicated instructions without us having to repeat ourselves in code!

 7. In this code, what are the numbers that are being 'passed' to the function on lines 26-29 called?

  functions

  non-numbers

  function-numbers

  parameters

 8. In this code, what is happening on line 15?

  It is causing an error, as it should just say print("Hi there") and not include the word 'say'

  It is calling the function called 'turtle' in order to print a message

  It is calling the function 'say' and passing it the message "HI there"

  It is printing the message "say - Hi there" directly to the screen

 9. Play with the code (and the game) in this trinket. Look at the adventure.py file. How many functions can you see?

  Just two

  None

  More than 20!

  Just one

 10. If you ran the following code, what would be printed to the screen?
def function_one():
   print("a")

def function_two():
   print("g")

function_two()

  The letter 'g'

  The letter 'a'

  Nothing

  function_two()