0% found this document useful (0 votes)
16 views14 pages

Fuction Theory DS

This has notes about Function theory in python programming

Uploaded by

Swaty
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)
16 views14 pages

Fuction Theory DS

This has notes about Function theory in python programming

Uploaded by

Swaty
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/ 14

FUNCTIONS

write reusable pieces/chunks of code, called functions


functions are not run in a program until they are
“called” or “invoked” in a program
 function characteristics:
• has a name
• has parameters (0 or more)
• has a docstring (optional but recommended)
• has a body
• returns something

8/11/2020 Python Programming 3

Dr. D.P.Sharma,MUJ
HOW TO WRITE and
CALL/INVOKE A FUNCTION
def is_even( i ):
"""
Input: i, a positive int
Returns True if i is even, otherwise False
"""
print("inside is_even")
return i%2 == 0

X=3
is_even(x)
8/11/2020 Python Programming 4

Dr. D.P.Sharma,MUJ
IN THE FUNCTION BODY
def is_even( i ):
"""
Input: i, a positive int
Returns True if i is even, otherwise False
"""
print("inside is_even")
return i%2 == 0

8/11/2020 Python Programming 5

Dr. D.P.Sharma,MUJ
Functions

8/11/2020 Python Programming 6

Dr. D.P.Sharma,MUJ
Various Forms of Function Arguments
There are 4 types of actual arguments are allowed in Python.

1) Positional Arguments

2) Keyword Arguments

3) Default Arguments

4) Variable Length Arguments

8/11/2020 Python Programming 10

Dr. D.P.Sharma,MUJ
Python Positional Arguments --Examples
def greet(name,msg):

"""This function greets to


the person with the provided message""" These are the arguments passed to
function in correct positional order. If
print("Hello", name + ', ' + msg) we change the order then result may
be changed.
greet("Monica", "Good morning!")
greet("Good morning!“, ”Monica“)
The number of arguments must equal
to the number of parameters otherwise
def sub(a, b): error will be generated.
print(a-b)
sub(100, 200)
sub(200, 100)
8/11/2020 Python Programming 11

Dr. D.P.Sharma,MUJ
Python Default Arguments--Examples
Sometimes we can provide default values for our positional arguments.
can provide a default value to an argument by using the assignment operator (=).

In this function, the parameter name does not have a


def greet(name, msg = "Good morning!"):
default value and is required (mandatory) during a call.
"""
This function greets to the person with On the other hand, the parameter msg has a default
the provided message. value of "Good morning!". So, it is optional during a call.
If a value is provided, it will overwrite the default value.
If message is not provided, it defaults to
"Good morning!" Any number of arguments in a function can have a
""" default value. But once we have a default argument, all
the arguments to its right must also have default values.
print("Hello",name + ', ' + msg)
This means to say, non-default arguments cannot follow
greet("Kate") default arguments.
greet("Bruce","How do you do?")

8/11/2020 Python Programming 12

Dr. D.P.Sharma,MUJ
Python Keyword Arguments--Examples
 We can pass argument values by
 keyword i.e by parameter name. # 2 keyword arguments

greet(name = "Bruce",msg = "How do you do?")


 Keyword argument in the form of
 name= value . # 2 keyword arguments (out of order)

def greet(name, msg = "Good morning!"): greet(msg = "How do you do? ",name = "Bruce")
"""
This function greets to the person with # 1 positional, 1 keyword argument
the provided message.
greet("Bruce",msg = "How do you do?").

If message is not provided, it defaults to Having a positional argument after keyword


"Good morning!" arguments will result into errors.
"""
print("Hello",name + ', ' + msg) greet(name="Bruce","How do you do?") //error

8/11/2020 Python Programming 13

Dr. D.P.Sharma,MUJ
Python Keyword Arguments--Examples
Here the order of arguments is not important but number of arguments must be matched.

Note: We can use both positional and keyword arguments simultaneously. But first we
have to take positional arguments and then keyword arguments, otherwise we will get
syntax error.

def wish(name,msg):
print("Hello",name,msg)
wish("DPSharma","GoodMorning")  Valid
wish("DPSharma",msg="GoodMorning")  Valid
wish(name="DPSharma","GoodMorning")  Invalid

SyntaxError: positional argument follows keyword argument

8/11/2020 Python Programming 14

Dr. D.P.Sharma,MUJ
Python Arbitrary Arguments --Examples
 Sometimes, we do not know in advance the number of arguments that will be passed into a
function. Python allows us to handle this kind of situation through function calls with arbitrary
number of arguments.

 function definition use an asterisk (*) before the parameter name to denote this kind of argument

def greet(*names):
"""This function greets all
the person in the names tuple."""

# names is a tuple with arguments


for name in names:
print("Hello", name)

greet("Monica","Luke","Steve","John")

8/11/2020 Python Programming 15

Dr. D.P.Sharma,MUJ
Python Arbitrary Arguments --Examples
We can call this function by passing any number of arguments including zero number.

Internally all these values represented in the form of tuple.


We can mix variable length arguments with
positional arguments.

8/11/2020 Python Programming 16

Dr. D.P.Sharma,MUJ
Python Arbitrary Arguments --Examples
Note: After variable length argument, if we are taking any other arguments then we
should provide values as keyword arguments.

8/11/2020 Python Programming 17

Dr. D.P.Sharma,MUJ
Python Arbitrary Arguments --Examples
Note: We can declare keyword variable length arguments also.
 For this we have to use **. Ex. def f1(**n):

 We can call this function by passing any number of keyword arguments.

 Internally these keyword arguments will be stored inside a dictionary.


Output:
def display(**kwargs): n1 = 10
n2 = 20
for k,v in kwargs.items():
n3 = 30
print(k,"=",v)
rno = 100
display(n1=10,n2=20,n3=30) name = DPSharma
display(rno=100,name="DPSharma",marks=70,subject="Java") marks = 70
subject = Java

8/11/2020 Python Programming 18

Dr. D.P.Sharma,MUJ
Python Functions—Case Study
def f(arg1,arg2,arg3=4,arg4=8):
print(arg1,arg2,arg3,arg4)
1) f(3,2)  3 2 4 8
2) f(10,20,30,40)  10 20 30 40
3) f(25,50,arg4=100)  25 50 4 100
4) f(arg4=2,arg1=3,arg2=4)  3 4 4 2

5) f()  Invalid
TypeError: f() missing 2 required positional arguments: 'arg1' and 'arg2'

6) f(arg3=10, arg4=20, 30, 40)  Invalid


SyntaxError: positional argument follows keyword argument
[After keyword arguments we should not take positional arguments]

7) f(4, 5, arg2 = 6)  Invalid


TypeError: f() got multiple values for argument 'arg2'

8) f(4, 5, arg3 = 5, arg5 = 6)  Invalid


TypeError: f() got an unexpected keyword argument 'arg5'
8/11/2020 Python Programming 19

Dr. D.P.Sharma,MUJ

You might also like