• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

beginner needs help

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
digmont
Asked:
digmont
  • 3
  • 3
1 Solution
 
Infinity08Commented:
>> 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
 
Infinity08Commented:
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
 
elqnoCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
digmontAuthor Commented:
  why return should be written in program ??

without return what will happen ??
0
 
Infinity08Commented:
>>   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
 
elqnoCommented:
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
 
elqnoCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now