C++ default parameters

By making a parameter of a function a default parameter, the value of parameter can be optional when the function is called(it is OK to leave it blank.). To specify the default parameter, you must set its value. It is worth to note that a default parameter must be the last parameter in parameters list of a function.
Example:
#include <cstdlib>
#include <iostream>

using namespace std;
int sum(int a,int b,int c=0){
   int sum=a+b+c;
   return sum;
}

int main(int argc, char *argv[])
{
   cout<<sum(10,30)<<"\n";
   cout<<sum(12,34,30)<<"\n";
   system("PAUSE");
   return EXIT_SUCCESS;
}

Posted by: Ly | post date: 02-03-2013 | Subject: C++ Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.