Preview

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. 

  value / variable

  variable / argument

  parameter / value

  argument / variable

 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):
    return(x+1)
y = spam(3)       #call it
print(y)          #print it

  1

  spam

  3

  x

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

  3 / 6

  3 / Error

  3 / 123

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

  Hello World

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

  Nothing is being passed

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

 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];
print(changeme(mylist))

  You cannot pass a list as a parameter to a function

  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

  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
   return;

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

  Age, Name

  Age 979

  Methusalah 979

  return

 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):
		fred.forward(distance)
		fred.right(90)
#Running the following code would create a square of size 50 for each side:

square(50)

  square / distance / 50

  fred / distance / square

  turtle / square / 50

  square / 50 / x

 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.

  functonal / local

  functional / global

  global / local

  local /global

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

_______ 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 / error

  name / global / error

  name / local / error

  global / local / welcome

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

rolldice()
#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):
number=random.randint(1,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.

  parameter / number

  parameter / 12

  argument / number

  argument / parameter