Related Topics

C++ Programing
return_type function_name(parameter_list) {
// Function body (code to perform specific tasks)
// ...
return value; // Return a value of the specified return_type
}
return_type
: The data type of the value that the function will return.function_name
: The name of the function.parameter_list
: The list of parameters (if any) that the function accepts.value
: The value ofreturn_type
that the function sends back to the caller.
Here’s an example of a function that calculates the sum of two integers and returns the result:
#include
// Function to calculate the sum of two integers and return the result
int add(int a, int b) {
int sum = a + b;
return sum; // Return the sum
}
int main() {
int num1 = 5;
int num2 = 7;
// Call the function and store the result in a variable
int result = add(num1, num2);
std::cout << "Sum: " << result << std::endl;
return 0;
}
In this example, the add()
function takes two integer parameters a
and b
, calculates their sum, and returns the result. When the add()
function is called with num1 = 5
and num2 = 7
, it calculates the sum as 5 + 7 = 12
and returns this value to the caller.
The returned value 12
is then stored in the variable result
, which is displayed on the console.
Output:
Sum: 12
Using the return
statement, you can make your functions more versatile and perform different tasks, and then return results or intermediate values to be used in the rest of your code.
#include
// Function with two integer parameters
int add(int a, int b) {
return a + b;
}
// Function with three integer parameters
int add(int a, int b, int c) {
return a + b + c;
}
// Function with two double parameters
double add(double a, double b) {
return a + b;
}
int main() {
int num1 = 5, num2 = 10, num3 = 15;
double doubleNum1 = 2.5, doubleNum2 = 3.7;
// Call the first add() function with two integers
int result1 = add(num1, num2);
std::cout << "Sum of two integers: " << result1 << std::endl;
// Call the second add() function with three integers
int result2 = add(num1, num2, num3);
std::cout << "Sum of three integers: " << result2 << std::endl;
// Call the third add() function with two double values
double result3 = add(doubleNum1, doubleNum2);
std::cout << "Sum of two double values: " << result3 << std::endl;
return 0;
}
Output:
Sum of two integers: 15
Sum of three integers: 30
Sum of two double values: 6.2
In this example, we have overloaded the add()
function to handle different types and numbers of parameters. We have three versions of the add()
function:
int add(int a, int b)
: This function takes two integer parameters and returns their sum as an integer.int add(int a, int b, int c)
: This function takes three integer parameters and returns their sum as an integer.double add(double a, double b)
: This function takes two double parameters and returns their sum as a double.
The appropriate version of the add()
function is called based on the number and types of arguments provided during the function call.
Function overloading is a powerful feature in C++, as it allows you to create more versatile and expressive functions while keeping the function names consistent and easy to remember.
return_type function_name(parameter_type parameter_name1, parameter_type parameter_name2 = default_value);
return_type
: The data type of the value that the function will return.function_name
: The name of the function.parameter_type
: The data type of each parameter.parameter_name
: The name of each parameter.default_value
: The default value assigned to the parameter.
Here’s an example of a function with a default argument:
#include
// Function with a default argument
int add(int a, int b = 0) {
return a + b;
}
int main() {
int num1 = 5;
int num2 = 10;
// Call the function with one argument
int result1 = add(num1);
std::cout << "Sum with one argument: " << result1 << std::endl;
// Call the function with two arguments
int result2 = add(num1, num2);
std::cout << "Sum with two arguments: " << result2 << std::endl;
return 0;
}
Output:
Sum with one argument: 5
Sum with two arguments: 15
In this example, the add()
function has a default argument b = 0
. When you call the function with only one argument, the default value 0
is used for the second parameter b
. When you call the function with two arguments, the provided value for b
is used.
Using default arguments makes the function more flexible and allows you to provide sensible defaults when some parameters are not provided by the caller. This can be particularly useful when you want to provide a simple interface for common use cases while still allowing more customization if needed.
Keep in mind that once a default argument is specified in a function’s declaration, it must also be specified in its definition. Default arguments can only be provided in the function declaration, not in its definition.
#include
// Recursive function to calculate the factorial of a non-negative integer
int factorial(int n) {
// Base case: factorial of 0 and 1 is 1
if (n == 0 || n == 1) {
return 1;
}
// Recursive step: factorial(n) = n * factorial(n-1)
return n * factorial(n - 1);
}
int main() {
int num = 5;
int result = factorial(num);
std::cout << "Factorial of " << num << " is: " << result << std::endl;
return 0;
}
Output:
Factorial of 5 is: 120
In this example, the factorial()
function is a recursive function that calculates the factorial of a non-negative integer n
. The base case checks if n
is 0 or 1, in which case the function returns 1. For any other value of n
, the function calls itself with the argument n-1
and multiplies the result by n
. This recursive process continues until the base case is reached (when n
becomes 0 or 1).
When the factorial()
function is called with num = 5
, it recursively calculates the factorial as 5 * 4 * 3 * 2 * 1 = 120
.
Recursive functions are a powerful technique to solve problems that can be naturally broken down into smaller instances of the same problem. However, you should be careful with recursion, as it can lead to stack overflow errors or excessive memory usage if the base case is not reached within a reasonable number of recursive calls.
#include
// Function that takes two integers and a function pointer as parameters
int performOperation(int a, int b, int (*operation)(int, int)) {
return operation(a, b);
}
// Functions to be used as arguments
int add(int x, int y) {
return x + y;
}
int subtract(int x, int y) {
return x - y;
}
int main() {
int num1 = 5, num2 = 3;
// Pass the add function as a parameter to performOperation
int result1 = performOperation(num1, num2, add);
std::cout << "Result of add function: " << result1 << std::endl;
// Pass the subtract function as a parameter to performOperation
int result2 = performOperation(num1, num2, subtract);
std::cout << "Result of subtract function: " << result2 << std::endl;
return 0;
}
Output:
Result of add function: 8
Result of subtract function: 2
In this example, the performOperation()
function takes two integers (a
and b
) and a function pointer (operation
) as parameters. The function pointer operation
points to another function that takes two integers as arguments and returns an integer. Inside performOperation()
, the operation
function pointer is called with the provided arguments a
and b
.
The add()
and subtract()
functions are defined to be used as arguments to performOperation()
. When performOperation()
is called with the add
function, it returns the sum of num1
and num2
, and when called with the subtract
function, it returns the difference.
Instead of using function pointers, you can also use C++11’s std::function
and lambdas for more flexibility and type safety:
#include
#include
// Function that takes two integers and a std::function as parameters
int performOperation(int a, int b, std::function operation) {
return operation(a, b);
}
int main() {
int num1 = 5, num2 = 3;
// Using lambda functions as arguments
int result1 = performOperation(num1, num2, [](int x, int y) { return x + y; });
std::cout << "Result of add lambda: " << result1 << std::endl;
int result2 = performOperation(num1, num2, [](int x, int y) { return x - y; });
std::cout << "Result of subtract lambda: " << result2 << std::endl;
return 0;
}
Both approaches allow you to pass functions as arguments to other functions, enabling you to create more dynamic and flexible code.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.