Table of contents | |
Introduction | |
Defining a Function | |
Passing Parameters | |
Returning Values | |
Conclusion |
C++ is a popular programming language that supports the use of functions to perform specific tasks. A function is a block of code that can be reused multiple times in a program, making code modular and easier to manage. Functions in C++ make programming easier and more efficient.
In C++, a function is defined by specifying its return type, name, and parameters. The general syntax for defining a function is as follows:
return_type function_name(parameters) {
// function body
}
Example:
#include <iostream>
using namespace std;
// Function declaration
void greet();
// Main function
int main() {
// Function call
greet();
return 0;
}
// Function definition
void greet() {
cout << "Hello, World!" << endl;
}
Hello, World!
In the example above, we define a function named 'greet' that takes no parameters and returns nothing. We then call the function inside the 'main' function using 'greet();'. This function prints "Hello, World!" to the console when called.
Functions in C++ can accept parameters to perform specific tasks. Parameters can be of any data type, including integers, floats, doubles, characters, and arrays. When a function is called, values can be passed as arguments to the function's parameters.
Example:
#include <iostream>
using namespace std;
// Function declaration
void greet(string name);
// Main function
int main() {
// Function call
greet("John");
greet("Mary");
return 0;
}
// Function definition
void greet(string name) {
cout << "Hello, " << name << "!" << endl;
}
Hello, John!
Hello, Mary!
In the example above, we define a function named 'greet' that takes a string parameter named 'name'. We then call the function inside the 'main' function using 'greet("John")'; and 'greet("Mary");'. This function prints "Hello, John!" and "Hello, Mary!" to the console when called.
Functions in C++ can also return values to the calling function. The return type of the function should be specified in the function declaration and definition. To return a value, use the 'return' keyword followed by the value to be returned.
Example:
#include <iostream>
using namespace std;
// Function declaration
int square(int num);
// Main function
int main() {
int result = square(5);
cout << "The square of 5 is " << result << endl;
return 0;
}
// Function definition
int square(int num) {
return num * num;
}
The square of 5 is 25
In the example above, we define a function named 'square' that takes an integer parameter named 'num' and returns an integer value that is the square of the input. We then call the function inside the 'main' function using 'int result = square(5)'; and output the result using 'cout << "The square of 5 is " << result << endl;'. This function returns the square of the input number.
#include <iostream>
using namespace std;
// Function declaration
double area(double radius);
// Main function
int main() {
double r = 5.0;
double result = area(r);
cout << "The area of the circle with radius " << r << " is " << result << endl;
return 0;
}
// Function definition
double area(double radius) {
return 3.14159 * radius * radius;
}
The area of the circle with radius 5 is 78.5398
2. Write a function in C++ to check if a number is even or odd.
#include <iostream>
using namespace std;
// Function declaration
bool isEven(int num);
// Main function
int main() {
int n = 5;
if (isEven(n)) {
cout << n << " is even." << endl;
} else {
cout << n << " is odd." << endl;
}
return 0;
}
// Function definition
bool isEven(int num) {
if (num % 2 == 0) {
return true;
} else {
return false;
}
}
5 is odd.
Functions in C++ are an essential tool for writing efficient, modular, and readable code. They allow developers to write code that is reusable and easier to manage. C++ supports various types of functions, including those that return values, take parameters, or both. By understanding the syntax and usage of functions in C++, beginners can write more complex and useful programs.
70 videos|45 docs|15 tests
|
|
Explore Courses for Software Development exam
|