Preview

03 - Functions and Parameter Passing

 1. In python subroutines are often referred to as 'functions' (although in other languages a function only returns a single value). How many functions can you spot in the code below?
def main():
	print("Hi there")
	login()

def login():
	print("---Login---")
	register()
def register():
	print("----Register---")
main()						

  Four

  One

  Three

  Two

 2. All of the following statements are true: 1.Functions/Subroutines make programs more readable. 2. They reduce the duplication of code. 3. Complex problems are broken down into smaller chunks.

  True

  False

 3. Which key word is used for the creation of functions/subroutines in Python?

  create

  def

  func

  main

 4. What is the output of the code below?
def subroutine1():
	print("x")
	subroutine3()

def subroutine2():
	subroutine4()

def subroutine3():
	print("z")

def subroutine4():
	print("a")

subroutine2()

  a

  z

  y

  x

 5. On what line(s) can you spot parameter passing? (Note: a parameter is also called an 'argument' but can just be thought of as the variables that are being passed)
def printMax(a, b):
    if a > b:
        print(a, 'is maximum')
    elif a == b:
        print(a, 'is equal to', b)
    else:
        print(b, 'is maximum')
		
printMax(3, 4)

  Line 1 and Line 9

  Line 3 and Line 4

  No parameter passing is occuring in this code

  Line 1 and Line 7

 6. In the following program '3' and '4' are the parameters that have been passed to the subroutine printMax. What is the output of the following program?
def printMax(a, b):
    if a > b:
        print(a, 'is greater')
    elif a == b:
        print(a, 'is equal to', b)
    else:
        print(b, 'is greater')
printMax(3, 4)

  4 is greater

  a is equal to b

  b is greater

  3 is greater

 7. Variables can either be inside subs/functions or outside them. If the variables are inside, they are referred to as being 'local variables' or having 'local scope'. Variables that are declared outside are called G_________ variables.
x = 50
def func(x):
    print('x is', x)
    x = 2
    print('Changed local x to', x)
func(x)
print('x is now', x)

  Glocular

  Globulous

  Gloopy

  Global

 8. Type out the code for yourself. The output is "Value of x is, 2"
x = 50
def func():
    global x
    print('x is', x)
    x = 2
    print('Changed global x to', x)
func()
print('Value of x is', x)

  False

  True

 9. What is the output of the following program - it has been passed the parameters 2 and 3. Note the use of the RETURN statement that is commonly used in functions to return a value
def maximum(x, y):
    if x > y:
        return x
    elif x == y:
        return 'The numbers are equal'
    else:
        return y
 
print(maximum(2, 3))

  3

  Y

  X

  2

 10. For the purposes of learning theory, it is useful to remember that a FUNCTION returns a single value, where as a subroutine(subroutines are also called either procedures or methods) does not necessarily just return a single value. Correctly identify Test1
location=5
def Test1(location):
	print("All is okay at location:",location)
	print("Checking .....")
	print("It is safe to move on to the next location,")
	print("Location",location,"has been checked")
	

def Test2(location):
	if location>2:
		return True
	else:
		return False

	

  Test1 is a Function as it returns a single value, while Test2 is a subroutine/Procedure/Method

  Test2 is not a Function because it contains two values True and False

  Test1 is a Function because it contains only one value 'location'

  Test1 is a subroutine/procedure/method while Test2 returns a single value so is a Function