0% found this document useful (0 votes)
12 views12 pages

Python Functions in Procedural Programming

The document provides an overview of functions in Python, detailing their role in procedural oriented programming (POP) and highlighting their advantages such as reusability, readability, modularity, and efficiency. It explains the types of functions, including predefined and user-defined functions, as well as the concept of parameters, local and global variables, and the use of the 'global' keyword. Additionally, it covers function definitions, invoking functions, and various parameter types including required, default, and variable-length parameters.

Uploaded by

rutikhonde274
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

Python Functions in Procedural Programming

The document provides an overview of functions in Python, detailing their role in procedural oriented programming (POP) and highlighting their advantages such as reusability, readability, modularity, and efficiency. It explains the types of functions, including predefined and user-defined functions, as well as the concept of parameters, local and global variables, and the use of the 'global' keyword. Additionally, it covers function definitions, invoking functions, and various parameter types including required, default, and variable-length parameters.

Uploaded by

rutikhonde274
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Functions

===================================================================================
==
Python if multiparadigm programming language. It allows to write programs using
different programming paradigms.
1. Procedural Oriented Programming (POP)
2. Modular Oriented Programming (MOP)
3. Object Oriented Programming (OOP)
===================================================================================
==
Procedural Oriented Programming (POP)
===================================================================================
==
In procedural oriented programming instructions are organizined by dividing into
small pieces called subroutains. A small program within program is called
subroutain.
These subroutains can be functions or procedures.
===================================================================================
==
Advantage of POP
===================================================================================
==
1. Reusability: Allows to write code once use many times
2. Readability: Easy to understand
3. Modularity: divide code according their operations into small programs
4. Efficiency: Increase efficiency of program
===================================================================================
==
In python functions are building blocks of procedrual oriented programming
===================================================================================
==
What is function?
A function is small program within program
A function is named block with contains set of instructions to perform specific
operation.

Advantanges of functions
========================
1. reusability
2. readability
3. modularity
4. efficiency
===================================================================================
==
Basic steps for developing functions
===============================================================================
1. Writing function/Defining function
2. calling function/invoking function
===============================================================================
functions are 2 types
1. predefined function
2. user defined function

predefined functions are nothing but existing functions.


example: print(),input(),len(),max(),sum(),....
the functions build by programmer are called user defined functions, these are
application specific.
example: deposit(),withdarw(),login(),register()
==============================================================================
Defining function or Writing function
===============================================================================
python provides a keyword called "def" for defining or writing function.

syntax:
=======
def function-name([parameters]):
statement-1
statement-2

block should not be empty.


a function is defined,
1. with parameters
2. without parameters
=============================================================================
function without parameters
===============================================================================
a function without parameters does not receives any values from caller
it performs operation with receiving input.

def function_name():
statement-1
statement-2

===============================================================================
functions memory is allocated, when function is invoked and de-allocaed after
execution of function.
whenever function is called, execution control switched from calling place to
called function and after execution returns to calling place.
===================================================================================
=
Example
=======
def sayhello():
print("Welcome to functions")

sayhello()
sayhello()

Output
=======
Welcome to functions
Welcome to functions
===================================================================================
==

def draw_line():
for i in range(20):
print("*",end='')
print()

#main
draw_line()
print("PYTHON")
draw_line()
print("PROGRAMMING")
draw_line()
===================================================================================
=
local variables
==============================================================================
variable created inside function is called local variable
the scope of local variable is within function but cannot accessible outside the
function. the memory for local variables are created when function is called and
deleted after execution of function.

==============================================================================
def fun1():
a=100 # local variable

def fun2():
print(a)

fun1()
fun2()
===================================================================================
=
def fun1():
a=100 # local variable
print(a)

def fun2():
a=200 # local variable
print(a)

fun1()
fun2()
===================================================================================
global variables
===================================================================================
The variables created outside function are called global variables.
The scope of these variables within program and outside the program
In application development global variables are created to share data between
number of functions
===================================================================================
=
n1=10 # global variable
n2=5 # global variable
def add():
print(f'sum is {n1+n2}')
def sub():
print(f'diff is {n1-n2}')
def multiply():
print(f'product is {n1*n2}')
def div():
print(f'result is {n1/n2}')

add()
sub()
multiply()
div()
===================================================================================
=
a function can access global variable directly but cannot modify value directly.
===================================================================================
=
a=100
def fun1():
print(a)

def fun2():
a=300 #local variable
print(a)

fun1()
fun2()
fun1()
===================================================================================
=
global keyword
===================================================================================
=
inorder to modify global variable within function, a function uses global keyword.

global variable-name,variable-name

