passing constant to function

To add comments, please log in or register
Bill Morris
287
Bill Morris  
int myFunc(int& myParameter)
   {
      int iVal;
      // my code
      return(iVal);
   }

I want myParameter to be passed by reference, I would also like it to be optional, but I accept that I can't have it all.

When I don't need to pass a value to myParameter I try this "myVal = myFunc(0)" and I get an error "incompatible types".

Am I right that I can't pass a constant to a Formal Parameter? I have to create a variable 1st "int i" & "myVal = myFunc(i)"?

Simon Gniadkowski
17683
Simon Gniadkowski  
FoxGuy:


Am I right that I can't pass a constant to a Formal Parameter? I have to create a variable 1st "int i" & "myVal = myFunc(i)"?

By constant I assume you mean number ? you can pass a number to a function, e.g. Sleep(1000), sleeps for 1 sec.

Make myParameter a Globally defined variable then you don't have to pass it at all . . .

Bill Morris
287
Bill Morris  
RaptorUK:

By constant I assume you mean number ? you can pass a number to a function, e.g. Sleep(1000), sleeps for 1 sec.


In my example it was a number because I specified "bool myFunc(int& myParameter)" in the function. In other functions it might be "bool myFunc2(string& myParameter)".

Do you have any idea why I got an error when I tried to call myFunc with "result = myFunc(0)", but I didn't get an error when I called it with "result = myFunc(SomeIntVariable)"

Simon Gniadkowski
17683
Simon Gniadkowski  
FoxGuy:

In my example it was a number because I specified "bool myFunc(int& myParameter)" in the function. In other functions it might be "bool myFunc2(string& myParameter)".

Do you have any idea why I got an error when I tried to call myFunc with "result = myFunc(0)", but I didn't get an error when I called it with "result = myFunc(SomeIntVariable)"

Yes, if you are passing a variable by reference then you have to pass a variable . . . a function can't return an array, so how do we get a function to work on an array and return it ? we pass it by reference.
Bill Morris
287
Bill Morris  
RaptorUK:
Yes, if you are passing a variable by reference then you have to pass a variable . . . a function can't return an array, so how do we get a function to work on an array and return it ? we pass it by reference.

All right, I think I see some logic there. To make sure, do you agree with this conclusion?

If a function is receiving a parameter by reference, it must have a variable to put the value into to send back to the calling program. Passing a constant confuses the compiler because there's nothing to put the value into. Just having a local variable isn't good enough, it has to have someplace to pass the value back to the caller.

Simon Gniadkowski
17683
Simon Gniadkowski  
FoxGuy:

All right, I think I see some logic there. To make sure, do you agree with this conclusion?

If a function is receiving a parameter by reference, it must have a variable to put the value into to send back to the calling program. Passing a constant confuses the compiler because there's nothing to put the value into. Just having a local variable isn't good enough, it has to have someplace to pass the value back to the caller.


Essentially, yes.
To add comments, please log in or register