+ Reply to Thread
Results 1 to 3 of 3

Thread: How default parameter works in C++?

  1. #1

    How default parameter works in C++?

    How default parameter works in C++?

    Please give me some example.

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    In many cases it is possible that some of the arguments in the function are frequently used and can have default value i user do not supply then explicitly.

    Example:

    Code:
    int function1(int a1, int a2, int a3 =10)
    {
    	return a1+ a2 + a3 ;
    }
    
    void UseDefaultParameters()
    {
    	char szMesage[100] ;
    
    	int nValue = function1(0, 5, 10) ;
    	sprintf(szMesage, "Value = %d\n", nValue) ;
    	OutputDebugString(szMesage) ;
    
    	nValue = function1(0, 5) ;
    	sprintf(szMesage, "Value = %d\n", nValue) ;
    	OutputDebugString(szMesage) ;
    }
    Note:
    Alternative to this is to use function overloading.

  3. #3
    Thank you.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts