A function is a reusable block of code designed to perform a specific task. Functions help divide a large program into smaller, organized, and reusable parts.
A function is a named block of code that performs a particular task.
void greet() {
std::cout << "Hello";
}
Here, greet() is a function that displays a message.
Functions provide several advantages:
returnType functionName() {
// statements
}
For example:
void display() {
std::cout << "Welcome";
}
Here, void is the return type and display is
the function name.
A function declaration tells the compiler about a function before it is used.
void greet();
This is also called a function prototype.
The function definition contains the actual statements that the function executes.
void greet() {
std::cout << "Hello World";
}
The code between the braces is the function body.
A function does not execute just because it is defined. It must be called.
#include <iostream>
void greet() {
std::cout << "Hello World";
}
int main() {
greet();
return 0;
}
The statement greet(); calls the function.
void greet() {
std::cout << "Hello";
}
int main() {
greet();
std::cout << " World";
return 0;
}
The execution is:
main().greet() is called.greet() execute.main().main() executes.
The void return type means that the function does not
return a value.
void message() {
std::cout << "Welcome to C++";
}
This function performs an action but does not return a result.
A function can return an integer using the int return type.
int getNumber() {
return 100;
}
Calling the function:
int number = getNumber();
std::cout << number;
Output:
100
double getPrice() {
return 99.50;
}
int main() {
double price = getPrice();
std::cout << price;
return 0;
}
A function can return values of different data types.
int add() {
int a = 10;
int b = 20;
return a + b;
}
int main() {
int result = add();
std::cout << result;
return 0;
}
The function calculates and returns the sum of two numbers.
Parameters allow us to pass data into a function.
void greet(std::string name) {
std::cout << "Hello " << name;
}
The function can be called as:
greet("Rahul");
Output:
Hello Rahul
int add(int a, int b) {
return a + b;
}
int main() {
std::cout << add(10, 20);
return 0;
}
The values 10 and 20 are passed to the parameters a and
b.
void greet() {
std::cout << "Hello" << std::endl;
}
int main() {
greet();
greet();
greet();
return 0;
}
The same function can be called multiple times.
int multiply(int a, int b) {
return a * b;
}
int main() {
int result = multiply(5, 4);
std::cout << "Result = "
<< result;
return 0;
}
Output:
Result = 20
bool isEven(int number) {
return number % 2 == 0;
}
int main() {
if (isEven(10)) {
std::cout << "Even number";
}
return 0;
}
The function returns either true or false.
int maximum(int a, int b) {
if (a > b) {
return a;
}
return b;
}
int main() {
std::cout << maximum(25, 40);
return 0;
}
The function returns the larger of the two numbers.
#include <iostream>
void greet();
int main() {
greet();
return 0;
}
void greet() {
std::cout << "Hello World";
}
The declaration allows the function to be called before its definition.
#include <iostream>
void greet() {
std::cout << "Hello World";
}
int main() {
greet();
return 0;
}
If the function definition appears before main(), a separate
declaration is not necessary for this simple example.
int getAge() {
return 20;
}
int main() {
int age = getAge();
std::cout << "Age = "
<< age;
return 0;
}
A function can have no parameters and still return a value.
int square(int number) {
return number * number;
}
int main() {
int result = square(6);
std::cout << "Square = "
<< result;
return 0;
}
This function accepts a number and returns its square.
int factorial(int number) {
int result = 1;
for (int i = 1; i <= number; i++) {
result *= i;
}
return result;
}
int main() {
std::cout << factorial(5);
return 0;
}
The function uses a loop to calculate the factorial and returns the result.
void table(int number) {
for (int i = 1; i <= 10; i++) {
std::cout << number
<< " x "
<< i
<< " = "
<< number * i
<< std::endl;
}
}
int main() {
table(7);
return 0;
}
A function can contain loops and other programming statements.
void message() {
std::cout << "Welcome";
}
void display() {
message();
std::cout << " to C++";
}
int main() {
display();
return 0;
}
One function can call another function.
int add(int a, int b) {
return a + b;
}
int main() {
int x, y;
std::cout << "Enter first number: ";
std::cin >> x;
std::cout << "Enter second number: ";
std::cin >> y;
std::cout << "Sum = "
<< add(x, y);
return 0;
}
The user provides values, which are passed to the function.
int add(int a, int b) {
return a + b;
}
The arguments passed to the function should match the expected parameters appropriately.
#include <iostream>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int main() {
int a = 20;
int b = 5;
std::cout << "Addition = "
<< add(a, b)
<< std::endl;
std::cout << "Subtraction = "
<< subtract(a, b)
<< std::endl;
std::cout << "Multiplication = "
<< multiply(a, b)
<< std::endl;
return 0;
}
Different functions perform different calculator operations.
#include <iostream>
char getGrade(int marks) {
if (marks >= 90) {
return 'A';
}
else if (marks >= 75) {
return 'B';
}
else if (marks >= 60) {
return 'C';
}
else if (marks >= 40) {
return 'D';
}
return 'F';
}
int main() {
int marks;
std::cout << "Enter marks: ";
std::cin >> marks;
std::cout << "Grade = "
<< getGrade(marks);
return 0;
}
The function accepts marks and returns a grade character.
int calculateTotal(int price, int quantity) {
return price * quantity;
}
| Concept | Meaning |
|---|---|
| Function | A reusable block of code that performs a specific task. |
| Declaration | Tells the compiler about a function. |
| Definition | Contains the actual function code. |
| Call | Executes the function. |
| Parameter | A variable that receives data passed to a function. |
| Argument | A value passed when calling a function. |
| Return Type | Specifies the type of value returned by a function. |
| void | Indicates that the function does not return a value. |
returnType functionName(parameters) {
// statements
}
void means the function does not return a value.Question: What is the main purpose of a function in C++?