C++ User-defined Function Types
In C++, a user-defined function is a function that you define to perform a specific task in your program. These functions can help make your code more modular, reusable, and easier to understand. User-defined functions are crucial for breaking complex problems into smaller, manageable pieces.
There are different types of user-defined functions based on their return types, parameters, and whether they perform any operations or simply return values. In this blog, we'll explore the following types:
A void function is one that doesn't return any value. It can be used when you want to perform an action, such as printing something to the screen or modifying a variable, but you don't need to return any result.
#include <iostream>
using namespace std;
// Void function definition
void displayMessage() {
cout << "Hello, this is a user-defined function!" << endl;
}
int main() {
// Function call
displayMessage();
return 0;
}
Output:
Hello, this is a user-defined function!
Explanation:
displayMessage()
is a void function because it does not return any value. It simply prints a message.void
is used as the return type to indicate that no value will be returned from the function.A function that returns a value performs some operation and sends a result back to the calling code. You define the return type of the function based on the type of value you want to return.
#include <iostream>
using namespace std;
// Function that returns an integer value
int addNumbers(int a, int b) {
return a + b;
}
int main() {
int result = addNumbers(10, 5); // Function call
cout << "The sum is: " << result << endl;
return 0;
}
Output:
The sum is: 15
Explanation:
addNumbers()
takes two integer parameters and returns their sum.int
, meaning it will return an integer value to the calling function.Functions can accept parameters (input values) that are passed when calling the function. These values are used inside the function to perform a task.
#include <iostream>
using namespace std;
// Function with parameters to calculate the area of a rectangle
double calculateArea(double length, double width) {
return length * width;
}
int main() {
double area = calculateArea(5.0, 3.0); // Function call with parameters
cout << "Area of the rectangle is: " << area << endl;
return 0;
}
Output:
Area of the rectangle is: 15
Explanation:
calculateArea()
takes two parameters: length
and width
, and returns their product (the area of the rectangle).5.0
and 3.0
representing the length and width of the rectangle.In C++, you can define default arguments for function parameters. This allows you to call the function with fewer arguments than the function definition provides, and the missing arguments will automatically take their default values.
#include <iostream>
using namespace std;
// Function with default arguments
double calculateArea(double length, double width = 1.0) {
return length * width;
}
int main() {
double area1 = calculateArea(5.0); // Call with one argument
double area2 = calculateArea(5.0, 3.0); // Call with both arguments
cout << "Area with default width: " << area1 << endl;
cout << "Area with specified width: " << area2 << endl;
return 0;
}
Output:
Area with default width: 5
Area with specified width: 15
Explanation:
calculateArea()
defines width
with a default value of 1.0
.width
defaults to 1.0
.C++ allows function overloading, which means you can define multiple functions with the same name but with different parameter types or numbers. This helps in performing similar operations with different types of data.
#include <iostream>
using namespace std;
// Overloaded function to calculate area of a rectangle
double calculateArea(double length, double width) {
return length * width;
}
// Overloaded function to calculate area of a square
double calculateArea(double side) {
return side * side;
}
int main() {
double area1 = calculateArea(5.0, 3.0); // Rectangle
double area2 = calculateArea(4.0); // Square
cout << "Rectangle area: " << area1 << endl;
cout << "Square area: " << area2 << endl;
return 0;
}
Output:
Rectangle area: 15
Square area: 16
Explanation:
calculateArea()
is overloaded with two versions: one that calculates the area of a rectangle (two parameters) and one that calculates the area of a square (one parameter).Recursion is a concept where a function calls itself. Recursive functions are useful for problems that can be broken down into smaller subproblems, such as calculating the factorial of a number or traversing a tree.
#include <iostream>
using namespace std;
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0) {
return 1; // Base case
} else {
return n * factorial(n - 1); // Recursive call
}
}
int main() {
int result = factorial(5);
cout << "Factorial of 5 is: " << result << endl;
return 0;
}
Output:
Factorial of 5 is: 120
Explanation:
factorial()
calls itself to compute the factorial of a number.n == 0
, which returns 1
. For any other value of n
, the function continues to call itself until n
reaches 0
.