C++Introduction to C++Learn C++

Learn About Void and Return Type in Functions in C++

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.

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.

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;

We can use this integer function in our function as in this full example,

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

We can use this float function in our function as in this full example,

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,

We can use this string function in our function as in this full example,

here in the last line, instead of declaring and setting the appinfo variable, you can directly use function as below like a string statement,

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,

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.

Get started building powerful apps with C++Builder!


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?