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 2

  Line 1

  There are no functions in this code

  Line 5

 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)

  make_cake(150,0)

  Line 2

  There are no functions in this code

 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.

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

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

  These lines are reminding the program to stop

 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

  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

  None of the above

 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 call first_function please

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

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

  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

  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?

  non-numbers

  parameters

  functions

  function-numbers

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

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

  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"

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

  None

  Just one

  More than 20!

  Just two

 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'

  The letter 'a'

  function_two()