0

if a function recives 3 pass-by-value parameters, how many actual parameters should be in call statement?

3
Contributors
2
Replies
16
Views
2 Years
Discussion Span
Last Post by rubberman
0

C++ allows default values in the declaration/definition of a function, so if there are such, you can leave the ones with defaults out of the call if the default is valid. Example:

#include <iostream>
using namespace std;
// Declaration - may be in a header file.
int someFunction( int a, int b = 0, int c = 100);

// Definition
int someFunction( int a, int b, int c)
{
    cout << "a == " << dec << a
         << ", b == " << b
         << ", c == " << c
         << endl;
    return a+b+c;
}

// Use of someFunction()
int main()
{
    int sum1 = someFunction(100);
    int sum2 = someFunction(100, 100);
    int sum3 = someFunction(100, 100,99);

    // sum1 will == 200, sum2 == 300, and sum3 == 299
    cout << "sum1 == " << dec << sum1
         << ", sum2 == " << sum2
         << ", sum3 == " << sum3
         << endl;
    return 0;
}

This is the output you should see:

a == 100, b == 0, c == 100
a == 100, b == 100, c == 100
a == 100, b == 100, c == 99
sum1 == 200, sum2 == 300, sum3 == 299

Edited by rubberman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.