0

Pls wat is return, when is it used, and y can a single coding ve many return. Some return 20, return 0, return ster... Need help

I need the concept of the return. what does it do when it is used. thank u.

14th Dec 2016, 4:18 PM
Michael Dogbey
Michael Dogbey - avatar
4 odpowiedzi
+ 6
What a function/method returns isn't output to the screen. Calling a function is like shorthand for the code inside the function. All of it is reduced to 1 value in the end. Ex: somefunc(10, 37) is the exact same as if you wrote 47 in its place. If a function returns something, you can store the result in a variable like this: sum = somefunc(x, y); or print it out: cout << somefunc(x, y) << endl;
14th Dec 2016, 6:04 PM
Tamra
Tamra - avatar
+ 5
When you create a method, it can return a value of a certain type. If it makes a calculation with some inputs, it can send back the result. It's possible for a method to have multiple return statements, depending on conditions in the code. But once any one of those return statement is run, the method has ended, and control returns to where the method was called. If you create a method like: int sumArray(int[] inArr) { int sum = 0; //your code return sum; } it will have to return an int. In this case, it returns the sum of an array's values. But it can return any type, like double, char, string, even void. If you create a method with return type void, it does not return a value after doing its action. However, it's sometimes a convention to write return; at the end anyway.
14th Dec 2016, 4:59 PM
Tamra
Tamra - avatar
0
@Tamra I tried make a int function and I also put return sum; in the function but it outputs not output.. int somefunc(int x,int y) { int sum = x + y; return sum; } it will output the sum of I use the cout .. and I tried remove the return and it doesn't have any errors..so what is the return actually do ??
14th Dec 2016, 5:53 PM
Fluffy Rabbit
Fluffy Rabbit - avatar
0
or is it doesnt work on this playground ??
14th Dec 2016, 5:56 PM
Fluffy Rabbit
Fluffy Rabbit - avatar