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.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?