Chapter 8 - Defining Functions
Chapter 8 - Defining Functions
CHAPTER 8
Functions And Program Organization
CONTENTS
1. Organizing Program
2. Defining a Function
3. Calling a Function
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
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
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
functionName(list of arguments)
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
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
Introduction to CSE 19
Scope of Variables
Example 1: Example 2:
Example 3: Example 4:
Introduction to CSE 20
7. Default Parameter
Introduction to CSE 21
Default Parameter
Introduction to CSE 22