12 Replies - 939 Views - Last Post: 15 August 2011 - 07:24 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

What is return used for other than cout?

Posted 14 August 2011 - 02:38 AM

What is return used for other than couting a function?

Thanks in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: What is return used for other than cout?

#2 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: What is return used for other than cout?

Posted 14 August 2011 - 02:51 AM

when writing functions such as the one in the following code, you can split up the load of you program and take larger problems and split them down into smaller simpler ones.


for the following code assume i have an employee class


#include <iostream>//used t write strings to the console
#include <string>//for strings
#include <ifstream>//to read data

using namespace std;

employee loadEmployee(string name)
{

ifstream ELoc;
ELoc.open("path//to//employee//name");

int maxHours;

employee mE=new employee;

ELoc>>mE.maxHours;
//continue "ELoc>>" until you have all the data you need

return mE;
}

void main()
{

employee testEmployee=loadEmployee("Test");

cout<<testEmployee.maxHours;

}




does that help or just confuse you more?

~Xtreampb~
Was This Post Helpful? 1
  • +
  • -

#3 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: What is return used for other than cout?

Posted 14 August 2011 - 02:56 AM

It helps

Nice signature by the way :D
Was This Post Helpful? 0
  • +
  • -

#4 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: What is return used for other than cout?

Posted 14 August 2011 - 03:04 AM

thx i like you avatar
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: What is return used for other than cout?

Posted 14 August 2011 - 05:05 AM

Did you even BOTHER to read the tutorials in my signature to which I pointed you in an earlier topic?
Was This Post Helpful? 1
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,566
  • Joined: 25-December 09

Re: What is return used for other than cout?

Posted 14 August 2011 - 05:50 AM

Definitely read the links in JackOfAllTrades signature.

You also need to learn how to research these "basic" questions. By learning to use the internet search engines you should be able to get the answers to these "basic" questions in a fraction of the time it takes you to ask the question and get an answer in a forum. Look at the first three links for "C++ return" in Google: The return Statement (C++), Structure of a program - C++ Documentation, and Functions (I) - C++ Documentation. And a little further down the list: C++ Notes: Function return Statement.

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.

Jim

This post has been edited by jimblumberg: 14 August 2011 - 05:51 AM

Was This Post Helpful? 2
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: What is return used for other than cout?

Posted 14 August 2011 - 06:28 PM

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.

example:
#include <iostream>
#include <iomanip>

using namespace std;

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

int& inc(int& n) { return ++n; }
bool whileLessThan(const int& n, const int& val) { return n < val; }

int main() {
    for (int n = 0; whileLessThan(n, 10); inc(n)) {
        int value = fib(n);
        cout << setw(value) << fib(n) << endl;
    }
    return 0;
}

output:
> "C:\CProjects\Forum Help\returnValue.exe " 
1
1
 2
  3
    5
       8
           13
                   21
                                34
                                                     55


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.
Was This Post Helpful? 2
  • +
  • -

#8 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: What is return used for other than cout?

Posted 15 August 2011 - 02:30 AM

View PostJackOfAllTrades, 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.
Was This Post Helpful? 0
  • +
  • -

#9 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: What is return used for other than cout?

Posted 15 August 2011 - 03:54 AM

Work through the tutorials immediately. They are vital.
Was This Post Helpful? 1
  • +
  • -

#10 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

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.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,566
  • Joined: 25-December 09

Re: What is return used for other than cout?

Posted 15 August 2011 - 06:00 AM

View Posthulla, 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.

Jim
Was This Post Helpful? 1
  • +
  • -

#12 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: What is return used for other than cout?

Posted 15 August 2011 - 06:45 AM

Wow you can do that for me? Cool. I've never seen anyone ask a question like that before so yeah.
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.
Was This Post Helpful? 5
  • +
  • -

Page 1 of 1