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?

  Line 5

  Line 2

  Line 1

  There are no functions in this code

 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(100,0)

  There are no functions in this code

  make_cake(150,0)

  Line 2

 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 lines are naming the program (triangle). The second linei s not needed.

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

  These lines are reminding the program to stop

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

 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.

  None of the above

  This cannot be done

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

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

 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!"

  call it by typing call first_function please

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

  call it by typing def first_function call

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

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

  cause errors and viruses to become inactive or disappear

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

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

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

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

  parameters

  function-numbers

  functions

  non-numbers

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

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

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

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

  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?

  More than 20!

  Just two

  None

  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()

  Nothing

  The letter 'g'

  function_two()

  The letter 'a'