Preview

03 - Creating Functions

 1. In general programming, you will come across the term 'subroutine'. What are subroutines?
Note: Watch the following video from www.teachyourselfpython.com for an overview of functions and parameter passing

  Subroutines may also be referred to as a function, procedure, or subprogram

  It is a portion of code that may be called and executed anywhere in a program and used repeatedly

  All of the above are valid definitions / examples of subroutines

  They are made up of a sequence of instructions that can receive data, process that data, and return a value.

 2. In different languages, this may mean something different, but typically a function is different from a procedure in that it…

  returns a single value (a procedure performs operations but does not necessarily return a value)

  always returns the number 1

  returns a function

  returns several values

 3. In Python, functions are typically created using the …

  import command (e.g. import main():)

  None of the above

  def command (e.g. def main():)

  system command

 4. In order to print the statement inside the function partone(): what needs to be done?
def mainmenu():
  print("===Welcome to the Main Menu====")
  

def partone():
  print("===This is part one=====")

mainmenu()

  Replace line 8 with print("def part one()"

  Replace line 8 with "part1"

  Replace line 8 with partone()

  This cannot be done as the main menu must be called first

 5. What is the output of the following program?
def one():
  print(1)


def two():
  print(2)

one()

  1,2

  print(one)

  1

  one,two

 6. Look carefully at the following program. What will happen when you run the program?
def one():
  print(1)
  two()


def two():
  print(2)
  three()

one()

  The program will only print 1

  None of the above

  The program will crash because the function 'three' is being called and does not exist

  The program will print 1,2,3

 7. How many functions can you spot in the following program?
import time

def bank():
  print("Welcome to your bank!")
  print("Verifying account number:>>")
  time.sleep(1.4)
  print(".................")
  print("Verifying password:>>")
  time.sleep(1.4)
  print(".....Logging you in to your account")
  time.sleep(1.4)
  print("===Please Wait===")
  time.sleep(1.4)
  print(".................")
  account()

def account():
  print("========WELCOME TO YOUR ACCOUNT======")
  password=input("Enter secure password:")
  denied()


bank()

  One function - time.

  Two functions - bank and account

  Three functions - def, time and sleep

  Five functions, bank, time, sleep, accout and def

 8. This code will not run as there is something missing in the function definition. What is it?
def main()
  print("This is the main function"

def function1():
print("This is function1")
  

def function2:
  print("This is function2")
  
main()

  A colon after main() at the very bottom of the program

  A double brackets after main()

  A colon after def main()

  None of the above

 9. Describe what will happen when this program is run?
import time

def main():
  print("---LATEST PHONES.COM----")
  time.sleep(1.3)
  print("printing list of top phones...please wait.....")
  topthree()
  
def topthree():
  print("Top Three phones are:Samsung, Nokia and HTC")
  #add the top three, in your opinion, here
  time.sleep(1.3)
  print("...now we are going to list the worst phone ever...please wait")
  worst()

main()

  It will not run and cause an error, as there are no functions defined

  It will print the top three phones and the statement leading up to printing the worst phones and then stop as there is no 'worst' function

  None of the above

  It will print the top three phones and the three worst phones before exiting the program

 10. Parameters can be passed to both procedures and functions. Name the parameters used in the code below.
def main():
  x=2
  y=3
  sum(x,y)

def sum(x,y):
  print(x+y)


main()

  x

  x and y

  def and print

  main and sum

 11. Which of the following statements accurately describes what will happen when this program is run?
def main():
  num1=7
  num2=2
  sum(x,y)

def sum(x,y):
  print(x+y)
  
main()

  It will fail to run. You are calling the function 'sum' with parameters x and y, but they don't exist

  The program will execute and print the letters x + y

  The proram will print '9' twice.

  The program will execute and print the number 9 (adding parameters x + y)

 12. Functions will usually integrate blocks of code and the advantage of using functions is ….

  reusability

  fast execution

  prevention of misuse

  automated validation

 13. What is the parameter in the following program and what will be output?
def main():
  username="user1"
  welcome(username)

def welcome(username):
  print(username)
  
main()

  There are no parameters here -parameters can only be numbers. The code will fail

  An error will occur as the parameter 'username' has been used, and this is not valid

  The parameter is 'username' and 'user1' will be printed to the screen

  The parameter is 'user1' and 'username' will be printed to the screen

 14. Analyse the code carefully and predict what the output will be.
def one():
  x=1
  print(x)
  two()


def two():
  x=2
  print(x)
  three()

def three():
  x=3
  print(x)
  add(x)
  
def add(x):
  print(x)
  
one()

  1,2,3,6

  1,2,3,3

  Error - the code will fail to run as the same parameter x has been used repatedly

  1,2,3

 15. Describe what is happening in this program?
value_of_goods=10

def vat(value_of_goods):
  return value_of_goods * 0.2

print(vat(value_of_goods))

  An error occurs as you cannot print out the result of a function as has been done on line 6

  None of the above

  The function vat is called with the parameter '10' and 10 is printed at program line 6

  The function vat multiples the parameter 'value_of_goods' by 0.2 and returns 2.0 (the value of the function is printed)