Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views70 pages

3 Functions Part 1

The document provides an overview of functions in programming, emphasizing their importance in creating modularized programs that simplify code, enhance code reuse, and facilitate testing. It covers concepts such as defining and calling functions, local variables, and the benefits of using functions in programming. Additionally, it discusses void functions, value-returning functions, and techniques for designing programs that utilize functions effectively.

Uploaded by

karencouzon04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views70 pages

3 Functions Part 1

The document provides an overview of functions in programming, emphasizing their importance in creating modularized programs that simplify code, enhance code reuse, and facilitate testing. It covers concepts such as defining and calling functions, local variables, and the benefits of using functions in programming. Additionally, it discusses void functions, value-returning functions, and techniques for designing programs that utilize functions effectively.

Uploaded by

karencouzon04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Manuel S.

Enverga University Foundation, Lucena City College of Engineering


An Autonomous University

Lesson:
Functions
Computer Fundamentals and Programming 2

HANNAH SHAMIRA P. SANTONIL


Instructor, College of Engineering
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Learning outcomes:
1. Introduction to Functions
2. Defining and Calling a Void Function
3. Designing a Program to Use Functions
4. Local Variables
5. Passing Arguments to Functions
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Introduction to
Functions
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Introduction to Functions
Concept:
A function is a group of statements that
exist within a program for the purpose of
performing a specific task.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Introduction to Functions
• Most programs perform tasks that are large
enough to be broken down into several subtasks.
• For this reason, programmers usually break
down their programs into small manageable
pieces known as functions.
• A function is a group of statements that exist
within a program for the purpose of performing a
specific task.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Introduction to Functions
• Instead of writing a large program as one long
sequence of statements, it can be written as several
small functions, each one performing a specific part
of the task.
• These small functions can then be executed in the
desired order to perform the overall task.
• This approach is sometimes called divide and
conquer because a large task is divided into several
smaller tasks that are easily performed.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Introduction to Functions
• A program that
has been written
with each task in
its own function is
called a
modularized
program.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Benefits of Modularizing a Program


with Functions
Simpler Code
• A program’s code tends to be simpler
and easier to understand when it is
broken down into functions.
• Several small functions are much easier
to read than one long sequence of
statements.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Benefits of Modularizing a Program


with Functions
Code Reuse
• Functions also reduce the duplication of code within
a program. If a specific operation is performed in
several places in a program, a function can be
written once to perform that operation, then be
executed any time it is needed.
• This benefit of using functions is known as code
reuse because you are writing the code to perform a
task once, then reusing it each time you need to
perform the task.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Benefits of Modularizing a Program


with Functions
Better Testing
• When each task within a program is contained
in its own function, testing and debugging
becomes simpler.
• Programmers can test each function in a
program individually, to determine whether it
correctly performs its operation. This makes it
easier to isolate and fix errors.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Benefits of Modularizing a Program


with Functions
Faster Development
• Suppose a programmer or a team of programmers is
developing multiple programs. They discover that each
of the programs perform several common tasks, such
as asking for a username and a password, displaying
the current time, and so on. It doesn’t make sense to
write the code for these tasks multiple times.
• Instead, functions can be written for the commonly
needed tasks, and those functions can be incorporated
into each program that needs them.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Benefits of Modularizing a Program


with Functions
Easier Facilitation of Teamwork
• Functions also make it easier for
programmers to work in teams.
• When a program is developed as a set of
functions that each performs an individual
task, then different programmers can be
assigned the job of writing different
functions.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Void Functions and Value-Returning


Functions
• When you call a void function, it simply
executes the statements it contains and
then terminates.
• When you call a value-returning
function, it executes the statements that
it contains, then returns a value back to
the statement that called it.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Void Functions and Value-Returning


Functions
• The input function is an example of a
value-returning function.
• When you call the input function, it gets
the data that the user types on the
keyboard and returns that data as a
string.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Void Functions and Value-Returning


Functions
• The input function is an example of
a value-returning function.
• When you call the input function, it
gets the data that the user types on the
keyboard and returns that data as a
string.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Void Functions and Value-Returning


