0% found this document useful (0 votes)
2 views26 pages

Chapter 4 - Functions in Python (Lecture Note)

This document outlines Unit 4 of a computer programming course focused on functions in Python. It covers the importance of modularization, the distinction between built-in and user-defined functions, and the scope of variables. Students will learn to define, call, and utilize functions to enhance code reusability and organization.

Uploaded by

mengistut216
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)
2 views26 pages

Chapter 4 - Functions in Python (Lecture Note)

This document outlines Unit 4 of a computer programming course focused on functions in Python. It covers the importance of modularization, the distinction between built-in and user-defined functions, and the scope of variables. Students will learn to define, call, and utilize functions to enhance code reusability and organization.

Uploaded by

mengistut216
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

Fundamentals of Computer Programming

( ITEd1322
(ITEd1322) )

UNIT 4: FUNCTIONS IN PYTHON


Prepared by Course Instructor
Competency-Based Objectives and Learning Outcomes
• The student can modularize Python programs using functions, demonstrating

code reusability and proper variable scope handling.

• Upon successful completion of this unit, students will be able to

» explain the benefits of modularization using functions

» differentiate between built-in and user-defined functions;

» define and call user-defined functions with and without parameters.

» develop simple function-based applications integrating built-in and user-defined

functions

Fundamentals of Computer Programming using Python 4/22/2026 137


Introduction

• In the previous units, programs have largely followed a linear or monolithic

structure, which becomes increasingly difficult to manage, understand, and

maintain as program size and complexity grow.

• Recognizing these limitations helps students build an awareness of the need for

better program organization and design.

• Functions enable programs to be organized into logical and manageable

modules of code.

Fundamentals of Computer Programming using Python 4/22/2026 138


Introduction … (2)
• Brainstorming

Fundamentals of Computer Programming using Python 4/22/2026 139


Overview of Functions

• A function is a set of statements as a block of code that performs a specific

task.

• It is a block of reusable code that performs a particular task.

• Example:

» Given a problem of writing a Python program that calculates the area and

circumference of a rectangle and shading it, you can define three independent yet

related functions that will do these respective actions.


Fundamentals of Computer Programming using Python 4/22/2026 140
Benefits of Functions

• Code reusability

• Modularity

• Improved Readability

• Abstraction

• Maintenance

• Enhanced Collaboration

Fundamentals of Computer Programming using Python 4/22/2026 141


Types of Functions

• Functions can be divided in to TWO broad categories.

A. Built-in Library Functions

o It is standard functions that are made available with the Python interpreter to be imported

and used in our program.

o Such functions are sometimes also referred to as standard library functions.

B. User-Defined Functions

o It is created by you, the programmer.

Fundamentals of Computer Programming using Python 4/22/2026 142


A. Built-in Functions … (1) … Commonly used Built-in Functions
• …

Fundamentals of Computer Programming using Python 4/22/2026 143


A. Built-in Functions … (2) … Commonly used Built-in Functions
• Example 1:

» Write a python program that import built-in functions and compute square root and

power of numbers.

Fundamentals of Computer Programming using Python 4/22/2026 144


A. Built-in Functions … (3) … Commonly used Built-in Functions
• Example 2:

» Write a python program that use calendar built-in function to display days of a

specified year and month programmatically.

Fundamentals of Computer Programming using Python 4/22/2026 145


B. User-Defined Functions
• It is created by the user/programmer from scratch for a particular task.

• Such a function needs to be first defined by specifying a function name

preceded by a def keyword along with optional parameters having one or more
statements as part of the body of the function.

• Syntax of Python function Declaration/Definition

Fundamentals of Computer Programming using Python 4/22/2026 146


B. User-Defined Functions … (2)
• Once a function is defined, it can be called as many times as we want in any part of our
program by specifying the functions name and its arguments or parameters.

• The parameters are used to pass input values for the function.

• It can return a value using the return keyword. If no return is used, the function will return
none by default.

• Functions have different level of complexities.

» Simple Function

» Function with return Type

» Function with Parameters

» Functions Involving Different Scope of Variables such as global and local variables.

Fundamentals of Computer Programming using Python 4/22/2026 147


B. User-Defined Functions … (3) … Simple Function

• This function doesn't have any parameters and doesn't return any values.

• It simply displays the output message.

• Example:

• Now, to use the greet function, we need to call it.

• Example:

Fundamentals of Computer Programming using Python 4/22/2026 148


B. User-Defined Functions … (4) … Simple Function
• The order of the control of the program flows of execution visually, for the

greet () function.

Fundamentals of Computer Programming using Python 4/22/2026 149


B. User-Defined Functions … (5) … Function with Parameters & return value
• Example:

» A function called add, which uses x and y parameters to accept data for the

function. The body of the function computes the sum of the two variables and
returns the sum to the calling function.

Fundamentals of Computer Programming using Python 4/22/2026 150


B. User-Defined Functions … (6) … Function with Parameters & return value
• Activity

Fundamentals of Computer Programming using Python 4/22/2026 151


B. User-Defined Functions … (7) … Function with a Default Parameter

• In Python, we can give a default value to a function parameter.

• If the caller doesn’t provide a value, Python uses the default.

• If the caller provides a value, it overrides the default.

• Example:

» Imagine a company pays a base salary to employees, and an overtime bonus only if

specified. Thus, make the overtime default value 0 so that if the caller doesn’t

mention overtime, it assumes 0, shown on NEXT SLIDE.

Fundamentals of Computer Programming using Python 4/22/2026 152


B. User-Defined Functions … (8) … Function with a Default Parameter
• …

Output:

Fundamentals of Computer Programming using Python 4/22/2026 153


B. User-Defined Functions … (9)
• …

Fundamentals of Computer Programming using Python 4/22/2026 154


Scope of Variables
• When we work with functions, variable visibility and access are dependent on

the region of the program where they are defined, which is often referred to as
the scope of the variable.

• It determines the lifetime (how long the variable exists) and visibility (where it

can be used).

• There are TWO most common scope of variables in function.

A. Local Scopes of Variable

B. Global Scopes of Variable


Fundamentals of Computer Programming using Python 4/22/2026 155
Scope of Variables … (2) … Local Scopes of Variables
• Variables having local scope are created inside a particular function, and they

will be accessible only within that function. Such variables are created when a
function is called and destroyed from memory when it finishes.

• Example:

Fundamentals of Computer Programming using Python 4/22/2026 156


Scope of Variables … (3) … Global Scopes of Variables

• Global variables are declared outside of any function and are accessible

throughout the program. Such a variable lives in memory as long as the

program runs.

• A local variable with the same name as a global variable hides the global one

inside that function.

• To change a global variable inside a function, the global keyword must be used

in the program, shown on NEXT SLIDE.

Fundamentals of Computer Programming using Python 4/22/2026 157


Scope of Variables … (4) … Global Scopes of Variables
• …

Fundamentals of Computer Programming using Python 4/22/2026 158


Scope of Variables … (5)
• Activity

Fundamentals of Computer Programming using Python 4/22/2026 159


Unit Summary
• …

Fundamentals of Computer Programming using Python 4/22/2026 160


Unit Review Questions
• …

Fundamentals of Computer Programming using Python 4/22/2026 161

You might also like