Preview

16 - Functions and Procedures

 1. Sub programs can be used to save time and simplify code. The two main types of sub programs are ________________________________

  parts and classes

  modules and definitions

  procedures and functions

  defs and parts

 2. The main difference between a procedure and a function (and this is for computing theory, so ignore any python syntax and specificity) is that....

  a function never returns a value to the main program

  a function always returns at least two different values

  a function always returns a single value to the main program.

  a function can perform many different functions and output the results

 3. What is an argument?

  An argument has nothing to do with computer science whatsoever

  An argument is a fight that occurs between two functions over a value

  An argument is a value that is passed into a function or procedure

  An argument is a type of procedure

 4. The following pseudo code converts an exam result to a grade using the given boundaries. Is the example shown below a procedure or function?
Function GradeCalculator(score As Integer) 
STRING Grade
If Score>=90 THEN
	Grade = A*
If Score>=80 THEN
	Grade = A
If Score>=70 THEN
	Grade = B
If Score>=60 THEN
	Grade = C
Else
	Grade = Fail
Return (Grade)
End Function

  Function because it returns, in the end, a single value 'Grade'

  Neither

  Actually, the whole thing is called an 'argument' because it has many functions

  Procedure because it has several functions inside of it

 5. What will the output be if the input is '10'? #Extension: Try and code another function called 'multiply' which takes the input given by the user and multiplies it by itself.

  12

  4

  10

  2

 6. Consider the input and output for the following function print(fact(2))>> 2 print(fact(3))>> 6 What would the output be if called with '4' as shown below.
def fact(x):
    if x < 2:
        return 1
    return x * fact(x - 1)

print(fact(4))

  10

  24

  12

  8

 7. In many languages, a function that does not return a value is called a ________________ (but do see the note below)
>>It is worth noting in python that everything defined with a 'def' is 
called a function, and sometimes functions which don't return a value are
referred to as non-fruitful functions.
Source: http://interactivepython.org

  parameter

  boolean

  argument

  procedure

 8. Which of the examples below is a procedure?
#Example 1

def square(x):
    y = x * x
    return y

toSquare = 10
result = square(toSquare)
print "The result of " + str(toSquare) + " squared is " + str(result)


#Example 2
def update_display():
    print("Your score: " + str(score))
	time.sleep(1)
	print("High score: " + str(high_score))
	time.sleep(1)
	print("Lives remaining: " + str(lives))
	time.sleep(1)

  Both examples are functions (they return a single value)

  Example 2

  Both examples are procedures (they perform multiple actions with multiple outputs)

  Example 1

 9. What is wrong with the following function definition?
def addEm(x, y, z):
    return x+y+z
    print 'the answer is', x+y+z

  Both B and C are correct in this instance

  B - You should not have any statements in a function after the return statement.

  A - You should never use a print statement in a function definition

  C - When the function gets to "return", it will immediately stop executing the function

 10. What will the following code output?
def square(x):
    y = x * x
    return y

print square(5) + square(5)

  25+25

  25

  Error

  50