C + + all variables and functions must have a type;
C The default type in the language is not legal in C + +!
In C language can write some very strange function! and can run successfully!
What type is the return value of the F (i) function? What is the parameter type? We have a value of 10 to print out!
How many parameters can a G () function accept? We have no parameters, but we can add parameters when the function is called and run successfully!
F (i)
{
printf ("i=%d\n", I);
}
G ()
{
return 5;
}
int main ()
{
f (10);
printf ("G () =%d\n", G (1,2,3,4,5));
return 0;
}
Operation Result:
i = 10
G () = 5
Press any key to Continu
So this is the C language bad place, this bad place C + + has abandoned it!
The same code we put in C + + to see!
#include <iostream>
using namespace Std;
F (i)
{
printf ("i=%d\n", I);
}
G ()
{
return 5;
}
int main ()
{
f (10);
printf ("G () =%d\n", G (1,2,3,4,5));
System ("pause");
return 0;
}
Found compile error.
In C + +, whether it is a function return value type or variable type, or the number of function parameters, we can not be sloppy, C + + compiler will be able to find the error! The following program compiles before you can pass!
#include <iostream>
using namespace Std;
int f (int i)
{
printf ("i=%d\n", I);
return 0;
}
int g ()
{
return 5;
}
int main ()
{
f (10);
printf ("G () =%d\n", g ());
System ("pause");
return 0;
}
So C + + is more rigorous in the type checking of variables and functions!
Summary:
in C in Language
an int f () is a function that returns a value of type int and accepts arbitrary arguments;
an int f (void) represents an parameterless function that returns a value of type int.
in C + + in
int f () and int f (void) have the same meaning, all of which represent an parameterless function that returns a value of type int.
C + + emphasizes the type, and any program element must indicate the type.
Long Press to unlock
Unlock more Insider Stories
Legal programming
: Lightspeed-tech
Technology-driven Life
C + + Note 13: C + + extension--c++ all variables and functions must have a type