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

Functions GRD 12

Uploaded by

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

Functions GRD 12

Uploaded by

papneja.aniket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

FUNCTIONS

• An optional return statement


For ex:- z=5*6+sum_no(x,y)
FORMAL PARAMETERS

ACTUAL PARAMETERS

Formal parameters are given within function header and actual parameters are
mentioned in the function call statement and can be constants, variables,
symbols or expressions
Specifying names for the values being passed, in the function call is
known as Keyword arguments.
Ex 1:- positional/ required argument
def interest(principal,time,rate):
return (principal*time*rate)/100
X=interest(p,t,r)
The order and number of arguments in the function call should match with the
order and number of parameters in the function header.
Ex 2:- default argument within a function header allows to skip arguments at
the time of function call . In a function header non-default arguments
cannot follow default argument.
def interest(principal,time=2,rate=0.10):
return principal*time*rate
X=interest(p) or x=interest(p,6
Ex 3:- The Specifying of names to the values being passed, in the function call
is known as Keyword Arguments.
def interest(time=2,prin=3000,rate=0.12)
Rules for combining all three types of
arguments
Python states that in a function call statement:
• An argument list must first contain positional
(required) arguments followed by any
keyword argument
• Keyword arguments should be taken from the
required arguments preferably
• You cannot specify a value for an argument
more than once
For instance consider the following function header:
def int(prin,cc,time=2,rate=0.09):
return print*time*rate
Int(prin=3000,cc=5)
Int(rate=0.12,prin=5000,cc=4)
Int(cc=4,rate=0.12,prin=5000)
Int(5000,3,rate=0.05)
Int(rate=0.05,5000,3)
Int(5000,prin=300,cc=2)
Int(5000,principal=300,cc=2)
Int(500,time=2,rate=0.05)
Variable length arguments
In Python, we can pass a variable number or
variable length of arguments to a function using
special symbols. There are two special symbols:
• *args (Non Keyword Arguments)
• **kwargs (Keyword Arguments)
*args

The special syntax *args in function definitions in python is used


to pass a variable number of arguments to a function. It is used
to pass a non-key worded, variable-length argument list.
For example : we want to make a multiply function that takes
any number of arguments and able to multiply them all together.
It can be done using *args.
Using the *, the variable that we associate with the * becomes
an iterable meaning you can do things like iterate over it, run
some higher-order functions such as map and filter, etc.
**kwargs
• it allows us to pass the variable length of
keyword arguments to the function.
• In the function, we use the double
asterisk ** before the parameter name to
denote this type of argument. The arguments
are passed as a dictionary and these
arguments make a dictionary inside function
with name same as the parameter excluding
double asterisk **.
Scope of variables
• Scope came about because early programming languages (like BASIC) only
had global names. With this kind of name, any part of the program could
modify any variable at any time, so maintaining and debugging large programs
could become a real nightmare. To work with global names, you’d need to keep
all the code in mind at the same time to know what the value of a given name is
at any time. This was an important side-effect of not having scopes.
• Some languages like Python use scope to avoid this kind of problem. When you
use a language that implements scope, there’s no way for you to access all the
variables in a program at all locations in that program.
• Note: You’ll be using the term name to refer to the identifiers of variables,
constants, functions, classes, or any other object that can be assigned a name.
• The names in your programs will have the scope of the block of code in which
you define them. When you can access the value of a given name from
someplace in your code, you’ll say that the name is in scope. If you can’t access
the name, then you’ll say that the name is out of scope.
LEGB rule
For every name reference within a program that is when you
access a variable from within a program or function. Python
follows name resolution rule, also known as LEGB rule
1. Local environment
2. Enclosing environment
3. Global environment
4. Built-in environment
Example : Built in Environment
def calcsum(x,y):
z=x+y
return z
n1=int(input(“enter first num:”))
n2=int(input(“enter second num:”))
sum=calcsum(n1,n2)
print(sum)
NOTE:- in the above code x, y and z are local
variables. sum,n1 and n2 are global variables.
x=5
def func(a):
b=a+1
return b
y=int(input(“enter a num:”))
z=y+func(x)
print(z)
x,y and z are global variables
A,b are local variables
1.def calcsum(a,b,c):
2. s=a+b+c
3. return s
4.def avg(x,y,z):
5. sm=calcsum(x,y,z)
6. return sm/3
7.n1=int(input(“enter first number:”)) =5
8.n2=int(input(“enter second number:”))=2
9.n3=int(input(“enter third number:”))=4
10.print(“average is:”,avg(n1,n2,n3))
Variable in global scope but not in local scope

def calcsum(x,y):
s=x+y # 8
print(s)
return s
n1=int(input(“enter first num:”)) # 5
n2=int(input(“enter second num:”)) # 3
sum=calcsum(n1,n2)
print(sum)

Output:-
5
8

Variable neither in local scope nor in global scope


Python would return error as name is neither in local nor in global environment
def greet():
print(“hi”,name)
greet()
Same variable name in local scope as well as in global scope:
def state1():
tigers=15
print(tigers)
tigers=95
print (tigers)
state1()
print(tigers)

Output:
95
15
95

The underlined statement will create a local variable as it is assignment statement. It won’t
refer to tigers of main program
To use Global variable inside local scope
def state1():
global tigers
tigers=15
print(tigers)
tigers=95
print tigers
state1()
print(tigers)

Output:
95
15
15

The Underlined statement is an indication not to create local variable with the
name tigers, rather use global variable tigers.
The global statement is a declaration which holds for
the entire current code block it means that the listed
identifiers are part of the global environment.
The global statement cannot be reverted in a program
run. One should avoid using global statement in
python program.
Although global variables can be accessed through
local scope, but it is not a good programming
practice. So, keep global vatiables global, local
variables local.

You might also like