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

Function in Python

Uploaded by

Bhoomi Dangar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
11 views10 pages

Function in Python

Uploaded by

Bhoomi Dangar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 10

Function in Python

1.What is a Function?
2.Function Call
3.Parameters &
Arguments
4.Return Statement
5.Documentation string

-prepared by
Priyanka Patel
What is Function ?
 A function is a block of organized,
reusable code that is used to perform a
single, related action. Functions provide
better modularity for your application
and a high degree of code reusing.

 As you already know, Python gives you


many built-in functions like print(), etc.
but you can also create your own
functions. These functions are
called user-defined functions.

-prepared by Priyanka
Patel 2
Function Call
Once the function is definition and structured correctly, we have to call the function to
execute it statement otherwise your function does nothing. For example,
my_first_function() in the below program is a function call. A function can be even
called multiple times in a program.

# Function definition is here


Example:
def printme( str ):
"This prints a passed string into this
function“
print str
return;

# Now you can call printme function


printme("I'm first call to user defined
function!")
printme("Again second call to the
same function")
When the above code is executed, it produces the following
result − call to user defined function!
I'm first
Again second call to the same function

-prepared by Priyanka
Patel 3
Parameters & Argument
Function Arguments
You can call a function by using the following types of Exaplm
# :Function definition is here
e
formal arguments − def printme( str ):
"This prints a passed string into this
• Required arguments function"
• Keyword arguments print str return;
• Default arguments
• Variable-length arguments: # Now you can call printme function
printme()

Required arguments Output:


Traceback (most recent call last):
Required arguments are the arguments passed File "test.py", line 11, in <module>
to a function in correct positional order. Here, printme();
the number of arguments in the function call
should match exactly with the function TypeError: printme() takes exactly 1
definition. argument (0 given)
To call the function printme(), you definitely
need to pass one argument, otherwise it gives a -prepared by Priyanka
syntax error as follows − Patel 4
Parameters & Argument
Keyword arguments
Keyword arguments are related to the function calls. When you use keyword
arguments in a function call, the caller identifies the arguments by the parameter
name.
This allows you to skip arguments or place them out of order because the Python
interpreter is able to use the keywords provided to match the values with
parameters. You can also make keyword calls to the printme() function in the
# Function definition is here
following ways −
def printme( str ):
"This prints a passed string into this Output:
function“
print str
return; My string

# Now you can call printme function


printme( str = "My string")

-prepared by Priyanka
Patel 5
Parameters & Argument
Default arguments
A default argument is an argument that assumes a default value if a value is not
provided in the function call for that argument. The following example gives an
idea on default arguments, it prints default age if it is not passed −

# Function definition is here


def printinfo( name, age = 35 ):
"This prints a passed info into this Output :
function“ Name: miki
print "Name: ", name Age 50
print "Age ", age
return; Name: miki
Age 35
# Now you can call printinfo function
printinfo( age=50, name="miki" )
printinfo( name="miki" )

-prepared by Priyanka
Patel 6
Parameters & Argument
Variable-length arguments
You may need to process a function for more arguments than you specified while
defining the function. These arguments are called variable-length arguments and
are not named in the function definition, unlike required and default arguments.
# Function definition is here
def printinfo( arg1, *vartuple ): Output is: 10
"This prints a variable passed
arguments“
Output is: 70 60
print "Output is: "
print arg1 for var in vartuple: 50
print var return;

# Now you can call printinfo function


printinfo( 10 )
printinfo( 70, 60, 50 )

-prepared by Priyanka
Patel 7
Return Statement
 The return statement returns with a value from a function instead of printing
it out. It allows us to assign the output of the new function to a new variable.
 It is the last statement to be executed inside the function, meaning the return
statement exits the function.
# A Python program to return multiple
# values from a method using class
class Test:
def __init__(self):
self.str = “Priyanka Patel" Output:
self.x = 20
Priyanka Patel
# This function returns an object of Test
def fun(): 20
return Test()

# Driver code to test above method


t = fun()
print(t.str)
print(t.x)

-prepared by Priyanka Patel 8


Documentation String
The first statement of the function body can optionally be a string literal; this string literal is
the function documentation string or docstring. It’s good practice to include docstrings in the
code that you write. A docstring explains to the public about the modules, functions, classes,
and methods.
def square(n):
'''Takes in a number n, returns the square of
n'''
return n**2

print(square.__doc__)
Output:

Takes in a number n, returns the


square of n

-prepared by Priyanka
9
Patel
thank you
Priyanka Patel
20SOECE11109
ppatel879@rku.ac.in

You might also like