What will happen if a Function is tried to return more than one value at a time?
There are often cases of using return statements while dealing with function calls. Generally, only one thing is returned whether it’s a primitive data type like integer, character, etc, or a non-primitive data type like an array, string, vector, etc depending upon the function’s return type.
But what will happen if we try to return more than one value via return statements?
This article focusses on discussing the scenario when more than one values are returned via return statements.
Predict the Output:
The value of k = 300
Most of you must be wondering the output of the above code will an “Error” because in the above code, more than allowed values are being returned to the function call.
In the cases like this when multiple values are returned without taking special precaution like array, pointers, structures, references, tuple, classes and objects, then in that case the last value is returned and all the values before that are simply ignored.
The number of returning values can be many but only the last occurring will be returned and also one can also ignore the brackets or braces “( )” and can write without braces just by separating the multiple values by a comma as shown in the below code:
The value of k = 0
Here the output is 0 because the last variable t which is equal to c % d = 0. So when multiple values are returned then the value of t gets assigned to the variable k.