Question:
What is meant by "return" value in c++?
shaista_jabeen_pakistani
2011-06-26 10:30:14 UTC
I can't understand what is meant by return in c++, also please tell me the meaning of return value. thanks.
Six answers:
?
2011-06-28 23:26:34 UTC
To understand the meaning of "return", one should first know the concept of functions and procedures.



What is a procedure?



Procedure is a set of instructions which is executed to get a desired result without being called.All the computer programs are procedures.

Procedure do not return value.



What is a function?



Functions are the procedures which return a value.





What is meant by "Returning a Value"?



Returning a value means giving a output.





Check Example



#include



int add(int a,int b) //function declared and defined

{

int c;

c=a+b;

return c;

}



void main()

{

int d;



d=add(2,3)



cout<<"The sum is "<
}





in the above example, there is a integer type function"add" is declared.



Let us understand the algorithm...



complier starts reading the program from void main



it declares variable d of integer type.

then it sees that function add is called as the value assigned to d.

it goes to the int add function.

adds the two numbers(2 and 3) and stores its value in c.



WHEN IT READS THE "RETURN C" COMMAND IT REPLACES ADD(2,3) IN "d=add(2,3)" with value of 'c'.



Clearly, functions takes the values of 'a' and 'b' as inputs; processes them (c=a+b) and returns the value of 'c' to the main function as output.....









I think my answer will be helpful to you.



Regards,

Siddharth
Connor Anthony
2011-06-26 11:19:10 UTC
In most Object-Orientated languages (and other language types) a function returns a value. eg.

char function( parameters ) {

... // the function will do something with the parameters

return output; // and after it is done, it will return or 'give back' a value

}



in C++, you can tell what a function will return by looking at the first line.



>>> CHAR <<< function(parameters){...}; this function MUST return a variable of the same type as 'char'.



Now, to understand the reason behind 'returns' in layman terms, you can think of it as a TRADE.

If you were to go to a market, and you wanted to by a 2 pack of bubble gum, think of the return type as 'gum' and the parameter as 'money'. To get the gum, you must give the clerk the money to buy the gum. this is how the function would look:



GUM clerk ( money ){

clerk counts money to make sure it is the write amount

clerk hands you bubblegum <-- this is the return line. it would look like 'return bubblegum'

}



That is why there are return values. When you hand the clerk the money, wouldn't you expect what you paid for back? Sorry if this wasn't that clear.
?
2011-06-26 14:40:56 UTC
Have you done basic algebra? In math, you input an x-value into a function and the output is the y-value. In programming, the arguments you supply when calling the function are like the x-value, and what the function returns is the y-value. If you are unfamiliar with basic algebra, I'd suggest you learn some before worrying about programming.
?
2011-06-26 10:36:23 UTC
if a function is called, the return value is "returned" back in the main program/method that calls the function

so like

void main function

add(someint,anotherint);

IE it adds the two ints.

inside that add function has a

return addedint;

which turns the add(someint,anotherint) to addedint



well, just think of it as the final piece that makes the function work.
2011-06-26 11:11:53 UTC
Return statement

The return statement stops execution and returns to the calling function. When a return statement is executed, the function is terminated immediately at that point, regardless of whether it's in the middle of a loop, etc.



Return optional in void functions

A void function doesn't have to have a return statement -- when the end is reached, it automatically returns. However, a void function may optionally contain one or more return statements.

void printChars(char c, int count) {

for (int i=0; i
cout << c;

}//end for



return; // Optional because it's a void function

}//end printChars
2011-06-26 22:37:07 UTC
In simple words.. Return value is the value thrown by a function after it gets executed.





#include

int main() // it means the value thrown by this function would be an integer type

{

cout<<"Hello word";

cout<
return 0; // now this means we are not returning anything or just 0;

}







now one more example......



#include

void main() // it means the function is not throwing(returning) any value..

{

cout<<"Hello word";

return 0; // now we don't need this line..

}





other example...



#include



int fun() // any function of type int (it will return int value)

{

cout<<"return something";

return 2;

}



void main() // it means the function is not throwing(returning) any value..

{

cout<<"Hello word";

cout<
cout<< fun();

}





now output will be...



Hello world

return something

2







//hope it helped you........


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...