Jump to content
  • Advertisement
Sign in to follow this  
SteveHatcher

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

This topic is 920 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

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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!