0% found this document useful (0 votes)
39 views

Chapter 8 - Defining Functions

This document discusses functions and program organization in Python. It covers defining functions with parameters, calling functions by passing arguments, scope of variables, and default parameters. Functions allow breaking programs into smaller, reusable pieces of code. Parameters pass data into functions, while arguments are the actual values passed during a function call. Variables inside functions have local scope, while global variables defined outside have broader scope. Default parameters simplify functions by specifying default values for optional arguments.

Uploaded by

mailiemtruc05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Chapter 8 - Defining Functions

This document discusses functions and program organization in Python. It covers defining functions with parameters, calling functions by passing arguments, scope of variables, and default parameters. Functions allow breaking programs into smaller, reusable pieces of code. Parameters pass data into functions, while arguments are the actual values passed during a function call. Variables inside functions have local scope, while global variables defined outside have broader scope. Default parameters simplify functions by specifying default values for optional arguments.

Uploaded by

mailiemtruc05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

HO CHI MINH CITY UNIVERSITY OF TRANSPORT

FACULTY OF INFORMATION TECHNOLOGY


SOFTWARE ENGINEERING DEPARTMENT

CHAPTER 8
Functions And Program Organization
CONTENTS

1. Organizing Program

2. Defining a Function

3. Calling a Function

4. Positional and Keyword Arguments

5. Passing Arguments

6. Scope of Variables

7. Default Parameter

Introduction to CSE 2
Problem
Example 1:
• Suppose that you need to find the sum of integers
• from 1 to 10, 20 to 37, and 35 to 49
• How do you write a program to solve this problem?
Example 2:
• Given two integers n and k. Find the binomial coefficient
𝑛 𝑛!
=
𝑘 𝑘!(𝑛−𝑘)!
• How do you write a program to solve this problem?
Problem: Repeat a particular task many times → very time
consuming and very inefficient.

Introduction to CSE 3
1. Organizing Program

• Our programs are going to start getting bigger and more


complicated.
→ organize them in smaller pieces so they’re easier to write, read
and modify later.
• There are three main ways to break programs into smaller parts:
• Functions - Procedural programming
• Modules - Modular programming
• Objects – Object oriented programming

Introduction to CSE 4
Functions - Procedural programming
• A function is a block of code that performs a specified task.
• A function is a subprogram to build a bigger program.
• This is the solution that give a name to a piece of code.
→Every time that you need it, just call it.
• Benefits of breaking a program into functions:
• Code re-use within a program or across multiple programs
→ Write one time, use many times.
• Smaller, simpler functions are easier to read, understand and
maintain.
• Reduce complexity of the program.
• Hiding implementation details from users.

Introduction to CSE 5
Example

Introduction to CSE 6
2. Defining a Function

• The syntax for defining a function is as follows:

def functionName(list of parameters):


<Function body>

Function name parameter

Function
header

Function
body

Return value

Introduction to CSE 7
Defining a Function

NOTE:
• Data can be passed to functions as parameter
• Parameters are optional, separated by commas. A function may
not have any parameters.
• “return” statement:
return [<expression>]
• Used to return a value.
• <expression>: is optional.
• The function terminates when a return statement is executed.
• Some functions perform desired operations without returning a
value

Introduction to CSE 8
Examples

Introduction to CSE 9
3. Calling a Function

• Defining a new function does not make the function run.


→ To use a function → have to call it.
→ Calling a function executes the code in the function.
• Syntax of a function call:

functionName(list of arguments)

• Arguments: are assigned to the parameters in the function definition


• There are two ways to call a function:
1. If the function returns a value → a call to that function is usually
treated as a value
2. If a function does not return a value → the call to the function
must be a statement.

Introduction to CSE 10
Calling a Function

Introduction to CSE 11
Calling a Function
When a function is called, the program control jumps to that
function definition and executes the statements inside the function
body.
Value -2.7 is passed to val

After executing the body of the function, the program control


jumps back to where the function was called.

Introduction to CSE 12
Functions without Return Values
• Some functions do not return any value.
→ By default, None is returned automatically
→ return statement can be omitted, but it can be used for terminating
the function.

Introduction to CSE 13
Returning Multiple Values

• Syntax:
return val1, val2, val3, ..., valN
• When calling a function returning multiple values, the number of
variables on the left side of = operator must be equal to the
number of values returned.

Introduction to CSE 14
4. Positional and Keyword Arguments
• There are two kinds of
arguments: Positional
• Positional arguments: arguments
must match the
parameters in order,
number, and type.
• Keyword arguments:
passing each argument
in the form
key = value
Keyword
• Note: arguments
Positional arguments must
appear before any keyword
arguments.

Introduction to CSE 15
5. Passing Arguments
• All data are objects in Python, a variable for an object is actually a
reference to the object.
• When you call a function with arguments:
• Value of an argument is passed to a parameter → This mechanism is
known as Pass By Value.
• This value is actually a reference value to the object.
• Note:
• If argument is immutable, the changes made to the parameter will
not affect the argument.
• If the argument is mutable, the changes made to the parameter
variable will affect the argument.

Introduction to CSE 16
Passing Arguments
Call increment()
Id: 8791208661824
x 1

n += 1
Id: 8791208661824
x 1
Id: 8791208661856
n 2

Introduction to CSE 17
Passing Arguments

Introduction to CSE 18
6. Scope of Variables

• The scope of a variable is the part of the program where the


variable can be referenced.
• Local variable: created inside a function
• Can only be accessed within a function.
• Scope: starts from its creation and continues to the end of the
function that contains the variable.
• Global variable: created outside all functions
• Accessible to all functions
• Scope: starts from the point they are defined and continues to the
end of the program

Introduction to CSE 19
Scope of Variables
Example 1: Example 2:

Example 3: Example 4:

Introduction to CSE 20
7. Default Parameter

• Specify a default value for one or more parameters.


• This creates a function that can be called with fewer arguments
than it is defined to allow.
• Note:
• The non-default parameters must be defined before default
parameters.

Introduction to CSE 21
Default Parameter

Introduction to CSE 22

You might also like