Introduction to User-Defined Function and its Requirements

Returning Values from a Function

Learning about returning values will clear a lot of basics in the C++ language. The void keyword specifies that the function should not return a value. Moreover, if we want the function to return a value, we have to use a data type (such as int, string, etc.) instead of the void. After that, use the return keyword inside the function provided.

returning values

Example

                    

int myFunction(int x) {

return 5 + x;

}

int main() {

cout << myFunction(2);

return 0;

}

// Outputs 7 (5 + 2)

This example returns the sum of a given function with 2 parameters:

Example

                    

int myFunction(int x, int y) {

return x + y;

}

int main() {

cout << myFunction(5, 2);

return 0;

}

// Outputs 7 (5 + 2)

We can also store this result as a variable:

Example

                    

int myFunction(int x, int y) {

return x + y;

}

int main() {

int z = myFunction(5, 2);

cout << z;

return 0;

}

// Outputs 7 (5 + 2)

Let’s have a look at a basic function that will return an integer value and a sample program that calls it:

                    

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#include 

// int is the return type

// A return type of int refers to that the function will return some integer value to the caller (the definite value is not stated here)

int returnFive()

{

// the return statement specifies the definite value that will be returned

return 5; // return the specific value 5 back to the caller

}

int main()

{

std::cout << returnFive() << '\n'; // prints 5

std::cout << returnFive() + 2 << '\n'; // prints 7

returnFive(); // okay: the value ‘5’ is returned, but it is ignored since main() doesn't do anything with it

return 0;

}

Output:

Browse more Topics under User-Defined Function and its Requirements

Void Return Values

The functions are not necessary for returning a value. Moreover, to tell the compiler that a function does not return a value, a return type of void is applied. Let’s see at the doPrint() function:

                    

1

2

3

4

5

void doPrint() // void is the return type

{

std::cout << “In doPrint()” << ‘\n’;

// This function does not return a value so no return statement is required

}

This function comes with a return type of void, representing that it does not return a value to the caller. Because it does not return a value, no return statement is required.

Here we have another example of a function that will be returning the void and a sample program that calls it:

                    

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#include <iostream>

// void means the function does not return a value to the caller

void returnNothing()

{

std::cout << “Hi” << ‘\n’;

// This function does not return a value, therefore, no return statement is required

}

int main()

{

returnNothing(); // okay: function returnNothing() is called, no value is returned

std::cout << returnNothing(); // error: this line will not compile.  You’ll need to comment it out to continue.

return 0;

}

FAQs on Returning Values from a Function

Question 1: How does a function returns a value?

Answer: For returning a value back to the caller, we need 2 things. Firstly, our function has to specify what type of value it will return. Moreover, this will be done by setting the function’s return type, which is the type that will be defined before the name of the function.

Question 2: How can we return 2 values from a function?

Answer: We can easily return more than 1 values from a function with the use of the method known as “call by address” and “call by reference”.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.