Functions:
A function is a block of code which only runs when it is called. You can pass data, known
as parameters, into a function. A function can return data as a result.
What is the value of functions?
Writing a program in “good style” is similar to writing a good practical report. In the pract
report we don’t clutter the main report with detailed calculations. In the main part of the
report, we simply refer to the result from the detailed calculations and if the reader is
interested to know how you obtained the result, he/she can go to the appendix and go
through the detailed calculations. The reason we put the detailed calculations in the
appendix is to streamline the main report and not to clutter it with detailed calculations.
The detailed calculations in the main report detracts from the flow of the main report.
In the same way, when we are writing a program in good style, the main program should
act as a master program that calls various functions when that part of the code is needed.
The details of the calculations are deferred to various functions.
Creating a Function
In Python a function is defined using the def keyword:
def function_name(parameters):
"""docstring/comments"""
statement(s)
return
Above is shown a function definition that consists of the following
components.
1. Keyword def that marks the start of the function header.
2. A function name to uniquely identify the function. Function naming follows the
same rules of writing identifiers in Python.
3. Parameters (arguments) through which we pass values to a function. They
are optional.
4. A colon (:) to mark the end of the function header.
5. Optional documentation string (docstring) to describe what the function does.
6. One or more valid python statements that make up the function body.
Statements must have the same indentation level (usually 4 spaces).
7. An optional return statement to return a value from the function.
Example
def my_function():
print("Hello from a function")
Calling a Function
To call a function, use the function name followed by parenthesis:
Example
def my_function():
print("Hello from a function")
my_function( )
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You can add
as many arguments as you want, just separate them with a comma.
1. def fact():
2. num = input("enter a number : ")
3. fact = 1
4. i = 1
5. while i <= num:
6. fact = fact * i
7. i=i+1
8.
9. print "factorial of the number : %d" %fact
10.
11. fact()
# Python program to find the factorial of a number provided by the user.
# change the value for a different result
num = 7
# To take input from the user
#num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)