Page 157 - TP_Plus_v2.2_Class_8
P. 157
Creating a Function
We can create a function in the following ways:
Defining a Function: We use the def keyword to begin the function definition.
Naming a Function: We should provide a meaningful name to our function.
Parameters: These are the variables given inside the parentheses in the function definition. The
parameters (separated by commas) are specified within the parenthesis following the name of the
function.
Body of the function: The body of the function contains Python statements that perform the task or
computation the function is designed for.
The syntax of creating a function is as follows:
def < name of the function > (list of parameters)
<body of a function>
Calling a Function
A function can be called anytime from other functions or from the command prompt after the
definition. For calling a function, we type the function followed by parentheses containing any required
parameter. For example:
To call a function.
def my_function(): Name of a function
print (“Hello”) Body of a function
my_function() Function call
Program 2: To print a string by calling a function.
Program2.py
File Edit Format Run Options Window Help
#Function definition
def hello (name):
print('Hello ' +name+ '! How do you do?')
Output
#Calling function by passing parameter Hello Orange! How do
hello('Orange') you do?
Functions and String in Python 155

