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!

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Use The Comprehensive Form Properties In C++ VCL Apps

C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Add Characters to Wide Strings in Modern C++

C++ComponentsLanguage FeatureLearn C++

How To Create Gorgeous Transparent App Windows With C++

C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Get A Substring of a Wide String in C++

en_USEnglish