You are on page 1of 25

Chapter 5

Functions
Introduction to functions
• A function is a group of statements that exist within a program for the
purpose of performing a specific task.

• Getting the employee’s hourly pay rate


• Getting the number of hours worked
• Calculating the employee’s gross pay
• Calculating overtime pay
• Calculating withholdings for taxes and benefits
• Calculating the net pay
• Printing the paycheck
Subtasks
• Break program into subtasks
• Makes programming more manageable
• Subtasks = A Function
• Divide and Conquer – if you take a large task and break it into smaller
tasks it is easier to complete. Example: Our house situation
Visual
Modularized
• A program that has been written using functions is called
modularized.
Benefits of Modularization
• Simpler Code
• Code Reuse
• Better Testing
• Faster Development
• Easier Facilitation of Teamwork
Types of Functions
• Void Functions – executes statements within and then terminates

• Value-Returning Functions – executes the statements that are within


and then returns a value back to the statement that called it.
Example: input(), float(), int()
Void Function
def message():
print('I am Arthur,')
print('King of the Britons.')
Calling a Function

1 # This program demonstrates a function.


2 # First, we define a function named message.
3 def message():
4 print('I am Arthur,')
5 print('King of the Britons.')
6
7 # Call the message function.
8 message()
1 # This program has two functions. First we
2 # define the main function.
3 def main():
4 print('I have a message for you.')
5 message() 6 print('Goodbye!')
7
8 # Next we define the message function.
9 def message():
10 print('I am Arthur,')
11 print('King of the Britons.')
12
13 # Call the main function.
14 main()
A professional appliance service wants you to write a program to help their
technicians with step by step instructions on how to repair dryers.

STEPS Pseudocode

Turn to page 221 to see code

Acme_dryer.py
Local Variables
• created inside a function
• cannot be accessed by
statements outside the
function
Scope
• The part of the program that can access the variable.
• A variable is visible only to statements in the variable’s scope.
• A variable cannot be accessed by code until the variable has been
created.
Participation Assignment
• Go to page 224 (birds.py)
THINK
• Read over the birds.py program
• On a piece of paper write down the order in which statements are
executed.
PAIR
• Group together and compare your answsers
SHARE
Passing Arguments
• Watch video in the Chapter 5 Moodle Book
Passing Multiple Arguments
Parameters vs Arguments
• Parameters are set up when you create a function.

• Arguments are what you pass into the function when it is called.

• Parameters don’t care what variable name you send in.


• Parameter and Argument names do not have to match.
Keyword Arguments
Global Variables
• A global variable is
accessible to all the
functions in a program.

• These can be changed


throughout the program.

• Do not use these often. Not


good programming.
Global Constants
• A global name that
references a value
that cannot be
changed
Value-Returning Functions

• A function that returns a


value back to the part of
the program that called it.

You might also like