Functions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

FUNCTIONS

OBJECTIVES

Create programs that use user-defined


and built-in functions in Python
Introduction
• It is difficult to prepare and maintain a large- scale program and the
identification of the flow of data subsequently becomes harder to
understand.
• The best way of creating a programming application is to divide a big
program into smaller subprograms and repeatedly call these
• Using functions, an entire program can be divided into small,
independent modules.
• This improves the code readability as well as the flow of execution as
small modules can be managed easily
Types of Functions
• Built-in Functions
• Built-in functions are pre-defined functions of Python
programming; you can call them to perform the task. For example:
id(), type(), print(), etc.
• User-Defined Functions
• Users can define their functions based on their requirements.
• For creating a user-defined function, the mandatory keyword is ‘def,’
and the optional keyword is ‘return’.
Return Statement
The return statement is used if you want to return some values from a
function.
The default return value will be None if you are not mentioning the return
statement.
Return Statement (cont…)

Write a program that calculates the addition and


subtraction of two numbers.
Arguments in a Function
• Parameters are used to give inputs to a function.
• They are specified with a pair of parenthesis in the
function’s definition.
• When a programmer calls a function, the values are also
passed to the function.
Arguments in a Function – Position Arguments
• Positional arguments are passed to the function in a proper positional
order.
• The position and the number of the arguments should be compared
• By changing the order, the result may change, whereas by changing the
number of arguments, an error message will be displayed.
Arguments in a Function – Default Arguments
• Parameters within the function’s definition may have default values. You
can assign the default value of a parameter with the help of the
assignment operator.
Arguments in a Function – Variable Length Arguments
• When you pass the distinct number of arguments each time, then they
are referred to as variable length arguments.
• Internally, the defined values are taken in a tuple.
Arguments in a Function – Keywords Arguments
• If the programmer knows the parameter name used within the function
then the parameter name can explicitly be used while calling the
function.
Score of Variables
Python supports the following scope of variables.
• Global Variables - Global variables are defined outside the functions;
thus, they have global scope.
• Local Variables - Variables and parameters that are initialized within the
function, including parameters, are said to exist in that function’s local
scope.
Global Keywords
The global keyword is used:
1. To define the global variable inside the function
2. To declare the global variable available to function for modifications, if
needed.
Recursive Function
Python also supports the recursive feature, which means that a function is
repeatedly calling itself.
Lambda function
• Lambda functions are named after the Greek letter lambda. They only
have a code to execute that is associated with them.
• A lambda function does not contain a return statement.
• It contains a single expression as a body and not a block of statements as a
body
Lambda function (cont…)
Lambda function (cont…)
filter() function
• The filter() function extracts elements from an iterable (list, tuple etc.) for
which a function returns True
• Syantax: filter(function, iterable)
filter() function
Write a program that filters cities with below 25 degrees temperature.

# Abu Dhabi, 35
# Almaty, 22
# Ankara, 32
# Astana, 1
# Bangkok, 30
# Beijing, 28
# New Delhi, 32
# Osaka, 26
# Pyongyang, 23
# Quezon, 33
filter() function
Write a program that filters cities with below 25 degrees temperature.

# Abu Dhabi, 35
# Almaty, 22
# Ankara, 32
# Astana, 1
# Bangkok, 30
# Beijing, 28
# New Delhi, 32
# Osaka, 26
# Pyongyang, 23
# Quezon, 33
filter() function
map() function
map() is a built-in function that allows you to process and transform all the
items in an iterable without using an explicit for loop, a technique
commonly known as mapping.
map() function
Write a program that converts the following Fahrenheit to Celsius.
[70, 80, 88, 78, 90, 78, 66]; using a function and lambda
Formula: (F-32) * 5/9
map() function
Write a program that converts the following Fahrenheit to Celsius.
Manila 90, Tokyo 77, Seoul 79, Bangkok 87, Jakarta 90
Nested functions
• The functions declared within the scope of another function are called
nested functions.
Generator Functions
• An important class of functions is the generators, which simplifies the
job of writing the iterators.
• It is a routine that is used to manage the iteration behavior of a loop.
Next: Object-oriented programming

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy