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

Lecture 7 - Functions

The document discusses functions in Python. It defines what functions are and their purposes, including breaking programs into modular chunks. It covers defining functions using def, passing arguments, return values, and various function types like built-in vs user-defined functions. Examples are provided for defining functions that accept parameters, returning values, and passing lists. Functions allow dividing programs into smaller, organized parts through modularization.

Uploaded by

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

Lecture 7 - Functions

The document discusses functions in Python. It defines what functions are and their purposes, including breaking programs into modular chunks. It covers defining functions using def, passing arguments, return values, and various function types like built-in vs user-defined functions. Examples are provided for defining functions that accept parameters, returning values, and passing lists. Functions allow dividing programs into smaller, organized parts through modularization.

Uploaded by

wsa asdd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

LECTURE 7

• ‘Functions’ in python
• Introduction
• Defining a Function
• Passing Arguments
• Return Values
• Passing a List
FUNCTIONS
Introduction to Function
• Function: group of statements within a program that perform specific task
• Usually one task of a large program
Functions can be executed in order to perform overall program task
• Known as divide and conquer approach
• Modularized program: program wherein each task within the program is in
its own function
Functions help break our program into smaller
and modular chunks. As our program grows
larger and larger, functions make it more
organized and manageable
Functions help break
our program into
smaller and modular
chunks. As our
Blocks of
program grows larger
codes and larger, functions
make it more
organized and
manageable
Types of functions:
Void Functions and Value-Returning Functions
• void function:
• Simply executes the statements it contains and then terminates.
• Executes the lines of code without returning a value
print(), .format()
• value-returning function:
• Designed to return values upon executes
• Executes the statements it contains, and then it returns a value back
to the statement that called it.
• The input(), int(), and float() functions are examples
of value-returning functions.
Built-in vs User-defined Functions

Built-in functions: those provided by the language


- Can implement straight away by calling the function name
- print(), pow(), abs()

User-defined functions: those written by developers/programmers


- We can define our own functions
- Define (create) function that perform certain task, then call the
function to use it in the program
Defining a Function
Using ‘def’
• To create a function, we write its definition. General format (basic syntax) of
a function definition in Python:
Identifie
Function definition r
begins colo
with key word def n
Function body – defines
what the function does
- Each statement in block use
same indentation
Example Defining and calling a Function
These statements
cause myGreetings
function to be created
Example function
without parameter
and no return value

This statement calls


myGreetings
function, causing it to
execute
Passing information to a Function
Adding a parameter student allows the function
to accept any value of student that you specify.

Basically the function now expects you to


provide a value for student each time you call it.
In this case, the student’s value is ‘Jenny’ and
Jenny is known as the argument

TRY THIS!
Write a function called fav_class() that accepts one parameter, course.
The function print a message, such as ‘My favorite class is Python’.
Call the function, making sure that you include the course name in the
function call
parameter

argument
Passing Arguments
Positional Arguments – must follow the order

Multiple
function calls
Passing Multiple Arguments in Multiple functions
Calling another function known as
fav_class with specified arguments
In the main function, it calls another function called
power()

The power function has ‘number’ as the parameter


but we have assigned ‘a’ as the value. Hence, that is
why the output is 25

• Changes made to a parameter value


within the function does not affect the
argument
• Known as pass by value
• Provides a way for unidirectional
communication between one function
and another function
• Global variable: created by assignment statement written outside all the
functions
• A global variable is accessible to all functions in a program file
• If a function needs to assign a value to the global variable, the global variable
must be redeclared within the function
• Global constant: global
name that references a
value that cannot be
changed
• To simulate global constant in
Python, create global variable
and do not re-declare it within
functions
Return Values
A function can also process some data and then return a value or set of
values – it can be an integer, float, string, tuple.
The value the function returns is called a return value

TRY THIS!
Write a program that calculates
and display the area of a circle.
Using two functions, main and
area_circle.
In the area_circle, return the
value back to the main function
HINT: Area of a circle is r2
Returning Multiple Values
Function with Default Values
• Parameters of a function can have default values
• Default values assigned to them when no values are passed as
arguments
• Function definition format with default values:
def functionName(par1=value, par2=value)
def functionName(par1, par2, par3=value, par4=value)
Passing a List
First we define a function students() that
expects a list of name. The function loops
through the list that it receives.

We then define the names using listA and


the pass listA to the students() in the
function call

TRY THIS!
Write a program that display the toppings of your pizza using a list that
contains 3 toppings. For example: ‘Your pizza has the following toppings:’
Using * to pass an arbitrary number as arguments

By doing this you don’t have to define a list


every time

You might also like