Jump to content

default arguments

- - - - -

  • Please log in to reply
4 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
The following text from a book:

Quote

Default arguments are useful if you don’t want to go to the trouble of writing arguments that, for example, almost always have the same value. They are also useful in cases where, after a program is written, the programmer decides to increase the capability of a function by adding another argument. Using default arguments means that the existing function calls can continue to use the old number of arguments, while new function calls can use more.

Could you please explain it to me? I couldn't understand it. It would be really kind of you. Perhaps a simple example would make it clear.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I mean it could be applied to little things, say a function works with floats and that is not accurate enough, you could have added a default parameter "bool useDoubles=false" and then check within the function if it is false or not.

Old code will still receive a float as the statement is false, new code however can explicitly define func(true) to use this new functionality, ("existing function calls can continue to use the old number of arguments, while new function calls can use more.")

Often these default arguments are not the most useful, however there are odd situations where you could add new functionality (yet not wanting a new function.)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Alexander. I appreciate your effort to make it understandable for me. But unfortunately, I'm still struggling to grasp the underlying meaning. Could you please help me a bit more? Perhaps, a simple code would make it clear. Thanks.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts

int func(void)

{

}


// Can be called as

func(); // This call is written in code at many places which i don't want to break


Now if i change it's definition to

int func(int a=4)

{

}


func(10); // would work and be able to pass value 10 to func based upon which func could do something else such as it might have a new switch case


switch (a)

{

    case 10:

        cout << "This is new functionality without breaking older calls to func" << endl;

        break;

}


func(); // old call would still continue to work with value 4 without requiring any changes


Edited by fayyazlodhi, 01 June 2011 - 11:43 AM.
typo


#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
It is a bit hard to think of any, especially in C/C++ where you usually would write very carefully your functions.

I do know however, PHP has a lot of functions of which implement these arguments in newer versions, you could look at a few:

PHP: microtime - Manual (return as float, instead of array of integer and float portion)
PHP: md5 - Manual (return as binary, instead of hexadecimal representation of the binary)

I am sure you could find many more, as PHP changes often and people suggest features, they just append them as default arguments (so that the old method will return the old result)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users