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.

Related posts
C++Learn C++Videos

Learn C++ With Monoids, Monads, and Applicative Functors: Repeated Software Patterns (CPPCon 2020) Video

C++Learn C++Videos

Learn C++ With Pipes: How Plumbing Can Make Your C++ More Expressive by Jonathan Boccara (CPPCon 2020) Video

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

Learn Classes and Objects in C++

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

Learn about Access Specifiers in C++ Classes