0% found this document useful (0 votes)
5 views27 pages

Functions in Python

The document provides an overview of functions in Python, including their advantages, elements, types, and how to define and call them. It discusses built-in, user-defined functions, and modules, along with different types of parameters such as positional, default, and keyword arguments. Additionally, it covers the scope of variables, returning values from functions, and modifying global variables.
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)
5 views27 pages

Functions in Python

The document provides an overview of functions in Python, including their advantages, elements, types, and how to define and call them. It discusses built-in, user-defined functions, and modules, along with different types of parameters such as positional, default, and keyword arguments. Additionally, it covers the scope of variables, returning values from functions, and modifying global variables.
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
You are on page 1/ 27

FUNCTIONS IN PYTHON

A function is a group of related


statements that perform a
specific task.

Advantages:
•Development made easy and fast

•Code sharing becomes possible

•Code re-usability increases

•Program readability increases

•Avoids repetition
ELEMENTS OF A FUNCTION
def a1(x, y): # function header
print( x+y) # function body

a1(5, 6) # function call


a1(2090, 3895) # function call

def: Keyword for defining


sum1: Identifier
x, y: formal parameters, that are used in the
definition of the function
5,6, 2090, 3895 : actual parameters, that will be
acted upon during function execution
TYPES OF FUNCTIONS

¢ Built- in Functions – Pre-defined


functions
¢ Modules – Separate file containing
functions and variables.
¢ User-defined – Function created
and defined by the programmer.
BUILT – IN FUNCTIONS
Eg. Type Conversion
Type
Conversion

Implicit Explicit

Few other built in functions are:


input, eval, abs, type, len, range
functions
A=4+5.8902
MODULES - CODE

OUTPUT: 5.0

Some Examples of Modules are string, math, random


USER-DEFINED FUNCTIONS
¢ Keyword used is: def
USER DEFINED FUNCTIONS

Lines 1-2 are function definition A= int(input())


Line 3 is the function call B=int(input())
x, y – formal parameters Avg_number(A,B)
3, 4 – actual parameters
TYPES OF FORMAL PARAMETERS

¢ Positional arguments
¢ Default arguments

¢ Keyword or named arguments

¢ Positional arguments: In this the number of


formal parameters must match the number of
actual parameters. Eg.
def abc(a,b,c): #function definition
:
:
abc( x,y,z) #function call1
abc( 2, 3,5) #function call2
abc(4, m, n) #function call3
¢ Default Arguments: In this default values are
specified in the function header of the function
definition. eg.
def cylinder(r, h=4, pi=3.14): #function definition
vol=pi*r*r*h
print(vol)

cylinder(2, 3) #function call1, 3rd argument missing


cylinder(3,7, 22/7) #function call2, no argument missing
cylinder(2)
A parameter having a default value in function header
becomes optional in function call. Function may or may
not have value for it.
Imp Note:
In a function header, any parameter cannot be missing
unless all the parameters appearing on its right have
their default values. eg.

def cylinder(pi=3.14, r, h): #invalid

def cylinder(r, pi=3.14, h): #invalid

def cylinder(r, h=5, pi=3.14): #valid


Eg. cylinder(2)

def cylinder(r=6, h=5, pi=3.14): #valid


Eg. cylinder()
¢ Keyword (named) Arguments: In this type of
function definition, we can write argument in any
order provided the arguments are named in
function call. eg
¢ def cylinder(r, h=9, pi=22/7): #function
definition

¢ cylinder(pi=3.14, r=4, h=5) #function call1

¢ cylinder(r= 6, pi=3.14) #function call2

¢ cylinder(pi=3.14, r=8, h= 7) #function call3

Keyword arguments are the named arguments


with assigned values being passed in the function
call statement.
RETURNING VALUES FROM FUNCTION
¢ A function may return any value or an expression. eg

¢ return 6+4

¢ return a**2

¢ return (a+8/3)/b

¢ return (a+b)/c

¢ return(l) # returning a list


SCOPE OF VARIABLES
¢ Global scope: Variables declared at top level of
module or directly in the interpreter
¢ Local scope: Variables declared inside the
function definition or loop

OUTPUT
CODE
from math import pi
print("built in value of pi is:", pi) # pi is a built-in function
# Global Scope
pi = 3.15

def outer():
# Enclosing Scope
pi = 3.14159

def inner():
# Local Scope
pi = 3.141
pi=pi * 10
print("inner:", pi) # This will print 'local pi'

inner()
print("outer:", pi) # This will print 'enclosing pi'

outer()
print("global:", pi) # This will print 'global pi'
QUESTION1
Write a function to create the following figure:
**********
* *
* *
**********
QUESTION 2
Display a sequence using function. Eg.
2,5,8,11,14,17,20 (start is 2, end is 20 and step is 3)
QUESTION 3, 4
Write the output of the following:

Immutable parameter in function


QUESTION 5, 6
Write the output

Mutable parameter in function

Immutable parameter in function


QUESTION 7
Write the output
PYTHON CAN RETURN MULTIPLE VALUES
DIFFERENT WAY OF RETURNING MULTIPLE VALUES
RETURNING DICTIONARY
ACCESSING VALUE OF GLOBAL VARIABLE IN
A FUNCTION
MODIFYING GLOBAL VARIABLE INSIDE A
FUNCTION

int is an immutable type

Global keyword
required for immutable
data type
MODIFYING GLOBAL VARIABLE INSIDE A
FUNCTION

Global keyword not


required for mutable
data type
COMPARE

You might also like