after this statement, listed variable names are referred as global variables
===================================================================================
==
a=100
def fun1():
print(a)

def fun2():
global a
a=300
print(a)

fun1()
fun2()
fun1()
==================================================================================
base=0.0 # Global Variable
height=0.0 # Global Variable

def read():
global base,height
base=float(input("Base :"))
height=float(input("Height :"))

def find_area():
area=0.5*base*height
print(f'Area is {area}')

read()
find_area()
===================================================================================
=
function with parameters
===================================================================================
==
what is parameter?
parameter is a local variable which receives values from caller.
a function is defined with different types of parameters or arguments.

1. function with required parameters or arguments


a. function with required positional only arguments
b. function with required keyword only arguments
2. function with default parameters or arguments
3. function with variable length parameters or arguments
a. function with positional variable length parameters or arguments
b. function with keyword variable length parameters or arguments
===================================================================================
=
function with required parameters or arguments
===================================================================================
==
function with required parameters or arguments required values at the time of
invoking or calling the function.

def add(a,b):
c=a+b
print(a,b,c)

add(10,20) # positional
add(a=10,b=20) # keyword
add(b=100,a=300)# keword

function with required parameters receives values using parameter positions or


parameter names
===================================================================================
=
return keyword
===========================================================================
a function returns the value to caller using return keyword
after returning value, it terminates execution of function

def max2(a,b):
if a>b:
return a
else:
return b

res1=max2(10,20)
print(f'maximum is {res1}')
res2=max2(200,100)
print(f'maximum is {res2}')
res3=max2(b=2,a=1)
print(f'maximum is {res3}')
===============================================================================
def fun1():
return 10,20,30,40
a=fun1()
print(a)
==========================================================================
def fun1():
return 10
return 20
return 30

x=fun1()
print(x)
=========================================================================
def fun1():
print("PYTHON")
return
print("JAVA")

fun1()
================================================================================
function required positional only parameters
===================================================================
def fun1(a,b,/):
print(a,b)

fun1(100,200)
#fun1(a=100,b=200)
============================================================================
function with required keyword only parameters
============================================================================
def fun1(*,a,b):
print(a,b)

fun1(a=10,b=20)
#fun1(100,200)
==========================================================================
def fun1(a,b,/,*,c,d):
print(a,b,c,d)

fun1(10,20,c=30,d=50)
fun1(10,20,c=30,d=40)
===============================================================================
function with default parameters or arguments
===================================================================================
=
these parameters are given values at the time of defining function. at the time of
invoking function, if values are not given it assign default values

syntax:

def function-name(param1,param2,param3=value,param4=value,..):
statement-1
statement-2
=================================================================================
def fun1(a=10,b=20):
print(a,b)

fun1()
fun1(100)
fun1(b=200)
fun1(1000,2000)
fun1(a=1,b=2)
===========================================================================
def simple_int(a,t,r=1.5):
s=a*t*r/100
return s

res1=simple_int(45000,12)
print(f'simple int is {res1}')
res2=simple_int(50000,24,2.0)
print(f'simple int is {res2}')
===============================================================================
def draw_line(ch='*',size=30):
for i in range(size):
print(ch,end='')
print()

draw_line()
draw_line('$')
draw_line(size=20)
draw_line("$",15)
============================================================================
function with variable length parameters
============================================================================
function with variable length parameter receives 0 or more values
a function can be defined with 2 types of variable length parameters
1. function with postional variable length parameters
2. function with keyword variable length parameters
==================================================================================
function with positional variable length parameters
===================================================================================
variable length parameter is prefix with *

def function-name(*variable-name):
statement-1
statement-2

variable length parameter is of type tuple


=============================================================================
def fun1(*a):
print(a,type(a))

fun1()
fun1(10)
fun1(10,20,30,40,50,60)
================================================================================
def add_numbers(*values):
s=0
for value in values:
s=s+value
return s

res1=add_numbers()
print(res1)
res2=add_numbers(10,20)
print(res2)
res3=add_numbers(10,20,30,40,50,60)
print(res3)
==========================================================================
function with variable length keyword arguments
=========================================================================
variable length keyword arguments are prefix with **
this parameter receive key and value and store these keys and values into
dictionary
variable length keyword argument is of type dictionary

def function-name(**parameter-name):
statement-1
statement-2

def fun1(**a):
print(a,type(a))

fun1()
fun1(a=10)

fun1(a=100,b=200,c=300)
========================================================================
def fun1(*a,**b):
print(a,b)

fun1(10,20,30,40)
fun1(x=100,y=200,z=300)
fun1(10,20,x=100,y=200)
=======================================================================

You might also like