0% found this document useful (0 votes)
57 views10 pages

Function Full Complete Theory Notes

Uploaded by

Land lord
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)
57 views10 pages

Function Full Complete Theory Notes

Uploaded by

Land lord
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
You are on page 1/ 10

functions:

**********

*functions is a set of stmt's /block of stmt's it will get execute when ever it is
called (or)
invoke is called as functions.

*function will never get execute without calling, we should call explicitly.

*how many times we call, those many times it will execute and give me o/p.

*functions are classified into 2types,

1.pre-defined/built-in function
2.user defined function

1.pre-defined/built-in function:
********************************
*these are the functions which are already present in s/w and we will utilize is
called as
pre-defined functions.
*we can't modify the pre-defined function.
example:
--------
print(), input(), len(), type(), instance(), int(), list(), etc...

2.user defined function:


************************
*these are the functions which are defined/developed by user based on customer reqt
specification
is called as user defined function.

*we can modify the user defined function.

syntax
------
def function_name(parameters): #function declaration
stmt1
stmt2
stmt3
:
return data

function_name(arguments) #function call

"""
#exanple on function
def wish():
print("Gud morning")
#wish -> if we write without any opening and closing brackets() then it is just a
function name
# wish() --> Gud morning
# wish() --> Gud morning

#print(wish()) #None
#above line function is not returning any value so, default it will return "None"

#print(wish) #<function wish at 0x00000210E5233E20>


#the above line we are printing the function name, it will print memory address of
function

#1.wf to perform addition of 2 numbers


def addition():
a = 10
b = 20
print(f"Result:{a+b}")

#addition() #Result:30

#wf to check string is palindrome or not


def palindrome():
name = "momy"
if name==name[::-1]:
print("Palindrome string")
else:
print("Not palindrome string")

#palindrome() #Not palindrome string

#according to above example's the function will exceute same set of i/p, if we want
to run for different i.p

#then we should modify in the program, it will become hard coding, to avoid thid we
can go input()

#2.wf to check string is palindrome or not with user input


def check_palindrome():
name = eval(input("enter a string:"))
if name==name[::-1]:
print("Palindrome string")
else:
print("Not palindrome string")

#check_palindrome()
"""
enter a string:"nayan"
Palindrome string
"""
#according to above example, if we go input() function to take i/p from the user,
there is drawback that

#is it should wait until the user enter some data, bcz of this time consumption to
avoid this, when we

#call a function there it self only we can pass the i/p is called as function with
parameter/argument.

function with argument/parameters:


**********************************
*a function will accept argument/parameter in function declaration.

*when we are calling function, then it is mandatory to pass arguement/parameter to


the function.

*the no. of parameters declared in function declartion, same no. of argumnets need
to pass in function call.

syntax:
-------
def function_name(arg1, arg2, arg3, .....): #arg1=val1, arg2=val2,
arg3=val3 internally vaues will get assigned to argumnet
stmt1
stmt2
return data

function_name(val1, val2, val3, .....)

types of argumnet:
******************
*calling a function with different argument as classified below,

1.Normal function calling with argument (postional arguments)

2.positional only argument

3.keyword only argument

4.combination of positional only and keyword only argument

5.combination of keyword only and positional only argument

6.variable positional only argument

7.variable keyword only argument

8.combination of variable positional and variable keyword argumnet


"""
#1.Normal function calling with argument
def add(a, b): #a=10 b=20
print(a+b) #10+20 --> 30

#different ways of passing argument


# add(10, 20) #30 ->positional arg
# add(30, 50) #50 ->positional arg
# add(a=30, b=60) #90 ->keyword arg
# add(b=30, a=35) #65 ->keyword arg
# add(20, b=30) #50 ->combination of positional and keyword arg

#possiable way of passing argument


