Expansion of function parameters (6)

        In C++, it is possible to provide a default value for a parameter when the function is declared, and when the function is called without providing a value for the parameter, the default value is used . The default value of the function must be specified in the function declaration, so can the default value of the parameter appear in the function definition? What happens when the default value of the parameter in the function declaration and definition is different? We analyze through example code

#include <stdio.h>

int mul (int x = 0);

int main(int argc, char *argv[])
{
    printf("mul() = %d\n", mul());
    printf("mul(-1) = %d\n", mul(-1));
    printf("mul(2) = %d\n", mul(2));

    return 0;
}

int mul (int x)
{
    return x * x;
}

        We see that when declaring the function mul, we declare the parameter to be 0 by default. Let's see if this program can be compiled successfully? If so, what will be printed in the main function?

picture.png

        We found that it did not report an error, that is to say, in C++, when a function is declared, it supports parameter default values. Then let's try to support this way of writing in the C language than that?

picture.png

        We see that this way of writing is not supported in the C language compiler, then we try to give the default value in the definition, but not in the declaration, to see if the g++ compiler supports it

picture.png

        We see that it throws an error, so we can only give default values ​​to parameters when declaring them.

        So since the function supports parameter default values, does it have any rules for setting multiple parameters? There are two rules: a> the default value of the parameter must be provided from right to left; b> the default value is used when the function is called, the subsequent parameters must use the default value .

        Let's analyze the code as an example

#include <stdio.h>

int add (int x, int y = 1, int z = 2);

int main(int argc, char *argv[])
{
    printf("add(1) = %d\n", add(1));            // 4
    printf("add(1, 2) = %d\n", add(1, 2));        // 5
    printf("add(1, 2, 3) = %d\n", add(1, 2, 3));// 6

    return 0;
}

int add (int x, int y, int z)
{
    return x + y + z;
}

        Let's analyze this code, the default parameters y and z are 1 and 2 respectively. Then the return values ​​in the main function are 1 + 1 + 2 = 4; 1 + 2 + 2 = 5; 1 + 2 + 3 = 6; let's see if this is the case?

picture.png

        We see that the results are indeed as we analyzed. So let's try to only give y the default value, that is, not to follow the assignment from right to left, and see if it will compile successfully?picture.png

        We see that the compilation reports an error, so what if we only want to give the default value of y? Just swap the y and z positions and put it on the far right.

        Let's talk about function placeholder parameters. In C++, placeholder parameters can be provided for functions. Placeholder parameters only have parameter type declarations, but no parameter name declarations; in general, placeholder parameters cannot be used inside a function body .

        Let's take the code as an example to see what the function placeholder parameter looks like

#include <stdio.h>

int func(int x, int);

int main(int argc, char *argv[])
{
    printf("func(1, 2) = %d\n", func(1, 2));

    return 0;
}

int func(int x, int)
{
    return x;
}

        Can we try to compile it?

picture.png

        We see that the compilation is passed, so do we think this is unnecessary? Obviously it only needs one parameter, but there is an extra placeholder parameter. Next, let's talk about the meaning of function placeholder parameters. The use of placeholder parameters in combination with default parameters is compatible with irregular writing methods that may appear in C language programs. We still remember that void func() and void func(void) are equivalent before. In C language, it is not equivalent, because the first one means that it can accept any number of parameters, and the latter one does not accept parameter. In C++ they both mean the same thing.

        Through the study of function placeholder parameters, the summary is as follows: 1. The default value of the parameter is supported in C++; 2. If the parameter value is not provided when the function is called, the default value is used; 3. The default value of the parameter must be provided from right to left ;4. If the default value is used when the function is called, the subsequent parameters must use the default value;


        Welcome to learn C++ language together, you can add me QQ: 243343083 .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325387830&siteId=291194637