## Recommended Posts

When do you type "return" values in the parameters ( & ) and when do you do it in the functions body { & }? ex:
 //General Methods
static GameEngine*		GetEngine() {return m_pGameEngine;};
BOOL					Initialize(int iCmdShow);
LRESULT				HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);

//Acessor Methods
HINSTANCE GetInstance() { return m_hInstance; };
HWND GetWindow() { return m_hWindow; };



##### Share on other sites
jpetrie    13132
So-called "output" parameters (those passed by reference, as pointers or references, and intended to be used to carry values out of the function) are usually used when a functions actual return value is not sufficient. This might be, for example, if a function has multiple values to return, or if the return value is "optional" (i.e., may not exist).

In general, you should return via the function's return type when you can (it's usually clearer) as the default. If you have to or if there is compelling advantage to using output parameters, use those instead.

##### Share on other sites
Glak    315
I think that there might be some confusion. The & symbol has nothing to do with return values. When it is used in a declaration (a variable declaration or a parameter) it creates a reference. When it is used in as a prefix unary operator is creates a pointer. The two uses are completely unrelated and neither is inherently connected to return values.

##### Share on other sites
please correct me if im wrong, but ive always thought that ( ) is "input" parameters (values i intend to use to calculate the "output" { })
ex.
#include <iostream>int Fordobler(int TalTilFordobling);int main(){   int resultat = 0;   int input;   std::cout << "\nIndtast et tal mellem 0 og "             << "10.000, som skal fordobles: ";   std::cin >> input;   std::cout << "\nInden Fordobler kaldes...";   std::cout << "\ninput: " << input             << " fordoblet: " << resultat << "\n";   resultat = Fordobler(input);   std::cout << "\nTilbage fra Fordobler...";   std::cout << "\ninput: " << input             << " fordoblet: " << resultat << "\n\n";   return 0;}int Fordobler(int original){   if (original <= 10000)      return original * 2;   else      return -1;   std::cout << "Du kan ikke komme hertil!\n";}

##### Share on other sites
Hollower    685
Yes but think about it this way. If the input parameters are not const you can modify their values within the function body. This usually doesn't cause side-effects because they are temporary objects limited to the scope of the function. But if what you "input" is a non-const reference to an outside object, any modifications will be a kind of side-effect or "output" from the function, lasting after the function has ended. Does that make sense?

##### Share on other sites
Glak    315
no. The block { } is definitely not the output of the function. It is the actions taken by the function. The return value is the output.

##### Share on other sites
Here's a brief introduction to a few of the computer science terms you appear to have misunderstood.

Consider the following snippet of code.

#include <iostream>int myFunction(int &A, int B){    A += B;    std::cout << "A: " << A << " B: " << B << std::endl;        return (A * B);}void myCallingFunction(){    int x = 5;    int y = 6;    int z = myFunction(x, y);}

int myFunction(int &A, int B)

This declares a function named myFunction, with a return value of int which takes two arguments or parameters. There are two ways of parameter passing. The first is called pass-by-value whereby the function gets a copy (with a value equal to that of the parameter passed) which is locally scoped and so can be modified - but no changes persist after the function returns (because the original was never modified - only a copy was, and that copy disappears once the function returns). The second is called pass-by-reference - this is where the function gets called with parameters and can modify these - modifying them modifies the original variable passed as a parameter. By default, C++ uses pass-by-value.

int &A

This, however, means that the first parameter passed to the function is passed by reference. This means we can modify it and have the changes persist after the function.

{    A += B;    std::cout << "A: " << A << " B: " << B << std::endl;        return (A * B);}

This is the function body. This defines the procedure which the computer will follow when the function is called. Let's look at it in more detail.

A += B;

This adds B to A. However, because A is passed by reference, it has a side-effect (a change to the process' state) of modifying the original version of A. Because this has effects outside of the scope of our function, this is output.

std::cout << "A: " << A << " B: " << B << std::endl;

This outputs text to a console (most likely your screen). This is also a side-effect, and is therefore also output.

return (A * B);

This returns the int you get by multiplying A by B. This is not a side-effect and does not directly change the process' state.

Side-effects are only permitted in some languages - some functional languages adhere tightly to a mathematically pure form of computation, which does not allow for side-effects.

[Edited by - TheUnbeliever on March 4, 2007 5:29:20 PM]

##### Share on other sites
Glak> isnt the action done inside the { } the return value?
ex:
[source lang ="cpp"]x(){1+2;};

equals x=2+1?
*thanks for clearing this part up TheUnbeliever*
---------------------------------------------------------------------------
can you please explain the use of () and {} for functions? seems like ive missed that part.
in the function
[source lang ="cpp"]...int x,y, z;...cout<< "type two numbers, and i will give the sum << endl;cin >> y, zcout << "result is: " << x <<...int x(int y,z){y+z;};

/sketches
------------
is this true, that i have to write int y, z in the function x's parameters(argument) becouse thats what im going to use in the function(lack of the word) inside the bracets { }?

otherwise, can you please explain the propper use of ( ) and { }, becouse i dont think ive understood it correct (line from 1st post:)
[source lang = "cpp"]GetEngine() {return m_pGameEngine;}; Initialize(int iCmdShow); // call it 2HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam); //call it 3

what is happening inside the () and {} in these 3. Not asking about what HWND hWindow etc does, im interesting in when to put it inside ( ) or {}. I thought that when i then called GetEngine() it would be equal to m_pGameEngine (= GetEngine returning m_pGameEngine). in 2 & 3 im not quite sure whats happening, is Initialize() and HandleEvent() doing the "same thing" as what is written in its parameters (in that case it would be similiar to returning?)
sorry if its hilarius questions, but im still learning the language, and i want to be 100% sure.

##### Share on other sites
KulSeran    3267
int Foo ( int x, int y ){x = x + y;return ( x + y );}int Bar ( int &x, int y ){x = x + y;return ( x + y );}void Moo ( int x, int y ){x = x + y;}int main ( int argc, char **argv ){int x = 2;int y = 3;cout << Foo ( x, y ) << endl;cout << x << endl;cout << y << endl;x = 2; y = 3;cout << Bar ( x, y ) << endl;cout << x << endl;cout << y << endl;//cout << Moo ( x, y ) << endl; // can't do this, since Moo doesnt return anythingMoo ( x, y );cout << x << endl;cout << y << endl;}

What goes in the () is the parameters. This can get very complex if you want, but the simple cases are by-value
and by-refrence. Foo passes both by value, so the parameters are not changed. Bar on the other hand passes x by refrence
and so x will have changed after calling bar (a side effect of the function).

what goes in {} is work to be done in a scope. after a function prototype, means that the {} is the body of the function
and represents the work that is done in the function.

the statement "return" signifies the information that is returned from the function. the type of the statement must match the
type specified in the function prototype.
ie int foo() { return 1; } but not string foo() {return 1;}

HINSTANCE GetInstance() { return m_hInstance; };
m_hInstance is a variable of type HINSTANCE that is stored in the class that function came from.
so the line "return" returns that value from the function.

int x, y, z;cout<< "type two numbers, and i will give the sum << endl;cin >> y;cin >> z;cout << "result is: " << sum(y,z) << endl;...int sum(int a, int b){return (a + b);};

so,
1) sum() doesnt have to have the same parameter names as the calling code
2) you have to pass the parameters to the function "sum(y,z)"
3) you can't have a variable named the same as a function call if you expect everything to work right
(so i changed the function from "x" to "sum"