In C++, return types specify the type of value that a function returns after performing its task. Functions in C++ can return various types of values, including built-in data types, user-defined data types, pointers, and references. Understanding return types is crucial for designing functions that effectively interact with other parts of a program.
In C++, functions can return basic data types such as integers, floating-point numbers, characters, and booleans. Here’s an example demonstrating functions with different return types
Note – We can return any data type from a function , already defined and user defined as well , here we have discusses basic data types and user defined data types , we will go in more detail when we came to know pointers , arrays then we will se how to mix them with functions and return them as return type.
#include
// Function returning an integer
int add(int a, int b) {
return a + b;
}
// Function returning a floating-point number
double divide(double x, double y) {
return x / y;
}
// Function returning a character
char getGrade(int score) {
if (score >= 90) return 'A';
else if (score >= 80) return 'B';
else if (score >= 70) return 'C';
else if (score >= 60) return 'D';
else return 'F';
}
int main() {
std::cout << "Sum: " << add(5, 3) << std::endl; // Output: Sum: 8
std::cout << "Result: " << divide(10.0, 3.0) << std::endl; // Output: Result: 3.33333
std::cout << "Grade: " << getGrade(85) << std::endl; // Output: Grade: B
return 0;
}
// output //
Sum: 8
Result: 3.33333
Grade: B
In this example, the add
function returns an integer, the divide
function returns a floating-point number, and the getGrade
function returns a character.
add
function adds the two integers 5
and 3
, resulting in 8
.divide
function divides 10.0
by 3.0
, resulting in approximately 3.33333
.getGrade
function determines the grade based on the score 85
and returns 'B'
.Note – we will cover this section in more detail when we get know more about struct if you are coming from C language then you can relate more.
C++ allows functions to return user-defined data types, including structures, classes, and enumerations. Here’s an example demonstrating a function returning a user-defined struct
#include
// Define a struct representing a point in 2D space
struct Point {
int x;
int y;
};
// Function returning a Point
Point createPoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
return p;
}
int main() {
Point p = createPoint(3, 4);
std::cout << "Point: (" << p.x << ", " << p.y << ")" << std::endl; // Output: Point: (3, 4)
return 0;
}
// output //
Point: (3, 4)
In this example, the createPoint
function returns a Point
structure containing coordinates (x, y)
.
createPoint
function creates a Point
object with coordinates (3, 4)
and returns it.main
function, the returned Point
object is assigned to the variable p
.std::cout
statement then prints the coordinates of the Point
object as (3, 4)
.Understanding return types in C++ is essential for designing functions that interact effectively with other parts of a program. By mastering the concepts discussed in this chapter, programmers can write functions that return various types of values, including built-in data types, user-defined data types, pointers, and references, thus enhancing the flexibility and usability of their code.Happy coding! ❤️