We explained how to declare and define simple functions. They had no return, and in a previous post we explained that main function has an integer return that represents our code is successfully done or not. In C++ programming language, we can easily define many functions and some functions may have return values some may not. We can return single variables like integer numbers (int), floating numbers (float), text (string) or we can return multiple variables, vectors, structures, class objects or pointers too.
All return types are defined before the functions’ name. If we have no return value, we use the void keyword, that means this function has no return value, it is a void function.
1 2 3 4 5 6 7 |
void myfunction() // this is the function declaration, return type is void { // the definition of the function (statements and other functions) // no return statement here because it is a void function } |
If we remember info() sample from a previous posts, it had no return value, so here we can use the void to define this function. In some compilers, if there is no return in our function void type should be used, otherwise we may have error or warning.
1 2 3 4 5 6 7 8 9 |
void info() { std::cout << "This is my application.\n"; std::cout << "Here is my information.\n"; std::cout << "Developed in 2021 by My Company\n"; std::cout << '\n'; } |
Table of Contents
An Integer Function Example
We can define return type as a integer value and we return that value in our function. Here return value (result) expected to be same as defined return type, See example below;
1 2 3 4 5 6 7 |
int calculate() // return type is int { int result = 500+3*100; return result; // return value is int } |
We can use this integer function in our function as in this full example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> int calculate() { int result = 500+3*100; return result; } int main() { int height = calculate(); std::cout << "vertical distance at 0.5 seconds for an object drop is " << height << '\n'; } |
In accordance with the limits of returning value we can use bool, char, int, short int, unsigned short int, long int, unsigned long int, long long int, unsigned long long int as a return type of this function and return value of this function. For more information please see details about data types.
A Float Function Example
1 2 3 4 5 6 7 |
float calculate() // return type float { float result = 0.5*9.81*0.5*0.5; // (1/2)(gt^2) return result; // return value float } |
We can use this float function in our function as in this full example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> float calculate() { float result = 0.5*9.81*0.5*0.5; // (1/2)(gt^2) return result; } int main() { float height = calculate(); std::cout << "vertical distance at 0.5 seconds for an object drop is " << height << '\n'; } |
Instead of float return type and return value, we can use double too.
A String Function Example
we can use string with using namespace std; or we can use std::string to define string functions. This info() function example below, returns information about the application in string format,
1 2 3 4 5 6 7 |
std::string info() // return type is string { std::string str = "This is My Application and developed by My Company !"; return str; // return value is string } |
We can use this string function in our function as in this full example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> std::string info() { std::string str = "This is My Application and developed by My Company !"; return str; } int main() { std::string appinfo; appinfo = info(); // calling a string function std::cout << appinfo << '\n'; // we may directly call info() here too } |
here in the last line, instead of declaring and setting the appinfo variable, you can directly use function as below like a string statement,
1 2 3 |
std::cout << info() << '\n'; |
A Structure Function Example (Returning Multiple Variable Types)
In some cases we can return multiple variables by using structures which has formed with these multiple variable types. In example below we have a structure for robot information with multiple variables and our function sets and returns these multiple variables,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <string> struct mystruct { int id; float x,y; std::string name; }; struct mystruct robotinfo() { struct mystruct result; result.id = 1; result.x = 15.5; result.y = 20.2; result.name = "Robot Model One"; return result; } int main() { struct mystruct robot = robotinfo(); std::cout << "Robot Params:" << robot.name << ',' << robot.id << ',' << robot.x << ',' << robot.y << '\n'; getchar(); } |
Functions are very useful to transfer data obtained inside. We can return any types from a function (vectors, classes etc..) , we can also return pointers. Note that pointer parameter should be allocated inside the function or before the function used, at the outside or by another function.