User-Defined Functions
A function is a set of
statements that perform a specific task, a common structuring element that
allows you to use a piece of code repeatedly in different parts of a program.
The use of functions improve a program’s clarity and comprehensibility and
makes programming more efficient by reducing code duplication and breaking down
complex tasks into more manageable pieces. Functions are also known as
routines, subroutines, methods, procedures, or subprograms.
They can be passed as arguments, assigned to
variables, or stored in collections.
A user-defined Python
function is created or defined by the def statement and follows the syntax:
def function_name(parameter list):
The indented statements make
up the body of the function and are executed when the function is called. Once
the function is called, parameters inside round brackets become arguments.
Function bodies can have more
than one return statement which may be placed anywhere within the function
block. Return statements end the function call and return the value of the
expression after the return keyword. A return statement with no expression
returns the special value ‘None’. In the absence of a return statement within
the function body, the end of the function is indicated by the return of the
value ‘None’.
The docstring is an optional
statement after the function title which explains what the function does. While
it is not mandatory, documenting your code with a docstring is a good
programming practice.
Here is a simple function that prints I love
Pizza!
def love_pizza():
print “I love
Pizza!”
Here is a function with a parameter and return
keyword:
def absolute_value(number):
if number >=
0:
return number
else:
return -number
print(absolute_value(3))
print(absolute_value(-5))
In the above example, number is the parameter of
the function absolute_value. It acts as a variable name and holds the value of
a passed in argument.
Here is the output when the above code is run:
3
5
Following is a function with an if-then-else
statement.
def shutdown(yn):
if yn.lower() ==
“y”:
return(“Closing
files and shutting down”)
elif yn.lower()
== (“n”):
return(“Shutdown
cancelled”)
else:
return(“Please
check your response.”)
print(shutdown(“y”))
print(shutdown(“n”))
print(shutdown(“x”))
Python Shell will display:
Closing files and shutting down
Shutdown cancelled
Please check your response.
Function can take more than one parameter and
use them for computations:
return x * y + 2
print(calculator(2,6))
print(calculator(3,7))
Run the code and you’ll get the output:
14
23
Functions can call other functions
Functions
can perform different types of actions such as do simple calculations and print
text. They can also call another function.
For example:
def members_total(n):
return n * 3
def org_total(m):
return
members_total(m) + 5
To see what you code does, enter the following
print commands:
print(org_total(2))
print(org_total(5))
print(org_total(10))
You’ll get these results:
20
35
Comments
Post a Comment
If you have any query, please let us know