17 - Parameter Passing

 1. Fill in the blanks below. (basic definitions)
A Parameter is a special name given to a ___________ that is passed into a 
sub function. It is worth noting that both procedures and functions 
can take in parameters.
An __________ is the name given to the actual values when the sub 
program is called. 

  variable / argument

  argument / variable

  value / variable

  parameter / value

 2. A function is like a mini program within your program. In the example we define a function (spam) and it takes an argument, ___ in the example and then assigns that to a variable and then prints it
def spam(x):
y = spam(3)       #call it
print(y)          #print it





 3. This function has ____ positional parameters. The output when called on line 3 is_____
def foo(val1, val2, val3):
    return val1 + val2 + val3

  3 / 123

  3 / 6

  3 / Error

  0 / 3

 4. In the code below, what is being passed to the printme() function, and what is the output?
def printme( str ):
   "This prints a passed string into this function"
   print str

#Call the printme function
printme("Hello world")

  'str' is being passed, so 'str' will be printed

  This will cause an error - a parameter cannot be a string

  Hello World

  Nothing is being passed

 5. Analyse the code carefully and select an answer. What is the output here? Note that line 8 is printing (and calling) the changeme function, and passing it the list: mylist
# Function definition is here
def changeme( mylist ):
   "This changes a passed list into this function"
   return 1

# Now you can call changeme function
mylist = [10,20,30];

  10,20,30, because the list passed in is the list printed out and returned

  1, because the list is being passed, but the function simply returns 1

  You cannot pass a list as a parameter to a function

  1, because the list is not actually being passed into the function

 6. What is the output of the following?
def printinfo( name, age ):
   "This prints passed info into this function"
   print "Age ", age

# Now you can call printinfo function
printinfo( age=979, name="Methusalah" )

  Age, Name

  Age 979


  Methusalah 979

 7. The following example creates a procedure called '_______'. ________ is a parameter - it allows us to pass a value into the procedure for it to use. The ____ in the example is called the argument - because it is a value passed in.
#The pseudocode is included as a comment after the #.

import turtle #import the turtle graphics module
window = turtle.Screen() # make a graphics window called "window"
fred = turtle.Turtle() #make a turtle called "fred"
def square (distance):
	for x in range (0,4):
#Running the following code would create a square of size 50 for each side:


  square / distance / 50

  turtle / square / 50

  square / 50 / x

  fred / distance / square

 8. Scope refers to where a function, procedure, variable or constant can be used. They can be local or global. Fill in the blanks below
A ________ variable is declared in a way that makes it available 
to every other part of the program, whereas a ________ variable is
declared in a way that makes it only available within a specific function.
A module-scoped variable is only available with a single file or module.

  global / local

  functional / global

  local /global

  functonal / local

 9. This program asks for a name, and then prints that name out three times. Fill in the blanks below.
name = input("Please enter your name")
def greet():
    for x in range (0, 3):
    print (x, name)

_______ is a variable that is global in its scope
the x variable in the loop for x in range... is ______ to the greet function.
If you tried to access x from outside this function, you would 
get an ______ message as it only exists inside greet
if the same code above was in a module file, then name would be module-scoped

  global / local / welcome

  global / local / error

  name / local / error

  name / global / error

 10. Go through the example provided below and fill in the blanks below.
import random
def rolldice():
    print (number)
To roll the dice, you would need to call the functions by typing:

#If you wanted to have the option of rolling a dice with a 
#different number of sides, you could make these changes:

import random	
def rolldice(sides):
print (number)
Then call the procedure rolldice(12). 
#The word "sides" is known as a _____________ 
#The number ____ in the brackets is known as the argument.

  argument / number

  argument / parameter

  parameter / number

  parameter / 12