Page 97 - KEC Khaitan C8 Flipbook
P. 97
COMPONENTS OF PYTHON FUNCTION
A Python function consists of the following components:
Name of the function: A function name should be unique and easy to correlate with the task it
will perform. Function names must be unique within their scope.
Arguments: The input given to the functions is referred to as arguments. A function may or
may not have any arguments.
Statements: The statements are the executable instructions that the function perform.
Return Value: A function may or may not return a value.
TYPES OF FUNCTIONS IN PYTHON
We have mentioned earlier that one of the strengths of the Python language is that Python
functions are easy to define and use. Python functions can be categorised into built-in functions
and user-defined functions. Let us discuss them in detail.
Built-In Functions
The print() and input() belong to the category of built-in functions. We also have other built-in
functions like range(), type(), etc.
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:
Functions, String and List in Python 95

