Question:
Am not understanding return concept in programming language?
2014-03-30 01:41:07 UTC
when program executes at the end i heard common statement that the program return some value.... but i dont understand whom to return??? is return to program?? is return to ram?? return means again return a value??? please tell me in simple words,because am not understanding concept return in programming language...and why we return is there any problem if not return because void is no return, what does that mean no return???? TELL ME IN WHY RETURN????IS THERE ANY PROBLEM IF WE NOT RETURN A VALUE IN PROGRAMMING?? or if we return that may affect to any garbage collector???
Four answers:
Bob
2014-03-30 02:18:40 UTC
Most programming languages use a software structure called a stack for calculations. When you write something like a + b, the value of a is pushed onto the top of the stack, then the value of b is pushed onto the stack (so that stack has b then a on it). Then when the computer processes the +, it pops (removes) the values of a and b, adds them, and pushes the answer on the stack.



When you say a = c * d; the following steps occur:

1. the address of a is pushed onto the stack

2. The value of c is pushed on the stack

3. The value of d is pushed on the stack

4. they're both popped, multiplied and the product pushed on the stack

5. the result is popped off the stack, the address is popped off the stack, and the result copied to the location specified by the address. Once this happens, the stack is now empty and ready to use for the next statement.



So what happens when we say a = 5.0 * sin (b+c)?

1. The address of a is pushed

2. The value 5.0 is pushed

3. The value of b is pushed

4. The value of c is pushed

5. b and c are popped, added, and the sum pushed

6. The sin() function is called



And what happens inside sin()?

6.1 The value of b+c is popped off the stack

6.2 The sine of b+c is calculated (probably using the stack)

6.3 The result of sin(b+c) is pushed onto the stack.

6.4 The sin() function finishes

7. The result of sin(b+c) is popped off the stack, 5.0 is popped off the stack, they're multiplied together and the product pushed on the stack.



7. The result of sin() is popped from the stack, 5.0 is popped from the stack, they're multiplied together and the product pushed onto the stack. [The stack now has the result of 5*sin(b+c) at the top, followed by the address of a]

8. The top two values are popped from the stack and the assignment to a is completed.



All functions put their calculated answers on the top of the stack, and the "return" statement is simply what the programmer writes to make this happen. So when you say "return 10;" the value 10 is simply pushed on the top of the stack. There it waits until the next part of the program makes use of it in some way.



What happens if you don't return a value? If you don't return a value then you'll screw up the stack! For this reason, a good compiler always insists that you have a return statement for every function, and will flag an error if you don't.



Stacks are a basic feature in computing and there's stacks of information about them available on the web.
Dan
2014-03-30 03:42:21 UTC
Bob has given you an excellent answer right down to the wire, but perhaps it may help if I answer your questions one at a time with practical answers for a beginning programmer.



"...when program executes at the end i heard common statement that the program return some value.... but i dont understand whom to return???..." Yes, it does. You speak here of the return at the very end of the program, in C / C++ this would be from the function main, which is always used as both the program entry point and program exit point in those languages. Simply put, this is where your program in those two languages starts, the operating system, Ubuntu, Windows, etc; turns execution over to the code after storing it's state safely in the never-never land of RAM. When your program exits, it 'nudges' the operating system and returns, usually a zero, to indicate that it has done what it needs to do and that the operating system can wake up and continue the control of the machine.



