C ++ Lesson 7 - Extended function parameters

This article learn from Ditai Software College Tangzuo Lin teacher of C ++ courses


Introduction: C ++ to C function arguments in the upgrade and expansion done, provides default parameters and parameter placeholders. Positional parameters for non-standard compatibility written in C

Experiment 1: The default parameter values
Experiment 2: usage rule arguments
Experiment 3: Error Model function parameters
Experiment 4: in C and C ++ Comparative FUNC () and func (void) are equivalent
Experiment 5: parameter placeholder non-standard compatible written in C
experiment 6: positional parameters with default parameter values and not written specification compatible for use in C


Here Insert Picture Description

Here Insert Picture Description

Answer: The default parameter values ​​can only be specified in the statement, without giving a longer definition.


Experiment 1: Use the default parameter values

#include <stdio.h>

int mul(int x = 0);//函数参数的默认值

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

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


mhr@ubuntu:~/work/c++$ g++ 7-1.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
0
1
4
mhr@ubuntu:~/work/c++$ 

Here Insert Picture Description

Experiment 2: function parameters using rules

#include <stdio.h>

int add(int x, int y = 0, int z = 0);

int main(int argc, char *argv[])
{
    printf("%d\n", add(1));
    printf("%d\n", add(1, 2));
    printf("%d\n", add(1, 2, 3));
    
    return 0;
}

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

mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 7-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
1
3
6
mhr@ubuntu:~/work/c++$ 

Experiment 3: Demonstration error function argument

#include <stdio.h>

int add(int x, int y = 0, int z);

int main(int argc, char *argv[])
{
    printf("%d\n", add(1));
    printf("%d\n", add(1, 2));
    printf("%d\n", add(1, 2, 3));
    
    return 0;
}

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


mhr@ubuntu:~/work/c++$ g++ 7-2.cpp
7-2.cpp:3:5: error: default argument missing for parameter 3 of ‘int add(int, int, int)’
 int add(int x, int y = 0, int z);
     ^
mhr@ubuntu:~/work/c++$ 

In the design function default parameters, it can only be considered from right to left to provide default values ​​provided here is from the middle of error.

change

Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description
In embodiment C, two types are not equivalent, the former could accept any parameter, which represents takes no parameters.

Experiment 4: in C and C ++ Comparative FUNC () and func (void) are equivalent

In C
Here Insert Picture Description

Question: Why is this so?
Answer: because there are many non-standard written in C language, as described above experiment, the parameter C language function FUNC () represents, any acceptable parameters, while func (void) represents only takes no arguments! !

Here Insert Picture Description

In C ++

Here Insert Picture Description

Therefore, in C ++ FUNC () and func (void) is equivalent to, any parameters are not accepted.

Conclusion:
1. C language compiler parameter function FUNC () represents, any acceptable parameters, while func (void) represents only takes no arguments! ! The two are not equivalent.

2. In the C ++ compiler FUNC () and func (void) is equivalent to, any parameters are not accepted.


Experiment 5: C irregularities written place holders for parameters compatible

Here Insert Picture Description


Experiment 6: positional parameters with default parameter values ​​and non-standard C-compatible for use in the wording

#include <stdio.h>

int func(int x, int = 0);

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

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

Here Insert Picture Description

Published 200 original articles · won praise 100 · views 80000 +

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/104088718