Functions
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.
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
def function_name(parameters):
"""docstring/comments"""
statement(s)
return
6. One or more valid python statements that make up the function body.
Statements must have the same indentation level (usually 4 spaces).
Example
def my_function():
print("Hello from a function")
Calling a Function
Example
def my_function():
print("Hello from a function")
my_function( )
Arguments
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()
factorial = 1