?
Solved

beginner needs help

Posted on 2009-04-17
7
Medium Priority
?
272 Views
Last Modified: 2012-05-06
hi i m digmont i m a beginner in C++ and i cant understand "return" statment
 
please explain it with code

suppose i have a class named students
 
i made one function "marks"
 
i entered marks of three subject in it and the variable suppose "T" in which i have stored the total marks and now if i would return "T"  and i want to use same "T"  in other functions how will
it works??

Open in new window

0
Comment
Question by:digmont
  • 3
  • 3
7 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 24166460
>> and i cant understand "return" statment

A return statement returns the result of a function to the calling code.

So, for example :
int fun(void) {              // <--- this function returns a variable of type int
  return 5;                  // <--- the actual value it returns is 5
}
 
// calling code :
 
int v = fun();              // <--- the function is called, and the returned value is stored in the variable 'v'
std::cout << v;             // <--- this shows 5

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24166470
You might also be interested in this basic C++ tutorial about functions :

        http://www.cplusplus.com/doc/tutorial/functions/
        http://www.cplusplus.com/doc/tutorial/functions2/
0
 

Expert Comment

by:elqno
ID: 24166560
i don't really understand what you want, but i will answer as i understood you and as i would do it:

class student {
   private:
      int totalMarks;
   public:
      student() {
         totalMarks = 0;
      }
      void mark() { totalMarks++; }
};

void main() {
   student stu;
   stu.mark();
}

the code is useless so far, but i guess that if you add more functions or more variables to that class might start to become usefull, if you can please write more about what is your problem, because i really can't understand you.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:digmont
ID: 24168762
  why return should be written in program ??

without return what will happen ??
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24169242
>>   why return should be written in program ??

If you want to return something from a function, you need a return statement. If it's a void function (ie. it doesn't return anything), you don't need a return statement.
0
 

Expert Comment

by:elqno
ID: 24197978
Infinity08 is right for example if you have a function that makes the adding of 2 numbers and you don't want to put the "cout" inside the function, the only way is returning that value, I will write a small code about a function that takes 2 integers and returns the result, you must remember that the Data Type beside the function's name is what you are returning, if it is a int, you Must return an int value, if not you won't be able to compile the program.

The return is used in the main function, i have worked in dev-c++ and i know that it doesn't allow you to use a void main, you Must use int main(), i think the same happens in vc++ from microsoft, but i'm not sure right now...
int fun(int a, int b) {
   return(a+b);
}
 
void main() {
   cout << fun(2,3);
   return 0;
}

Open in new window

0
 

Expert Comment

by:elqno
ID: 24198026
Infinity08 is right for example if you have a function that makes the adding of 2 numbers and you don't want to put the "cout" inside the function, the only way is returning that value, I will write a small code about a function that takes 2 integers and returns the result, you must remember that the Data Type beside the function's name is what you are returning, if it is a int, you Must return an int value, if not you won't be able to compile the program.

The return is used in the main function, i have worked in dev-c++ and i know that it doesn't allow you to use a void main, you Must use int main(), i think the same happens in vc++ from microsoft, but i'm not sure right now...
int fun(int a, int b) {
   return(a+b);
}
 
int main() {
   cout << fun(2,3);
   return 0;
}

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question