Page 113 - trackpad v5.1 class 8 flipbook
P. 113
User-Defined Functions
User-defined functions are created by the user according to the need of the program. Once the
user defines a function, the user can call it in the same way as the built-in functions.
The main difference between these two categories is that built-in functions do not require to
be written by us, whereas a user-defined functions are created by the programmer to address
specific requirements or perform custom tasks within a program.
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: Provide a meaningful name to your function.
Parameters: The parameters (separated by commas) are specified within the parenthesis
following the name of the function. These are basically the input values we pass to the function.
Body of the Function: The body of the function contains Python statements that make our
function perform the required task.
The syntax of creating a function is as follows:
def < name of the function > (list of parameters)
<body>
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 and pass the parameters. For example:
To call a function
def my_function() Name of a function
print (“Hello”) Body of a function
my_function() Function call
Program 1: To print a string by calling a function
Program1.py
File Edit Format Run Options Window Help
#Function definition
def hello (name):
print('Hello ' +name+ '! How do you do?')
#Calling function by passing parameter
hello('Orange')
Functions, String and List in Python 111

