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

Recommended Answers

All 2 Replies

3

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.