Why Use Functions in C++?
Functions in C++ are essential for making code more modular, reusable, and manageable. They allow you to break down complex tasks into smaller, manageable pieces of code, improving readability and reducing errors. By using functions, you can avoid repeating code, which helps in writing cleaner and more efficient programs. Functions also allow for better debugging and maintenance of your code, as each function can be tested independently.
User-defined Functions
User-defined functions in C++ are functions created by the programmer to perform specific tasks. These functions allow you to define operations that are not part of the standard C++ library. By creating your own functions, you can make your code more organized and reusable. You can pass data to functions using parameters and receive results through return values. User-defined functions also help in reducing redundancy, enhancing program structure, and improving overall code clarity.
Types of Parameters
Default Parameter
A default parameter is an argument to a function that is not needed to be specified by the programmer. If we call the function without an argument, the default value, also known as the optional value, is used.
The default parameter is assigned using an equals sign (=).
Example
#include <iostream>
#include <string>
using namespace std;
void foo(int num = 10) {
cout << num << "\n";
}
int main() {
foo(1);
foo();
foo(20);
return 0;
}

You can also try this code with Online C++ Compiler
Run CodeOutput
1
10
20

You can also try this code with Online C++ Compiler
Run CodeYou can try and compile with the help of online c++ compiler.
Multiple Parameters
We can add as many parameters as we want in the function declaration separated by a comma. When working with multiple variables, the function call must have the same number of arguments as parameters, and the arguments must be supplied in the same sequence.
Example
#include <iostream>
#include <string>
using namespace std;
void foo(string first_name,string last_name, int age) {
cout << first_name <<" "<< last_name << " is "<<age << " years old. \n";
}
int main() {
foo("Tom", "Holland",25);
return 0;
}

You can also try this code with Online C++ Compiler
Run CodeOutput
Tom Holland is 25 years old.
Types of Parameter Passing
Pass By Value
Here the actual value of an argument is copied into the function parameter. Inside the function, changes to the parameter do not affect the argument.
Pass By Pointer
Here, the address of an argument is copied into the formal parameter. The address is used within the function to access the actual parameter used in the call. This signifies that changes to the parameter have an effect on the argument.
Pass By Reference
Here the reference of an argument is copied into the formal parameter. The reference is used within the function to access the actual parameter used in the call. This signifies that changes to the parameter affect the argument.
Before the argument, we use an “&” symbol to pass by reference.
Check out this article - Balanced Parentheses
Must Read Lower Bound in C++, Difference between argument and parameter
Function Call Methods
In C++, there are two primary methods to call a function: call by value and call by reference.
- Call by Value: In this method, the actual value of the argument is passed to the function. The function works on a copy of the data, meaning any changes made inside the function do not affect the original value. This method is useful when you don't want the function to alter the original data.
- Call by Reference: In this method, instead of passing a copy of the argument, a reference (or memory address) of the original variable is passed to the function. This allows the function to modify the original data directly. It is more efficient when large data needs to be passed or when you want to update the value of the arguments.
Both methods have their use cases depending on whether you need to modify the original data or simply work with a copy.
Frequently asked questions:
What is called function in C++?
A function in C++ is a block of code that performs a specific task. It can take input parameters, process them, and return a result. Functions help in reusability and organizing code into smaller, manageable pieces.
What are main functions in C++?
The main function in C++ is the entry point of a program. It is where execution starts. Every C++ program must have a main() function, and it typically returns an integer value to indicate the program's success or failure.
What is the function of this in C++?
In C++, the this keyword refers to the current object instance within a class. It is used to access member variables and functions of the class. It helps to differentiate between member variables and parameters with the same name.
Conclusion
In this article, we learned about function parameters in C++. We saw how parameters help functions receive input, process data, and return results. We also discussed different types of parameters, like value, reference, and pointer parameters, and how they improve code flexibility and reusability.
That’s all for today, folks. If you wish to learn from more curated articles, look at our library.
Recommended Readings:
Keep Coding!