You are on page 1of 6

def draw_line():

for i in range(30):
print("*",end='')
print()

def main():
draw_line() # invoking function/calling function
print("Python")
draw_line()

main()
Output:
******************************
Python
******************************
Whenever function is called the execution control switched from calling function
to called function and after execution of called function it returns to calling
function
The memory for the function is allocated when function is called/invoked and
executed and de-allocation of is done after execution of function.

Local variables
The variables declared/created within function are called local variables. The
memory for these variables allocated when function is called and de-allocated after
execution of function.
Local variables are used within function in which it is declared or created (OR) the
scope of local variable within function, these variables cannot accessible outside
the function.

Example:
def fun1():
x=100 # local variable
print(x)
def main():
fun1()
main()
Output:
100

Global variables
The variables declared outside the function are called global variables. Global
variables can be shared by more than one function. Global variables are global to
current program/module and these variables also used inside another program.

x=100 # global variable


y=200 # global variable
def fun1():
print(x)
print(y)
def fun2():
z=300 # local variable
print(x)
print(y)
print(z)
def main():
fun1()
fun2()
main()
Output:
100
200
100
200
300

Example:

num1=int(input("First Number"))
num2=int(input("Second Number"))
def add():
print(f'Sum is {num1+num2}')
def sub():
print(f'Diff is {num1-num2}')
def multiply():
print(f'Product is {num1*num2}')
def div():
print(f'Result is {num1/num2}')
def main():
add()
sub()
multiply()
div()
main()
Output:
First Number10
Second Number20
Sum is 30
Diff is -10
Product is 200
Result is 0.5

Python program is executed by invoking or executing executable statements.

def fun1():
print(x)
x=100 # executable statement
def fun2():
print(y)
y=200 # executable statement

def main():
fun1()
fun2()
main() # executable statement

Output:
100
200

Example:
def fun1():
print(k)
fun1()
k=100
Output:
print(k)
NameError: name 'k' is not defined

Example:
x=100
def fun1():
y=200 # local variable y
print(x,y)
def fun2():
x=500 # local variable x
print(x)
def main():
fun1()
fun2()
main()
Output:
100 200
500

global keyword
The global statement is a declaration which holds for
the entire current code block. It means that the listed
identifiers are to be interpreted as globals. It would
be impossible to assign to a global variable without
global, although free variables may refer to globals
without being declared global.

Syntax:
global variable,variable,…

Example:
x=100 # global variable
def fun1():
y=200 # local variable
print(x,y)
def fun2():
global x
x=500 # assign value 500 to global variable
print(x)
def fun3():
global y
y=600
print(y)
def fun4():
print(x,y)

def main():
fun1()
fun2()
fun3()
fun4()
main()
Output:
100 200
500
600
500 600

Example:
base,height=0.0,0.0
def read():
global base,height
base=float(input("Enter Base:"))
height=float(input("Enter Height"))
def find_area():
area=0.5*base*height
print(f'area of triangle is {area:.2f}')
def main():
read()
find_area()
main()
Output:
Enter Base:1.5
Enter Height2.5
area of triangle is 1.88

Function with arguments


Function with arguments receives values from calling
function (OR) function with arguments receives input.
Python allows writing a function with 4 types of
arguments.

1.Required positional arguments


2.Default arguments or optional arguments
3.Variable length arguments
4.Keyword arguments

Function arguments are local variable, which are used


within function. Memory of arguments allocated when
function is called and de-allocated after execution of
function.

Required arguments or required positional arguments

You might also like