In this tutorial, we are going to learn about functions and types of functions in C++.
Firstly, we will learn what is a function. A function is a group of statements that together performs a specific task. Every programming language has at least one function, that is main(). you can break the entire code into simpler functions, dividing up the code depending on the user, but logically the division of code is done in such a manner that every function performs the specified task. The function is just a placeholder of the code until the function gets called.
- Functions are used in developing codes because by using functions, we can decrease the code redundancy, that is if we need to perform a specific task over multiple objects, functions can play a vital role in reducing the redundancy and increasing efficiency.
- Functions make the code more modular. Consider having a large code, by dividing the large code into simpler functions, we can easily understand the specific functionality of the code
In C++, the general definition of a function is as follows.
A C++ function definition consists of a function header and a function body. The function definition is classified into all these parts.
The return type of a function is optional, which means it depends on the user whether to declare a function with a return type or not. Return type defines the data type of the value that the function returns. When there is no need for any kind of return value for a specific function, then we use the return type as void
The function name can be anything based on user interest. When we call a function, the calling function name should be the same as the function name that we defined.
When you declare a function, you may pass some parameters. These parameters are known as formal parameters. These parameters consist of the variables along with the data type mentioned. The variables are separated with commas. When the defined function is invoked, we may pass parameters according to the function. These parameters are known as actual parameters. Parameters are optional, that is a function may contain no parameters
This function body consists of the definition of the function.
Now we will look at a sample example of declaring a function
In this way, a function is declared. We declared a function named fun() and we passed a parameter of an integer variable x. the function fun() returns the value which is an integer type
Every C++ program consists of a special function, which is main() function. This main() function serves as the entry point of the program. This main() function is called by the operating system whenever the user tries to run the program. This main() function is of two types.
The definition of a function consists of the actual body of the function. Whenever a function is called, the entire body of the function gets executed. Thus, giving a body to a function itself is known as defining a function
Now let’s look at a sample example implementing the definition of the function
Here in the above example, the entire definition of the function can be done and we normally defined a variable x with some value in the function definition. This defined body of the function gets executed whenever the function gets called.
Calling a function
The calling of a function plays a vital role in using functions as until a function gets called, it occupies only the space of the code and it just holds its place in the code.
Now let’s look at a sample program implementing the function declaration, function definition, calling of the function
In the above example, a function fun() is declared and the body of the function is defined later in the main function, we called the function and at that instance, the function gets invoked and gets executed. This is a sample example of the usage of functions.
That’s it for this article. In the next article, we will learn about different ways of passing parameters to a function.