• Advertisement
Sign in to follow this  

Passing things into functions, should I assign them before using?

This topic is 767 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I am passing something into a function, when I am in that function, is there any advantage to assigning that 'passed in' variable before using it? Or can I just use it directly...

 

e.g. I use width and height directly where I need to..., where GAME_WIDTH GAME_HEIGHT are constants...

 

function call: graphics.init(GAME_WIDTH, GAME_HEIGHT)

 

function definition:

Graphics::init(int width, int height)

{

    DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory(&sd, sizeof(sd));
    sd.BufferCount = 1;
    sd.BufferDesc.Width = width;
    sd.BufferDesc.Height = height;

}

 

or is it better to assign them like

function definition:

Graphics::init(int wid, int hei)

{

    int width = wid;

    int height = hei;

    DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory(&sd, sizeof(sd));
    sd.BufferCount = 1;
    sd.BufferDesc.Width = width;
    sd.BufferDesc.Height = height;

}

 

Thanks for any help on this matter.

Edited by SteveHatcher

Share this post


Link to post
Share on other sites
Advertisement

It depends on what the function is trying to do.

 

You generally store a copy or a reference to something in a class because you will need to use it later, if you never need to use it, then you would probably not need to store it.  However this also depends on what part of your program you are implementing.

 

In general, I try never to add something to a program unless it is going to be used somewhere.

Share this post


Link to post
Share on other sites

Outside of really specific micro-optimizing cases requiring you write assembly, I can't think of when you'd get a real benefit for this. There's a good chance that if it makes the program faster, the compiler already does this, and otherwise the compiler will optimize it away.

Share this post


Link to post
Share on other sites

function call: graphics.init(GAME_WIDTH, GAME_HEIGHT)
If you only call it with constants, you can even drop the parameters, and use the constants in the function directly.

Share this post


Link to post
Share on other sites

There is no advantage to it if your values do not change. However... why are you defining a swap chain inside a function? I could be wrong here, but it's in danger of falling out of scope.

Share this post


Link to post
Share on other sites
It's safe to think of parameters are being variables within the function body scope that happen to be initialized by whatever arguments get passed in.
 
You can even re-assign them in the function, provided that the parameters are not const.
 
void myFunc(int foo) {
  std::cout << foo << "\n";
  foo = 12;
  std::cout << foo << "\n";
}

int main() {
  myFunc(5);
}

 

5
12

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement