Functions
A function is a block of code that performs a specific task. It is used to break down a program into smaller and modular pieces. Functions are used to make the code more organized, readable, and reusable. In C++, a function has the following syntax:
return_type function_name(parameters) { // code to be executed}Here is an example of a function that adds two numbers:
int add(int a, int b) { return a + b;}In the example above:
intis the return type of the function, which means the function will return an integer value.addis the name of the function.aandbare the parameters of the function.return a + b;is the code that adds the two numbers and returns the result. This could also be written asreturn sumif you have a variable calledsumthat stores the result.
Calling a Function
To call a function in C++, you need to use the function name followed by parentheses. Here is an example of calling the add function:
int result = add(5, 10);cout << result; // Output: 15In the example above, add(5, 10) calls the add function with the arguments 5 and 10, and stores the result in the variable result.
Function Prototypes
A function prototype is a declaration of a function that tells the compiler about the function’s name, return type, and parameters. It is used to inform the compiler about the existence of a function before it is called. Here is the syntax of a function prototype:
return_type function_name(parameters);Here is an example of a function prototype for the add function:
int add(int a, int b);Function prototypes are usually placed at the beginning of a program before the main function.
Function Overloading
Function overloading is a feature in C++ that allows you to define multiple functions with the same name but different parameters. The compiler determines which function to call based on the number and types of arguments passed to the function. Here is an example of function overloading:
int add(int a, int b) { return a + b;}
float add(float a, float b) { return a + b;}In the example above, we have defined two add functions with different parameter types (int and float). The compiler will call the appropriate function based on the arguments passed to the function.
Function overloading is useful when you want to perform the same operation on different data types.
Default Arguments
Default arguments are values that are used by a function if no argument is provided when the function is called. They allow you to define a default value for a parameter in a function. Here is an example of a function with default arguments:
int multiply(int a, int b = 2) { return a * b;}In the example above, the multiply function has a default argument of 2 for the parameter b. If you call the function with only one argument, the default value of 2 will be used for b.
Here is an example of calling the multiply function with and without the second argument:
int result1 = multiply(5, 3);cout << result1; // Output: 15
int result2 = multiply(5);cout << result2; // Output: 10In the example above, multiply(5, 3) multiplies 5 and 3, while multiply(5) multiplies 5 and the default value 2.
Recursion
Recursion is a programming technique where a function calls itself to solve a problem. It is useful when a problem can be broken down into smaller subproblems that are similar to the original problem. Here is an example of a recursive function that calculates the factorial of a number:
int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); }}In the example above, the factorial function calculates the factorial of a number n by calling itself with n - 1 until n reaches 0.
Here is an example of calling the factorial function:
int result = factorial(5);cout << result; // Output: 120In the example above, factorial(5) calculates the factorial of 5, which is 5 * 4 * 3 * 2 * 1 = 120.
Recursion is a powerful technique, but it can be inefficient for large problems due to the overhead of function calls. It is important to have a base case that stops the recursion to prevent infinite loops.