Default value functions, C/C++ Programming

Default Value Functions, When declaring a function we can specify a default value for each parameter. This value will be used if that parameter is left blank when calling to the function. This is done by assigning values to the arguments in the function prototypes. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is stepped on and the passed value is used. 

Default arguments must always be the rightmost in a function list i.e. when calling a function with two or more default arguments and an omitted argument is not the rightmost argument in the argument list then all arguments to the right of that argument must be omitted.

Example:

// default values in functions

#include 

using namespace std;

int divide  (int a, int b=2);
int divide  (int a, int b)
{

int r;
r=a/b;

return  (r);

}

int main  ()
{

cout  << divide  (12);
cout  << endl;

cout  << divide  (20,4);
cout  << endl;

return  0;

}

 

Output

6

 

Posted Date: 8/21/2012 11:51:23 AM | Location : United States







Related Discussions:- Default value functions, Assignment Help, Ask Question on Default value functions, Get Answer, Expert's Help, Default value functions Discussions

Write discussion on Default value functions
Your posts are moderated
Related Questions

Write a c++ program to find the sum of 0.123 ? 103 and 0.456 ? 102 and write the result in three significant digits.

This program requires the use of a windows box, no console applications are allowed. Prepare for Christmas now.  Each year, Reindeer Gift Emporium publishes a Christmas price list

how to write code for z transformer

flow chart for push operation in stack

Project Description: Currently seeking someone who can create me a software application (google chrome crx file_ that will auto add all shoe sizes to cart directly and if not av

in a program for what purpose print f and scan f is used

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

You are to develop a Clinic System. 1. Identify and briefly explain the objects, classes, methods and attributes for the Clinic System. 2. Draw a use case diagram to represen

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a string of t