Functions
• The int and float functions are also
examples of value-returning functions.
• You pass an argument to the int function,
and it returns that argument’s value converted to
an integer.
• Likewise, you pass an argument to the float
function, and it returns that argument’s value
converted to a floating-point number.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Defining and
Calling a Void
Function
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Defining and Calling a Void Function


CONCEPT:
• The code for a function is known as a
function definition. To execute the
function, you write a statement that calls
it.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Function Names
• Before we discuss the process of creating and
using functions, we should mention a few things
about function names.
• Just as you name the variables that you use in a
program, you also name the functions.
• A function’s name should be descriptive enough so
anyone reading your code can reasonably guess
what the function does.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Function Names
• You cannot use one of Python’s key words as a
function name. (See Table 1-2 for a list of the key
words.)
• A function name cannot contain spaces.
• The first character must be one of the letters a through
z, A through Z, or an underscore character (_).
• After the first character you may use the letters a
through z or A through Z, the digits 0 through 9, or
underscores.
• Uppercase and lowercase characters are distinct.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Defining a Function
• To create a function, you write its definition.
• Here is the general format of a function definition
in Python:
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Defining a Function
• The first line is known as the function header. It
marks the beginning of the function definition.
• The function header begins with the key word
def, followed by the name of the function,
followed by a set of parentheses, followed by a
colon.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Defining a Function
• Beginning at the next line is a set of statements
known as a block.
• A block is simply a set of statements that belong
together as a group. These statements are
performed any time the function is executed.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Defining a Function
• Notice in the general format that all of the
statements in the block are indented. This
indentation is required, because the Python
interpreter uses it to tell where the block begins
and ends.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Defining a Function
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Calling a Function
• A function definition specifies what a function
does, but it does not cause the function to
execute.
• To execute a function, you must call it. This is how
we would call the message function:
message()
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Calling a Function
• When a function is called, the interpreter jumps to
that function and executes the statements in its
block.
• Then, when the end of the block is reached, the
interpreter jumps back to the part of the program
that called the function, and the program resumes
execution at that point.
• When this happens, we say that the function
returns.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Indentation in Python
• In Python, each line in a block must be indented.
As shown in Figure 5-7, the last indented line after
a function header is the last line in the function’s
block.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Designing a
Program to Use
Functions
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Designing a Program to Use Functions


CONCEPT:
• Programmers commonly use a technique known
as top-down design to break down an algorithm
into functions.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Flowcharting a Program with Functions


Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Hierarchy Charts
• Programmers commonly use hierarchy charts for
this purpose. A hierarchy chart, which is also
known as a structure chart, shows boxes that
represent each function in a program.
• The boxes are connected in a way that illustrates
the functions called by each function. Figure 5-10
shows an example of a hierarchy chart for a
hypothetical pay calculating program.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Hierarchy Charts
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Example 1 - Functions
• Professional Appliance Service, Inc. offers maintenance and
repair services for household appliances. The owner wants
to give each of the company’s service technicians a small
handheld computer that displays step-by-step instructions
for many of the repairs that they perform. To see how this
might work, the owner has asked you to develop a
program that displays the following instructions for
disassembling an Acme laundry dryer:
Step 1: Unplug the dryer and move it away from the wall.
Step 2: Remove the six screws from the back of the dryer.
Step 3: Remove the dryer’s back panel.
Step 4: Pull the top of the dryer straight up.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Example 1 - Functions
• During your interview with the owner, you determine
that the program should display the steps one at a
time. You decide that after each step is displayed, the
user will be asked to press the Enter key to see the
next step. Here is the algorithm in pseudocode:
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Defining and Calling Functions


Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Example 1 - Functions
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Example 1 - Functions
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Pausing Execution Until the User


