python course 3
User-defined functions
Functions that we define ourselves to do certain specific task are referred as user-defined functions.
Functions that readily come with Python are called built-in functions. If we use functions
written by others in the form of library, it can be termed as library functions.
All the other functions that we write on our own fall under user-defined functions. So, our
user-defined function could be a library function to someone else.
Advantages of user-defined functions
1. User-defined functions help to decompose a large program into small segments which makes
program easy to understand, maintain and debug.
2. If repeated code occurs in a program. Function can be used to include those codes and execute
when needed by calling that function.
3. Programmers working on large project can divide the workload by making different functions.
Example 22:
Write a python script to print a message using user-defined function.
def message( ):
print("Iam working on it,")
print("shall let you know once I finish it off") #
Call the message function.
message( )
Docstring
The first string after the function header is called the docstring and is short for documentation string. It is used
to explain in brief, what a function does.
Although optional, documentation is a good programming practice.
Example 23:
def greet(name):
"""This function greets to
the person passed in as
parameter"""
print("Hello, " + name + ". Good morning!")
print(greet.__doc )
Output:
This function greets to
the person passed into the
name parameter
Function Call (Calling a Function)
Once we have defined a function, we can call it from another function, program or even the Python prompt. To
call a function we simply type the function name with appropriate parameters.
Example 24:
def greet(name):
"""This function greets to
the person passed in as
parameter"""
print("Hello, " + name + ". Good morning!")
greet('Paul') #call a function
Example 25:
# This program has two functions. First we define the main function. def
main( ):
print('I have a message for you.')
message( )
print('Goodbye!')
# Next we define the message function.
def message( ):
print("Iam working on it,")
print("shall let you know once I finish it off") #
Call the main function.
main( )
Example 26:
#Providing Function Definition
def sum(x,y):
"""Going to add x and y"""
s=x+y
print("Sum of two numbers is")
print(s)
#Calling the sum Function
sum(10,20)
sum(20,30)
The return statement :
The return statement is used returns a value from the function and goes back to the place from where it was
called with the expression. A return statement may contain a constant, variable, expression. If return is used
without anything, it will return None.
Example 27:
# Function definition is here
def changeme( x ):
return x #return statement with a argument
# Now you can call changeme function x
= 5
print changeme( x )
Example 28:
# Function definition is here
def changeme( x ):
return #return statement without argument
# Now you can call changeme function x
= 5
print changeme( x )
Void Functions:
Function performs a task, and does not contain a return statement such functions are called void functions. Void
functions do not return anything. 'None' is a special type in Python which is returned after a void function ends.
Example 29:
def check (num):
if (num%2==0):
print “True”
else:
print “False”
result = check (29)
print (result)
Output:
False
None
Required Arguments: In the required arguments, the arguments are passed to a function in correct
positional order. The number of arguments in the function call should exactly match with the number of
parameters specified in the function definition.
.
Keyword Arguments: Python allows functions to be called using keyword arguments. When we call
functions in this way, the order (position) of the arguments can be changed.
Example:
def display(name,x,y):
print ("The name of person is", name)
print ("The x value is", x)
print ("The Y value is", y)
display(y=20,name="RAMESH",x=40)
3. Default Arguments: A default argument is an argument that assumes a default value which defined in
the called function if a value is not provided in the function call for that argument.
Example:
def display(name,course="BTECH"):
print ("The name of person is", name)
print ("Course: ", course)
display("RAJESH","MBA")
display(course="MSC",name="RAJU") display(name="RAVI")
4. Variable-length or Arbitrary Arguments:
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.
In the function definition we use an asterisk (*) before the parameter name to denote this kind of argument.
Here is an example.
Example
def display(*names):
for i in names:
print("Hello",name)
display("Monica","Luke","Steve","John")
Comments
Post a Comment