Briefly explain the definition of internal and external functions and macros of C++

  • 2020-05-05 11:31:53
  • OfStack

C++ internal function and external function
Functions are global in nature, because one function is called by another, but it is also possible to specify that functions can only be called by this file and not by other files. Distinguish functions into internal and external functions based on whether they can be called by other source files.
The inner function

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


  static  Type identifier   The function name ( Parameter table );


Such as


  static int fun(int a, int b);


The inner function is also called the static (static) function. Using internal functions, you can restrict the function to the file in which it resides. If there are internal functions with the same name in different files, they do not interfere with each other. It is common to put functions and external variables that can only be used by the same file in one file, with static in front of them to localize them, and no other file can be referenced.
The external function

When defining a function, the keyword extern at the far left end of the function header indicates that the function is external and can be called by other files. If the function head can be written as:


  extern int fun (int a, int b);


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 all external functions.

In the file that needs to call this function, the function declared with extern is an external function.

Input two integers and ask for the larger one to be output.


/*******file1.cpp( file 1)*******/
#include <iostream>
using namespace std;
int main( )
{
  extern int max(int,int); // Declares that in this function you will call those defined in other files max function 
  int a,b;
  cin>>a>>b;
  cout<<max(a,b)<<endl;
  return 0;
}
/*******file2.cpp( file 2)*******/
int max(int x,int y)
{
  int z;
  z=x>y?x:y;
  return z;
}

Here's how it works:


7 -34 � 
7

When running a multi-file program on a computer, you need to create a project file (project file) that contains the individual files of the program. See the VC 6.0 tutorial for details.

From this example, you can use the extern declaration to call functions defined in other files in one file, or to extend the scope of the function to this file. The extern declaration takes the form of adding the keyword extern to the function prototype. Because functions are external in nature, external functions in other files are often called in programs. For programming purposes, C++ allows you to save writing extern when declaring functions. Example 4.15 the function declaration in the program main function can be written as:


  int max(int, int);

This is the prototype of the function that we've used many times. From this, we can further understand the function prototype. Function prototypes can be used to extend the scope of a function beyond the file in which it is defined (no need to use extern). Simply 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 in another file.

The most common example of extending a function scope with a function prototype is the application of the #include command. The header file specified by the #include command contains the information needed to call the library function. For example, the sin function needs to be called in the program, but the trig functions are not defined by the user in this file, but are stored in the mathematical function library. As described above, you must write the prototype of the sin function in this file, otherwise you cannot call the sin function. The prototype of the sin function is:


  double sin(double x);


It would have been up to the programmer to look up the prototype of the library function in the manual and write it out in the program before calling the library function, but this is obviously cumbersome and difficult. To reduce the programmer's difficulties, all mathematical function prototypes and other relevant information are included in the header file cmath with the following #include command:


  #include <cmath>


Can. At this point, the mathematical library functions can be legally called in the file.

C++ macro definition #define
A specified identifier (that is, the macro name) can be used to represent a string using the #define command. A definition macro typically USES a short name to represent a long string. Its general form is


  #define  identifier   string 


This is the definition of a symbolic constant that we've already introduced. Such as:


  #define PI 3.1415926


You can also define macro definitions with arguments using the #define command. The general form of its definition is


  static int fun(int a, int b);
0


Such as:


  static int fun(int a, int b);
1

The form used is as follows:


  static int fun(int a, int b);
2


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


  area=3*2;


Since C++ adds a built-in function (inline) that is more convenient than using macro definitions with arguments, the #define command is basically no longer used to define macros in C++, and is mainly used in conditional compilation.


Related articles: