Professional Documents
Culture Documents
Presentation3 v2
Presentation3 v2
BENR 2822
2 BENR
SAA2022
Chapter 3: Functions
• Functions
A function is a named sequence of
statements that belong together.
Their primary purpose is to help us
organize programs into chunks that
match how we think about the problem.
Python provides several built-in
functions such as print(), len() or type(),
but you can also define your own
functions to use within your programs.
SAA2022
Chapter 3: Functions
• Syntax
The basic syntax
for a Python
function definition
is:
SAA2022
Chapter 3: Functions
• Create a Function
To define a Python function, use def keyword. Here’s the simplest
possible function that prints ‘Hello, World!’ on the screen.
SAA2022
Chapter 3: Functions
• Call a Function
The def statement only creates a
function but does not call it.
After the def has run, you can call (run)
the function by adding parentheses after
the function’s name.
SAA2022
Chapter 3: Functions
• Pass Arguments
You can send information to a function
by passing values, known as arguments.
Arguments are declared after the
function name in parentheses.
When you call a function with
arguments, the values of those
arguments are copied to their
corresponding parameters (or
arguments) inside the function.
SAA2022
Chapter 3: Functions
• Pass Arguments
You can send as many arguments
as you like, separated by commas
,.
SAA2022
Chapter 3: Functions
• Types of Arguments
Python handles function arguments in a very flexible manner,
compared to other languages. It supports multiple types of
arguments in the function definition. Here’s the list:
1. Positional Arguments
2. Keyword Arguments
3. Default Arguments
4. Variable Length Positional Arguments (*args)
5. Variable Length Keyword Arguments (**kwargs)
SAA2022
Chapter 3: Functions
1. Positional Arguments
The most common are positional
arguments, whose values are copied
to their corresponding arguments in
order.
The only downside of positional
arguments is that you need to pass
arguments in the order in which they
are defined.
SAA2022
Chapter 3: Functions
• Exercises
a) Write a program to create a function that takes two arguments,
name and age, and print their value.
SAA2022
Chapter 3: Functions It is possible to combine positional and
keyword arguments in a single call. If you do
2. Keyword Arguments so, specify the positional arguments before
keyword arguments
To avoid positional argument def func(age, name, job)
confusion, you can pass arguments
using the names of their
corresponding parameters.
In this case, the order of the
arguments no longer matters
because arguments are matched by
name, not by position.
SAA2022
Chapter 3: Functions
3. Default Arguments Defaults allow you to make selected
arguments optional
You can specify default values for
arguments when defining a function.
The default value is used if the
function is called without a
corresponding argument.
SAA2022
Chapter 3: Functions
• Exercises
b) Write a program to create a function show_employee() using the
following conditions.
It should accept the employee’s name and salary and display both.
If the salary is missing in the function call then assign default value
9000 to salary.
SAA2022
Chapter 3: Functions
• Variable Length Arguments (*args and **kwargs)
Variable length arguments are useful when you want to create
functions that take unlimited number of arguments.
Unlimited in the sense that you do not know beforehand how
many arguments can be passed to your function by the user.
This feature is often referred to as var-args.
SAA2022
Chapter 3: Functions
4. Variable Length Arguments (*args)
When you prefix a parameter with
an asterisk * , it collects all the
unmatched positional arguments
into a tuple.
Because it is a normal tuple object,
you can perform any operation that
a tuple supports, like indexing,
iteration etc.
SAA2022
Chapter 3: Functions
5. Variable Length Keyword Arguments (**kwargs)
The ** syntax is similar, but it
only works for keyword
arguments.
It collects them into a new
dictionary, where the argument
names are the keys, and their
values are the corresponding
dictionary values.
SAA2022
Chapter 3: Functions
• Return Value
To return a value from a function,
simply use a return statement.
Once a return statement is
executed, nothing else in the
function body is executed.
SAA2022
Chapter 3: Functions
• Return Multiple Values
Python has the ability to return
multiple values, something missing
from many other languages.
You can do this by separating return
values with a comma.
When you return multiple values,
Python actually packs them in a
single tuple and returns it. You can
then use multiple assignment to
unpack the parts of the returned
tuple.
SAA2022
Chapter 3: Functions
• Exercises
c) Write a program to create function func1() to accept a variable
length of arguments and print their sum.
d) Write a program to create function calculation() such that it can
accept two variables and calculate addition and subtraction. Also, it
must return both addition and subtraction in a single return
statement.
SAA2022
Chapter 3: Functions
• Composition
One of the most useful features of Python is its ability to take small
building blocks and compose them.
SAA2022
Chapter 3: Functions
• Assigning Functions to Variables
When Python runs a def
statement, it creates a new
function object and assigns
it to the function’s name.
You can assign a different
name to it anytime and call
through the new name.
SAA2022
Chapter 3: Functions
• Python Function Executes at Runtime
Because Python treats def as an
executable statement, it can
appear anywhere a normal
statement can.
For example you can nest a
function inside an if statement
to select between alternative
definitions.
SAA2022
Chapter 3: Functions
• Exercises
e) Create an outer function that will accept two parameters, a and b
• Create an inner function inside an outer function that will calculate the
addition of a and b.
• At last, an outer function will add 5 into addition and return it.
f) Write a program to create a recursive function to calculate the sum of
numbers from 0 to 10.
• A recursive function is a function that calls itself, again and again.
SAA2022
END
OF Congratulations. Here’s a cookie.
CHAPTER 3
SAA2022