It’s time for you to learn about functions in python programming. This tutorial will help you understand how to reuse your code using the modular approach.
The modular approach refers to the use of functions. Each Function acts as a module, and several modules together make the complex codes you will be building at the end of this program.
What is a Function in Python?
A function in python programming is nothing but a block of code that executes only when you call it.
You can pass data to the function that you can use in the function as variables.
This data passed in a function is called the function’s parameter (more on later in the article).
A function can return the results as data to the main program.
Syntax of Functions in Python Programming
The syntax of a function in python is straightforward.
In the first line of function declaration, you must declare the function and name it whatever you like.
A parenthesis and a colon follow this name.
def function_name(parameter1, parameter2):
#body of the function
#here you have to write the code
#that should be executed when you call the fucntion
Here:
def – keyword is used to define the function
function_name – is the name of the function. It can be anything you want.
parameter1 – is the parameter that will be passed in the functionparameter2 – is also a parameter like the first one.
The number of parameters in a function varies. It can be 5 or zero depending on what you want to achieve.
def sum():
The first keyword, def, is the keyword to tell the compiler that you are declaring a function.
def refers to the word define, which means defining a function in python.
The second-word sum, followed by the parenthesis, is the function’s name.
It can be anything, such as add(), product(), or multiply(). It can be any name that you like.
But it is always suggested to use a name that closely resembles what your function is doing.
As the word sum() suggests, this specific function will add the values you pass in as the function’s parameter.
Parameters and Arguments in Functions
Before you learn about the function’s body, you must learn about the parameters and arguments.
A parameter is a variable usually listed inside the parenthesis when you declare the function.
def sum(value1, value2):
Here value1 and value2 are the parameters of the function.
Whereas argument is, the value passed in the function when it is called.
total = sum(3,4)
Here 3 and 4 are the arguments or the function sum().
Body of the Functions in Python
The body of any function is where you have to write the code that should be executed when the function is called.
In the function’s body, you can use the parameters you passed early and perform any operation you want.
We will add both values in our example of the sum() function.
You can create a new variable, such as value3, to store the sum of value1 and value2.
def sum(value1, value2):
value3 = value1 + value2
return value3
Or you can either return the sum of value1 and value2 without creating the new variable value3, such as:
def sum(value1, value2):
return value1 + value 2;
Now, you have to call this function in your main code. For example:
def sum(value1, value2):
value3 = value1 + value2
return value3
#main code to call the function
total = sum(3,4)
print(total)
#output 7
Return Keyword in Functions in Python
The return keyword is a reserved keyword in python programming. You can use this statement inside a function to return any value to the caller.
In the code above variable total is the caller of the function sum().
total = sum(3,4)
In the above code, 3 and 4 are the arguments of the function sum().
The function will add these two numbers and will return 7 as output to the caller variable total.
Now the value 7 will be stored in the total variable, and you can use it later anywhere in your program.
In this next tutorial, you will master the arguments and parameters of functions in pyt
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.