C++ internal and external functions

Functions are global in nature,Because a function is called by another function,However, you can also specify that functions can only be called from this file,It cannot be called by other files.Depending on whether the function can be called from other source files,Distinguish functions into internal functions and external functions.

Intrinsic function

If a function can only be called by other functions in this file,It is called an internal function.When defining internal functions,Prefix the function name and function type with static. The general format of the function header is:

 static type identifier function name (formal parameter list);

Such as

 static int fun (int a, int b);

Internal functions are also called static functions. Using internal functions,You can restrict the function to the file it is in.If you have an internal function with the same name in a different file,Do not interfere with each other.Functions and external variables that can only be used by the same file are usually placed in one file,They are localized in front of them by static,Other files cannot be referenced.

External function

When defining a function,If the leftmost end of the function is prefixed with the keyword extern, it means that the function is an external function.Available for other files to call.For example, the function header can be written as:

 extern int fun (int a, int b);

In this way, the function fun can be called for other files.If extern is omitted when defining a function, it defaults to an external function.The functions used earlier in this tutorial are external functions.

In the files that need to call this function,The functions used in extern declarations are external functions.

[Example] Enter two integers,Whichever is greater,Implemented with external functions.

using namespace std;
int main ()
  extern int max (int, int);//Declares that the max function defined in other files will be called in this function
  int a, b;
  cout<max (a, b)<endl;
  return 0;
/*******file2.cpp(File 2) ******* /
int max (int x, int y)
  int z;
  return z;

The operation is as follows:

7 -34↙

When running a program with multiple files on your computer,Need to create a project file (project file), the project file contains the various files of the program.For details, please see:vc6.0 tutorial.

Through this example, we can know that the function defined in other files can be called in one file by using extern declaration.Or to extend the scope of this function to this file.The form of extern declaration is to add the keyword extern to the function prototype. Since functions are external in nature,In a program, external functions in other files are often called.For easy programming,C++ allows saving extern when declaring functions. The function declaration in Example 4.15 program's main function can be written as:

 int max (int, int);

This is the function prototype we have used many times.This can further understand the role of function prototypes.A function prototype can be used to extend the scope of a function beyond the file in which it is defined (without having to use extern). Just include the function prototype of the function in every file that uses the function.The function prototype informs the compilation system:the function is defined later in this file,Or defined in another file.

The most common example of using function prototypes to extend function scope is the application of the #include command.The header file specified by the #include command contains the information needed to call library functions.For example, the sin function needs to be called in the program, but the trigonometric function is not defined by the user in this file.It is stored in the mathematical function library.As mentioned above,The prototype of the sin function must be written in this file,Otherwise, the sin function cannot be called. The prototype of the sin function is:

 double sin (double x);

Originally, the programmer should find out the prototype of the library function used from the manual when calling the library function.And write them one by one in the program,But this is obviously troublesome and difficult.To reduce the difficulty for programmers,The header file cmath includes the prototypes and other relevant information of all mathematical functions,Users simply use the following #include command:


Just fine. At this time, each mathematical library function can be legally called in the file.

C++ macro definition #define

You can use the #define command to represent a string with a specified identifier (ie, a macro name).The purpose of defining macros is to use a short name to represent a long string.Its general form is:

 #define identifier string

This is the definition of symbolic constants already introduced.Such as:

 #define pi 3.1415926

You can also use the #define command to define macro definitions with parameters.The general form of its definition is:

 #define Macro name (parameter table) string

Such as:

 #define s (a, b) a * b //Define macro s (rectangular area), a, b are macro parameters

The form used is as follows:

 area=s (3, 2);

Replace the formal parameters a and b in the macro definition with 3 and 2, respectively, that is, replace s (3, 2) with 3 * 2. So the assignment statement expands to:

 area=3 * 2;

Because C++ adds a built-in function (inline), it is more convenient than using macros with parameters.Therefore, #define command is no longer used to define macros in C++.Mainly used in conditional compilation.

  • Previous JavaScript to determine whether the webpage is closed or refreshed
  • Next How to resolve VisualSVN Server installation prompt error Repositories is not a valid short file name