If after you have tried to research your question and have studied the relevant links you still do not understand the question, provide some of the details of where you have already looked and ask the specific questions.

Re: What is return used for other than cout?

Do you realize that you actually answered your own question inside of your own question?

Lets say I have some function: int fib(int n) What it does is not important at this point I know that it is a function that "returns" and integer.

So I can test it with this:cout << fib(0) << endl;

Now those fancy << things are called operators. Specifically they are the left shift operators or operator<<(). So when you have operators and operands together in a syntax-correct string you have an expression.

So above I have used: cout, fib(0), and endl together with some operator<<() to form an expression.

So from the expression above we have learned that we can use return values within expressions (at least cout expressions).

Now if we look at the implementation of fib we might learn a little more:

int fib(int n) { return (n < 2) ? 1 : fib(n-2) + fib(n-1); }

again it is not really important to know what the function fib is doing, but if we look we see that the function fib is once again used within an expression. Unlike the expression with cout which used side-effects to display the value of fib(0) -- this expression seem to use the returned value of fib(n-2) and fib(n-1) in some arithmetic calculation.

So the return values of functions can be used in expressions. In fact a function call is a kind of expression itself. So we can use the return value of a function in the same way that we use expressions.

So when you use a return value with cout you are using the function with in an expression. The expression used the result of the function call -- this result is the return value -- to do some work with the value. That work might be testing a condition like the condition in a if statement or for-loop (see the use of the whileLessThan() function above), that work might be assigning a value for later use, it might be use in formatting an output, it might be the output.

exactly what you do with a return value is really kind of up to you and your program design. But what it allows you to do is leverage functions to break your program up into logical pieces of functionality.

Re: What is return used for other than cout?

Posted 15 August 2011 - 02:30 AM

JackOfAllTrades, on 14 August 2011 - 08:05 PM, said:

Did you even BOTHER to read the tutorials in my signature to which I pointed you in an earlier topic?

It irritates me, more than you would expect, to see you angry with me because I have not finished reading your two-part tutorial since I am currently teaching myself C so that I can understand your code examples without any doubts in the back of my mind . . . But thank you for writing them anyway and thank you for your concern.

@Jim, I'm not being lazy (you imply that I am). Whenever I try to load an MSDN website, the connection of my internet times out (I'm looking into it). I read structure of a program but did not understand it very well. I did not read "The return statement C++" though . . . Yeah, I'm a piece of crap. It helped alot, so I'll give you rep. Also, the notes were helpful so thanks.

Re: What is return used for other than cout?

Posted 15 August 2011 - 05:08 AM

Ok.

By the way, I'm currently learning on learncpp.com and I'm reading Sams Teach Yourself C++ and C++ for Dummies. I also occasionally read the C++ language tutorial on cplusplus.com, although I much prefer my books and learncpp.com.

Re: What is return used for other than cout?

Posted 15 August 2011 - 06:00 AM

hulla, on 15 August 2011 - 04:30 AM, said:

I'm not being lazy (you imply that I am).

Quote

I read structure of a program but did not understand it very well.

I am not implying that you are lazy. I am implying that you have not mastered your researching skills. When you do not understand something from a particular WEB site show the link and explain what you don't understand. You may also consider looking at the C++ Language Tutorial when you don't understand something in the Learn C++ tutorial, the added explanation and examples may help clarify the problem. Also always enter, compile and run the examples, if you don't understand an aspect of the program then try changing the program and see what happens. Programming is not usually something you can learn straight from a book, practice, practice, practice.

Quote

Whenever I try to load an MSDN website, the connection of my internet times out (I'm looking into it).

I find the MSDN website to be very good at explaining Microsoft specific queries but for general assistance I prefer to start with the C++ Reference, or Google the library function. The Microsoft site is sometimes very specific to the Microsoft tools and their specific library implementation. Also if you are using the Microsoft tools and you get a compile or link error that you don't understand then you may want to Google the specific error message and reading MSDN page explaining the error may offer clues as to what is causing the problem.

Again I am not trying to imply that you are stupid or lazy, you just need to realize that there are many other places to get information. Also when you do your internet searches, remember that the first link may not be the best link, you may need to refine your search criteria.

But again if you do not understand a concept from a given source, state the source, page and paragraph. And then explain what you do not understand. If it is a book or something that may not be widely available you may need to post a quote from that source. If you don't understand parts of JackOfAllTrades tutorial then state what part of the tutorial, and quote the link, asking specific questions. By showing this information it will do two things, first we might be able to rephrase some of the material to make it more understandable to you. Second it will definitely show that you are putting effort into the subject.

Re: What is return used for other than cout?

Posted 15 August 2011 - 07:24 AM

POPULAR

Actually hulla I think you have asked some excellent questions that often cut to the heart of a matter nicely.

But when it comes to beginner questions: Sometimes we do get a little tired of answering the same questions over and over. So sometimes you may get some "RTFM" kind of responses when you ask a question that should definitively be covered "In the FM" (whatever the "FM" might be).

Also Jim makes a very good point about research. A big part of being a programmer is doing research. Researching errors, researching techniques, researching theory... we really do read quite a bit.

Also - have you ever tried to teach someone who always interrupts with a question? Sometimes it is not always necessary to grok every nuance of a code example when you see it, because often the questions will be answered in a later topic.

In teaching computers this is often important. We have to use some things before we can really go into detail about what they are and why they are there. So think of it as taking a tour in a new city. If you ask questions about every new thing you see the tour does not get very far, but if you take notes and follow along often most of your questions get addressed OR you pick up on it along the way.

so for example in C you learn how to display the string "Hello World" long before you know what a string is, before you know what an array is, before you know what a "zero termination char array" is , before you know what a literal is, before you know what a const char is etc. So believe it or not, it may actually be years before you really understand what is going on inside the very first program you ever saw... because there are layers upon layers and you only are meant to see the surface.