# add() #TypeError: add() missing 2 required positional arguments:
'a' and 'b'
# add(10) #TypeError: add() missing 1 required positional argument:
'b'
# add(10, 20, 30) #TypeError: add() takes 2 positional arguments but 3
were given
# add(a=10,20) #SyntaxError: positional argument follows keyword
argument
"""

2.positional only argument(/):


*positional only argument is indicated with forward slash(/).
*in function declaration end of the parameter if we specify (/) then that
function will accept
only positional argument.
*we can't pass keyword argument.
"""
def mul(a, b, c,/):
print(a*b*c)

# mul(1,2,4) #8
# mul(a=3,b=2,c=1) #TypeError: mul() got some positional-only arguments
passed as keyword arguments: 'a, b, c'
# mul(1,2,c=4) #TypeError: mul() got some positional-only arguments
passed as keyword arguments: 'c'

"""
3.keyword only argument(*):

*keyword only argument is indicated with *.

*in function declaration beginning of the parameter if we specify (*) then that
function will accept
only keyword argument.

*we can't pass positional argument.

*we can interchange the keyword argument when we are calling the function

"""
def sub(*,a,b,c):
print(a-b-c)

# sub(a=50,b=20,c=10) #20
# sub(c=60,a=20,b=10) #-50
# sub(10,20,50) #TypeError: sub() takes 0 positional arguments but 3
were given
# sub(a=20,b=90,20) #SyntaxError: positional argument follows keyword
argument

"""
4.combination of positional only and keyword only argument:
*only positional arg denoted by / and only keyword arg denoted by *
*"always positional argument followed by keyword argument".
*always "/" followed by "*" in function declaration.
"""

def sum(a,b,/,*,c,d):
print(a+b+c+d)

# sum(20,30,c=10,d=40) #100
# sum(11,33,d=55,c=25) #124
# sum(20,4,30,40) #TypeError: sum() takes 2 positional arguments but 4
were given
# sum(c=20,d=10,20,10) #SyntaxError: positional argument follows keyword
argument

"""
5.combination of keyword only and positional only argument
*combination of keyword and positional argument is not possiable.
"""

# def div(*,a,b,c,d,/):
# print(a/b/c/d)

# div(a=10,b=20,30,40) #SyntaxError: invalid syntax


# div(a=10,b=30,c=40,d=50) #SyntaxError: invalid syntax

"""
6.variable positional only argument(*args):
*if a user doen't know how many positional argument will pass when we do
function in that case we go
variable positional only argument.

*it denoted with "*args" and specify in function declartion.

*it will accept any no. of positional argument and it will store in "tuple
format".

*it will not accept keyword argument.

*even we can pass empty argument then it will dispaly as empty tuple.
"""

def sqr(*args):
print(args)

# sqr() #()
# sqr(2) #(2,)
# sqr(2,3) #(2, 3)
# sqr(2,3,4,5,6) #(2, 3, 4, 5, 6)
#sqr(a=10,b=20,c=30) #TypeError: sqr() got an unexpected keyword argument 'a'

"""
7.variable keyword only argument(**kwargs):

*if a user doesn't know how many keyword argument will pass when we do function
in that case we go
variable keyword only argument.

*it denoted with "**kwargs" and specify in function decleration.

*it will accept any no. of keyword argument and it will store in "dictionary
format".

*it will not accept positional argument.

*even we can pass empty argument then it will dispaly as empty dictionary.
"""

def cube(**kwargs):
print(kwargs)

# cube() #{}
# cube(a=10,b=20) #{'a': 10, 'b': 20}
# cube(a=10,c=40,b=22) #{'a': 10, 'c': 40, 'b': 22}
# cube(10,a=20,c=30) #TypeError: cube() takes 0 positional arguments but 1 was
given

"""
8.variable positional only argument and variable keyword only
argument(*args,**kwargs):
*if a user doen't know how many positional and keyword argument will pass when
we do function in
that case we go variable positional and keyword only argument.

*it denoted with "*args" and "**kwargs" and specify in function declaration.

*it will accept any no. of positional and keyword argument and it will store in
"tuple format" and
"dictionary" format.

*even we can pass empty argument then it will dispaly as empty tuple and empty
dictionary.
"""
def expo(*args,**kwargs):
print(args,kwargs)

