< Previous | Contents | Next >

Using Default Arguments

When you write a function in which a parameter almost always gets passed the same value, you can save the caller the effort of constantly specifying this value by using a default argumenta value assigned to a parameter if none is specified. Heres a concrete example. Suppose you have a function that sets the graphics display. One of your parameters might be bool fullScreen, which tells the function whether to display the game in full-screen or windowed mode. Now, if you think the function will often be called with true for fullScreen, you could give that parameter a default argument of true, saving the caller the effort of passing true to fullScreen whenever the caller invokes this display-setting function.


 

Introducing the Give Me a Number ProgramSpecifying Default ArgumentsAssigning Default Arguments to ParametersOverriding Default Arguments