Introduction to Functions in C++: Define, Call, Parameters
Learn functions in C++, including defining, calling, and using parameters to build modular and reusable programs.
In C++, functions are reusable blocks of code that perform a specific task. They help keep large programs organized, readable, and maintainable. In this article, we will learn step-by-step how to define, call, use parameters, and return values in functions.
1. What Is a Function?
A function is an independent block of code that performs an operation and optionally returns a value. The general structure of a function in C++ is as follows:
return_type functionName(parameters) {
// Function body
}
Example:
int Add(int a, int b) {
return a + b;
}
This function takes two integers and returns their sum.
2. The 4 Basic Components of a Function
| Component | Description | Example |
|---|---|---|
| Return type | The data type returned by the function | int |
| Function name | The name used when calling the function | Add |
| Parameter list | The values the function accepts | (int a, int b) |
| Body | The code that executes within the function | { return a + b; } |
3. Defining and Calling a Function
Functions can be called inside or outside the main() function.
Typically, they are defined outside and called within main().
#include <iostream>
using namespace std;
int Add(int x, int y) {
return x + y;
}
int main() {
int result = Add(5, 8);
cout << "Sum: " << result << endl;
return 0;
}
When calling the function, parameters are passed in order.
In the call Add(5, 8), x=5 and y=8.
4. Return Type (return)
A function can return a result to where it was called.
This value is specified using the return statement.
double Average(int a, int b, int c) {
return (a + b + c) / 3.0;
}
int main() {
cout << "Average: " << Average(70, 85, 90) << endl;
}
If the function does not return a value, it is declared with the void type.
void PrintMessage() {
cout << "Hello, C++!" << endl;
}
5. Using Parameters
Functions can take parameters to perform dynamic operations. Parameters can be passed either by value or by reference.
Pass by Value
A copy of the variable is passed to the function. The original variable remains unchanged.
void ChangeValue(int x) {
x = x + 10;
}
int main() {
int number = 5;
ChangeValue(number);
cout << number; // 5
}
Pass by Reference
The & symbol allows passing the actual variable to the function.
Any changes made inside the function affect the original variable.
void ChangeValue(int &x) {
x = x + 10;
}
int main() {
int number = 5;
ChangeValue(number);
cout << number; // 15
}
6. Default Parameters
In C++, function parameters can have default values. If no argument is provided during the call, the default value is used.
void Greet(string name = "Visitor") {
cout << "Hello, " << name << "!" << endl;
}
int main() {
Greet(); // Hello, Visitor!
Greet("Alice"); // Hello, Alice!
}
7. Function Declaration (Prototype)
If a function is called before the compiler knows its definition, an error occurs. In such cases, the function signature (prototype) is declared above.
// Declaration
int Add(int a, int b);
int main() {
cout << Add(3, 4);
}
// Definition
int Add(int a, int b) {
return a + b;
}
8. Function Overloading
In C++, multiple functions can share the same name, as long as they differ in the number or types of parameters.
int Add(int a, int b) {
return a + b;
}
double Add(double a, double b) {
return a + b;
}
int main() {
cout << Add(3, 4) << endl; // int version
cout << Add(2.5, 4.1) << endl; // double version
}
9. Example: Geometric Calculations
The following example demonstrates how multiple functions can be used for different types of calculations.
#include <iostream>
using namespace std;
double SquareArea(double side) {
return side * side;
}
double RectangleArea(double width, double height) {
return width * height;
}
int main() {
cout << "Square area: " << SquareArea(4) << endl;
cout << "Rectangle area: " << RectangleArea(3, 5) << endl;
return 0;
}
This example shows how multiple functions can be defined and called within the same program.
10. TL;DR
- Functions make repetitive operations modular.
returnsends back a value;voidmeans no return.- Parameters can be passed by value or by reference.
- Default parameters allow flexible function calls.
- Multiple functions with the same name can exist (overloading).
- All examples can be run in Visual Studio 2022 or GCC 11+.