# expo() #() {}
# expo(10,20,30,40,a=1,b=2,c=3) #(10, 20, 30, 40) {'a': 1, 'b': 2,
'c': 3}
# expo(b=10,a=30) #() {'b':10, 'a':30}
# expo(1,2,3) #(1, 2, 3) {}
# expo(10,a=20,30,40) #SyntaxError: positional argument
follows keyword argument

"""

note:
=====
*if we write "*" in function declaration then it will pack the iterable.

*if we write "*" in function call then it will "un-pack" the iterable.
"""

#example on packing iterable


def wish(*args):
print(args)
# wish("ge","gn","gm",) #('ge', 'gn', 'gm')

#example on unpacking iterable


def wish(*args):
print(*args) #ge gn gm
# wish("ge","gn","gm",)

#example on unpacking iterable


l = [10, 20, 30]
def list_sum(a,b,c): #a=10,b=20,c=30
print(a+b+c) #60
# list_sum(*l) #list_sum(10,20,30)

#example on unpacking iterable


s = "hai"
def char(a,b,c): #a='h', b='a' c='i'
print(a,b,c) #h a i
#char(*s) #cahr('h', 'a' , 'i')

"""
return keyword:
***************

*return is keyword, which is used to access the value present inside the function,
to be accessed
outside the function then we go "return" keyword.

*to access and modify the local variable outside the function then go "return"
keyword.

*in function we can return sing value and multiple values.

*if we return single value it will be in same type.

*if we return multiple values then it will in tuple format

*if function not returning any value then default it will return "None".

*values will be returned, "line where the function got called".

*what ever the function is returning it can be access in 2 ways,


1.store in a variable
2.print the function directly

syntax:
-------
def fun():
stmt
return val #returning single value
(or)
return val1, val2, ... #returning multiple values

var_name = fun() #storing returned value in variable


(or)
print(fun()) #printing function
"""

#example accessing variable inside the function in outside the function

def add():
a = 10
# print(a) #NameError: name 'a' is not defined

"""
*according to above example "a" is a variable which is present inside the function,
we are trying to
acess outside the function, it will Error.

*if we want to access the value present inside the function be access outside the
function,
then we should use "return" stmt.
"""

#example on returning and storing in a variable


def add():
a = 10
return a

# res = add()
# print(res) #10

#example on returning and printing the function


def add():
a = 10
return a

# print(add()) #10

#example on without any return stmt


def add():
a = 10

# res = add()
# print(res) #None

#example on returning multiple values


def add():
a = 10
b = 20
return a,b

# print(add()) #(10, 20)

#3.wp to return length of the iterable without using any inbuilt function

def length(iterable): #iterable=[10,20,30,40]


count = 0
for _ in iterable:
count = count+1
return count

# print(length([10,20,30,40])) #4
# print(length("python")) #6
# print(length((11,22,33,44,55,66,77,88))) #8

default parameter:
******************
*a function declared with parameters and not passing any arguments (or) no. of
arguments missmatch then it will
throw error.
*we will decalre function with default value for all the parameters decalred in a
function.

*if user pass the value/argument to a function then default value will get
overriden else it will take
default value specified in a function and perform the operation.

*default parameters should be assigned in function declaration with equalto(=)


operator.

*default value can be anything, it totally depends on the program wht to consider
as a default value

syntax:
-------
def fun(var1=value, var2=value, var3=value, ..... ):
...

fun(value1,..)
"""

#example on function without default argument


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

# add(10, 20, 30) #60


# add(20, 40) #TypeError: add() missing 1 required positional
argument: 'c'
# add(10) #TypeError: add() missing 2 required positional
argument: 'b','c'
# add() #TypeError: add() missing 3 required positional
argument: 'a','b','c'

#function with default argument


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

# add(10, 20, 30) #60


# add(20, 40) #60
# add(10) #10
# add() #0

#function with default argument


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

# add() #8
# add(3) #9
# add(1,2) #4
# add(1,7,8) #16

You might also like