Presses Enter
• Sometimes you want a program to pause so the user can read
information that has been displayed on the screen. When the
user is ready for the program to continue execution, he or she
presses the Enter key and the program resumes.
• In Python, you can use the input function to cause a program
to pause until the user presses the Enter key. Line 7 in Program 5-
3 is an example:
input('Press Enter to see Step 1.')
• This statement displays the prompt 'Press Enter to see Step 1.'
and pauses until the user presses the Enter key.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Local Variables
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Local Variables
CONCEPT:
• A local variable is created inside a function and
cannot be accessed by statements that are
outside the function.
• Different functions can have local variables with
the same names because the functions cannot
see each other's local variables.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Local Variables
• Anytime you assign a value to a variable inside a
function, you create a local variable.
• A local variable belongs to the function in which it
is created, and only statements inside that
function can access the variable.
(The term local is meant to indicate that the variable
can be used only locally, within the function in which
it is created.)
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Local Variables
• An error will occur if a statement in one function tries to access a
local variable that belongs to another function. For example, look
at Program 5-4.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Local Variables

• This program has two functions: main and get_name. In line 8, the
name variable is assigned a value that is entered by the user. This
statement is inside the get_name function, so the name
variable is local to that function. This means that the name variable
cannot be accessed by statements outside the get_name
function.
• The main function calls the get_name function in line 3. Then,
the statement in line 4 tries to access the name variable. This
results in an error because the name variable is local to the
get_name function, and statements in the main function
cannot access it.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Scope and Local Variables


• A variable’s scope is the part of a program in
which the variable may be accessed.
• A variable is visible only to statements in the
variable’s scope.
• A local variable’s scope is the function in which
the variable is created.
• As you saw demonstrated in Program 5-4, no
statement outside the function may access the
variable.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Example 2:
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Passing
Arguments to
Functions
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Passing Arguments to Functions


Concept:
• An argument is any piece of data that is passed
into a function when the function is called.
• A parameter is a variable that receives an
argument that is passed into a function.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Passing Arguments to Functions


• Sometimes it is useful not only to call a function, but
also to send one or more pieces of data into the
function.
• Pieces of data that are sent into a function are known
as arguments.
• The function can use its arguments in calculations or
other operations.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Passing Arguments to Functions


• If you want a function to receive arguments when it is
called, you must equip the function with one or more
parameter variables.
• A parameter variable, often simply called a parameter,
is a special variable that is assigned the value of an
argument when a function is called. Here is an example
of a function that has a parameter variable:
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Passing Arguments to Functions


• This function’s name is
show_double. Its purpose is
to accept a number as an
argument and display the
value of that number
doubled. Look at the function
header and notice the word
number that appear inside
the parentheses.
• This is the name of a
parameter variable. This
variable will be assigned the
value of an argument when
the function is called.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Passing Arguments to Functions


Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Parameter Variable Scope


• You learned that a variable’s scope is the part of the
program in which the variable may be accessed.
• A variable is visible only to statements inside the
variable’s scope.
• A parameter variable’s scope is the function in which
the parameter is used.
• All of the statements inside the function can access the
parameter variable, but no statement outside the
function can access it.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Example: Passing an Argument to a Function


• Your friend Michael runs a catering company. Some of
the ingredients that his recipes require are measured in
cups. When he goes to the grocery store to buy those
ingredients, however, they are sold only by the fluid
ounce. He has asked you to write a simple program
that converts cups to fluid ounces.
• You design the following algorithm:
1. Display an introductory screen that explains what the
program does.
2. Get the number of cups.
3. Convert the number of cups to fluid ounces and display the
result.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Example: Passing an Argument to a Function


• This algorithm lists the top level of tasks that the
program needs to perform and becomes the basis of
the program’s main function. Figure 5-15 shows the
program’s structure in a hierarchy chart.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Example: Passing an Argument to a Function


Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Passing Multiple Arguments


• Often it’s useful to write functions that can accept
multiple arguments. Program 5-8 shows a function
named show_sum, that accepts two arguments.
• The function adds the two arguments and displays
their sum.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Passing Multiple Arguments


Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Passing Multiple Arguments


Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Making Changes to Parameters


• When an argument is passed to a function in Python, the function
parameter variable will reference the argument’s value. However,
any changes that are made to the parameter variable will not
affect the argument. To demonstrate this, look at Program 5-10.
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Making Changes to Parameters


Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Keyword Arguments
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

Keyword Arguments
Manuel S. Enverga University Foundation, Lucena City College of Engineering
An Autonomous University

End of Lesson

You might also like