Table of contents | |
Introduction | |
Creating a Function | |
Calling a Function | |
Function Parameters | |
Function Return Type | |
Function Overloading | |
Conclusion |
C++ is a popular programming language used for developing a wide range of applications. It is an object-oriented programming language that allows you to create classes, objects, and functions. Functions are an essential part of any programming language, including C++. A function is a group of statements that perform a specific task. Functions help to break down the code into smaller, more manageable parts, making the code more readable and reusable. In this article, we will discuss functions in C++ and how to use them.
To create a function in C++, you need to follow these steps:
Here is an example of a simple function that adds two numbers:
int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
In this example, we have declared a function called 'addNumbers' that takes two integer parameters ('a' and 'b'). The function then calculates the sum of these two numbers and returns the result.
Once you have created a function, you can call it from another part of your code. To call a function, you need to use its name and provide any required parameters.
Here is an example of how to call the 'addNumbers' function:
int result = addNumbers(5, 7);
In this example, we are calling the 'addNumbers' function with two integer values (5 and 7). The result of this function call is then assigned to the variable 'result'.
void printNumbers(int a, int b) {
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
In this example, we have declared a function called 'printNumbers' that takes two integer parameters ('a' and 'b'). The function then prints the value of each parameter to the console.
Functions can return a value or not. If a function does not return a value, then its return type is 'void'. If a function returns a value, then you must declare the type of the value that the function will return.
Here is an example of a function that returns a value:
int square(int x) {
return x * x;
}
In this example, we have declared a function called 'square' that takes an integer parameter ('x'). The function then calculates the square of the parameter and returns the result as an integer.
Function overloading is a feature in C++ that allows you to create multiple functions with the same name but with different parameters. The compiler will choose the appropriate function to call based on the parameters that are passed to the function.
Here is an example of function overloading:
int addNumbers(int a, int b) {
return a + b;
}
double addNumbers(double a, double b) {
return a + b;
}
In this example, we have declared two functions called 'addNumbers'. One function takes two integer parameters ('a' and 'b'), while the other function takes two double parameters ('a' and 'b'). The compiler will choose the appropriate function to call based on the types of the parameters that are passedSample Problems:
1. Write a C++ function to calculate the factorial of a given number.
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
In this example, we have declared a function called 'factorial' that takes an integer parameter ('n'). The function then calculates the factorial of the parameter recursively using the formula 'n! = n * (n-1)!'. The function returns the result as an integer.
2. Write a C++ function to find the maximum of three numbers.
int max(int a, int b, int c) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return max;
}
In this example, we have declared a function called 'max' that takes three integer parameters ('a', 'b', and 'c'). The function then compares each parameter to find the maximum value and returns it as an integer.
3. Write a C++ function to check if a given string is a palindrome.
bool isPalindrome(string str) {
int length = str.length();
for (int i = 0; i < length/2; i++) {
if (str[i] != str[length - i - 1]) {
return false;
}
}
return true;
}
In this example, we have declared a function called 'isPalindrome' that takes a string parameter ('str'). The function then compares the characters in the string to check if it is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. The function returns 'true' if the string is a palindrome, and 'false' otherwise.
70 videos|45 docs|15 tests
|
|
Explore Courses for EmSAT Achieve exam
|