In C++, a function is a code segment that performs a particular task. You can reuse it, which means that you can execute it more than once. Functions have a name, and they are beneficial in reading, writing, and modifying complex problems. This tutorial will help you learn all about C++ Functions.
What Are C++ Functions?
A function is defined as a group of statements or a block of code that carries out specific tasks. You need to give a particular name to the function and write some logic or group of information inside it. And then you can invoke that function from the main function. For example, you can name a function as factorial, and inside that function, you can write the logic to find the factorial of a number. And then, you can use it whenever you need that function in the program.
Why Use Functions in C++?
Functions are used to minimize the repetition of code, as a function allows you to write the code inside the block. And you can call that block whenever you need that code segment, rather than writing the code repeatedly. It also helps in dividing the program into well-organized segments.
Now, have a look at the syntax of C++ functions.
The syntax for creating a function is as follows:
Here, the return type is the data type of the value that the function will return. Then there is the function name, followed by the parameters which are not mandatory, which means a function may or may not contain parameters.
A function can be declared by writing its return type, the name of the function, and the arguments inside brackets. It informs the compiler that this particular function is present. In C++, if you want to define the function after the main function, then you have to declare that function first.
A function definition specifies the body of the function. The declaration and definition of a function can be made together, but it should be done before calling it.
When you define a function, you tell the function what to do and to use that function; you have to call or invoke the function. When a function is called from the main function, then the control of the function is transferred to the function that is called. And then that function performs its task. When the task is finished, it returns the control to the main function.
Next, this tutorial looks into the types of C++ functions.
Types of Functions in C++
There are two types of functions in C++
- Built-in functions
- User-defined functions
These are functions that are already present in C++; their definitions are already provided in the header files. The compiler picks the definition from header files and uses them in the program.
These are functions that a user creates by themselves, wherein the user gives their own definition.
You can put them down as the following types:
- No argument and no return value
- No argument but the return value
- Argument but no return value
- Argument and return value
No argument and no return value: In this type, as the name suggests, it passes no arguments to the function, and you print the output within the function. So, there is no return value.
No argument but return value: In this type, it passes no arguments to the function, but there is a return value.
Argument but no return value: In this type of function, it passes arguments, but there is no return value. And it prints the output within the function.
Argument and return value: In this type of function, it passes arguments, and there is also a return value.
Now, move on to the calling methods of C++ functions.
Function Call Methods
You can invoke or call a function in two ways: call by value and call by function.
Call By Value:
In this calling method, you only pass the copies of the variable and not the actual argument to the function. As the copies of the variable or arguments are being passed, any changes made to the variable in the function don’t affect the actual argument.
In the above program, you pass the variables to the function through the call by value method, i.e., you pass the copies of variables. So it won’t reflect the change inside the main function where the actual variables are present. But if you print the output in the called function, then you can see the changes over there.
Call By Reference:
In this calling technique, you pass the address or reference of the argument, and the function receives the memory address of the argument. In this case, the actual value of the variable changes, or you can say that it reflects the changes back to the actual variable.
In this example, you pass the reference of the variable using & (ampersand) sign and not the copy of the variable, so the actual value also changes in this case.
Below is the output of the above example.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
After reading this article on C++ functions, you would have understood why you use functions in C++, what is a function in C++, its syntax, and the types of functions in C++. You also learned about the calling methods of a function, i.e., call by value and call by reference, with the help of some examples.
If you want to learn further and explore other top software languages and tools and skills, then please visit Simplilearn’s Free Software Development Courses.
Do you have any questions regarding this article on C++ functions? If you do, then please put them in the comments section. We’ll help you solve your queries. To learn more about C++ functions, click on the following link: C++ Functions