"when program executes at the end i heard common statement that the program return some value.... but i dont understand whom to return???" Yes, return to RAM, that part configured, as Bob has said, as a stack. There are also other things floating about out there in the RAM space like heap, free memory areas, bios areas and the protected areas where various programs run. You don't decide that and needn't panic over it. The operating system will put your program where it can operate when it is initialized presuming you have done your part coding it, compiling it and building it in an appropriate manner. As for using return values in C/C++ on other functions besides main, it's up to you. Look at what the function does. Is there some value hanging out in there after it executes that we need to put into a variable for storage, pass to another function as a parameter or output to the screen easily? If yes, then return that value and use the function to move it where you would like it to go. Traditionally, in C language, return values were coded into functions so that operations withing those functions could be tested. An if statement may check that a certain file was opened for reading by the function. If an error was thrown when the code to do this executed, the function exited immediately with some value the programmer dreamed up and documented to mean "Hey, this file couldn't be opened for reading, don't try and read it yet!". That value could be trapped and acted upon by the function receiving it early in its code. With the advent of C++ and the try / throw / catch error controls, the return value is often freed up to be another way to move computed data from function to function inside the program.



"...please tell me in simple words,because am not understanding concept return in programming language...and why we return is there any problem if not return because void is no return, what does that mean no return????..." In simple words, other than the main function (or WinMain if using Windows headers usually), there is no requirement in C / C++ to pass anything anywhere any time by return. It is a stable and convenient feature that you will want to use in some programs as you become more familiar with the languages you're learning. Other languages may have a requirement for using return, I remember long ago you almost never did a function in BASIC offshoots that didn't use a return. It was how you dropped out of the function and went to the next one in line. Back to C / C++, if a function is written to return 'void' under C++, that means you don't even need the return keyword in the function, just code what it should do and close with a brace. In C, the same applies in most compilers in current use. Some early C compilers won't let you because they were non-standard and required an error level return to be made, period. As, I'm sure, Bob said in there some where, it means nothing is stored back on the stack and no value is passed anywhere. There may be or may not be a 'problem' if you don't return from main. It would depend on if the operating system in use requires and expects one from the compiler / language / linker in use or not. That varies, but it isn't a problem because you'll find out about it soon enough if it is a problem. Typically, for an operating system or hardware platform that I know of, C / C++ language *MUST* return a value from the function named main. This is the way it has been done, will be done and is likely to still be done for a very long time, regardless of whether the people who programmed the operating system thought that the error level return mattered or not. Get used to it if you're referring to C/C++.



"...TELL ME IN WHY RETURN????IS THERE ANY PROBLEM IF WE NOT RETURN A VALUE IN PROGRAMMING?? or if we return that may affect to any garbage collector???..." In C/C++ no, because *YOU ARE THE GARBAGE COLLECTOR*! You allocate, load into memory, point to and destroy all values either explicitly or implicitly in your program, the end. If you are referring to those little modern languages like Java (which I don't use!), I would be lying if I said I knew. For me, computers live, run and work on C, C++ and Assembler and I'm fine with that. There is an old saying "Quick, good, cheap pick two." I never liked folks that are silly enough to pick cheap or quick, it all oughta' just be good.



By the way, typing in all caps over the Internet is seen as screaming, a lot of people don't like to be screamed at. You also might have to learn to stop panicking when something you don't know comes up and learn to research it for yourself or get help politely and concisely. You still have never mentioned in what language you are attempting to work, what your real problem is or if this is just a general hypothetical gap in your understanding. Computers have no emotion. If you have to, take a coffee break go where no one can hear you and cuss for ten minutes solid. Then let it go and come back and find out what you need to know to get the job done. It's worked for me for years;-)
2014-03-30 03:29:13 UTC
See, in a simple way.



Every function returns a value.



int function() : returns an integer,

float function() : returns a float,

char function() : returns a character and so on.



by default.



if you are using int main() then main function is assumed calculating and returning and integer value, so it is necessary to return an integer value so that

" compiler should not show error ".



if you don't want to return anything, use void main() , function will be assumed empty function / void function and you won't have to return anything.



When you will study Functions and pointers etc, you will understand what all this is about.



Till then , good luck and keep studying. :)
?
2014-03-30 02:05:15 UTC
YOU NEED TO CHILL.



Also, given your ridiculous tone and user name, you are not getting a serious answer from me. Leave the computer stuff to grown ups, ok?


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