|
From: Ray W. <ra...@in...> - 2001-07-31 06:49:58
|
You can only effectively use one return statement in a function,
whether it is a class function or a regular function. The first
'return' statement will cause the function to terminate and return
back to the program which called it.
To return more than one value from a function you need to use
what's called 'call by reference' parameters; your function would
look like this - void AssignDate(int& month, int& day, int& year)
and then the function defination would follow. The '&' symbol
following the type specification allows the function to modify
the actual values passed to it, so somewhere in your function
defination you would want the month = some_value; day = some_other_value;
and year = still_another_value; then you can
use a return statement without a parameter (since function is void)
to terminate the function and lo and behold the variables you
passed to the function will now have the new values in them.
This use of 'call by reference' as opposed to 'call by value' is not
restricted to class methods or functions, but applies to all
functions. The 'call by reference' passes the address of the
variable to the function so that the actual value may be changed,
while the 'call by value' (which is what you get without the '&' only
passes the value of the passed variable and the function cannot modify the
variable which is passed.
bye, ray w.
At 09:43 PM 7/30/01 +0200, you wrote:
> -----Original Message-----
>From: dev...@li...
>[mailto:dev...@li...] On Behalf Of
>Tre...@ao...
>Sent: Monday, July 30, 2001 8:32 PM
>To: dev...@li...
>Subject: [Dev-C++] Class question
>
> After creating the
>class and compiling my program I get the following error.
>
>warning C4715: 'Date::assignDate' : not all control paths return a value
>
> It is supposed to validate a date entered and display the
>
>
><>
>using namespace std;
>
>class Date
>{
>public:
>Date();
>int assignDate(int, int, int);
>void displayDate();
>
>private:
>int month;
>int day;
>int year;
>};
>
>Date::Date()
>{
>month = 0;
> = 0;
> = 0;
>} //end of default constructor
>
>int Date::assignDate(int m, int d, int y)
>{
>month = m;
> = d;
> = y;
>
>>&&<= 12)
>{
>if (m = 2)
>if (y % 4 == 0)
>>&&<= 29)
>{
>return m;
>return d;
>return y;
>}
>>&&<= 28)
>
>return m;
>return d;
>return y;
>}
>else if (m == 4 || m == 6 || m == 9 || m == 11)
>>&&<= 30)
>{
>
> These 2 are not returned. return y;
>}
> else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m ==
>10 || m == 11)
>>&&<= 31)
>{
> These 2 are not returned. return d;
>return y;
>}
>}//end loop
>else
><<<< endl;
>//end ifs
>
>
>} //end of assignDate function
>
>
>void Date::displayDate()
>{
><<<<<<<<<< year;
> There are probably more errors too, but fix them first.
> * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com *
>Alternative URL: http://run.to/noicys
>
Ray Witter
|