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

Source: Internet
Author: User
Tags extend function prototype mathematical functions sin cmath

C + + internal functions and external functions
Functions are global in nature, because a function is called by another function, but you can also specify that a function can be invoked only by this file, not by other files. distinguishes functions from internal and external functions, depending on whether the function can be called by another source file.
Internal function

If a function can only be called by other functions in this file, it is called an intrinsic function. When you define an intrinsic function, you add static before the function name and function type. The general format for the first part of the function 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. With intrinsic functions, you can make a function limited 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. Functions and external variables that can only be used by the same file are usually placed in a file, preceded by static so that it is localized, and other files cannot be referenced.
External functions

When you define a function, if you prefix extern with the leftmost end of the first number of functions, this function is an external function that can be invoked by other files. If the first part of the function can be written as:

  extern int fun (int a, int b);


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

In the file where you need to call this function, the function used in the extern declaration is an external function.

"Example" input two integers, which require the output of the large, with an external function implementation.

/*******file1.cpp (document 1) *******/
#include <iostream>
using namespace std;
int main ()
{
  extern int max (int,int);//declare the max function int a,b that will be called in the other file in this function
  ;
  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;
}

The operating conditions are as follows:

7-34↙
7

When you run a program with multiple files on your computer, you need to create a project file (project file) that contains the individual files for the program in the project file. For more information, please see: VC6.0 use Tutorial.

This example shows that using an extern declaration can invoke a function defined in another file in one file, or extend the scope of the function to this file. The form of an extern declaration is to add the keyword extern on the basis of a function prototype. Because functions are external in nature, and often in programs to invoke external functions in other files, for ease of programming, C + + allows the province to write extern when declaring functions. Example 4.15 A function declaration in the main function of a program can be written as:

  int max (int, int);

This is the function prototype that we have used many times. The function prototype can be understood further. Function prototypes enable you to extend the scope of a function beyond the file that defines the function (you do not have to use extern). As long as the function prototype of the function is included in each file that uses the function. The function prototype notifies the compilation system that the function is defined later in this file or in another file.

The most common example of extending function scopes using function prototypes is the application of the #include command. The header file specified in the #include command contains the information required to invoke the library function. For example, the SIN function needs to be invoked in a program, but trigonometric functions are not defined by the user in this file, but are stored in a mathematical function library. As described above, you must write a prototype of the SIN function in this file, otherwise you cannot call the SIN function. The SIN function is a prototype:

  Double sin (double x);


It should have been the programmer's call to the library function to find out from the manual the library function used in the prototype, and in the program one by one write out, but this is obviously troublesome and difficult. In order to reduce the difficulty of the programmer, in the header file Cmath include all the mathematical functions of the prototype and other relevant information, the user only need to use the following #include command:

  #include <cmath>


Can. At this point, you can legitimately invoke each of the math library functions in the file.

C + + macro definition #define
You can use the #define command to represent a specified identifier (that is, a macro name) as a string. The function of defining a macro is generally to represent a long string with a short name. Its general form is:

  #define Identifier string


This is the definition symbol constant that has been 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 is a macro parameter

The following forms are used:

  Area=s (3, 2);


Replace the form parameters A and b in the macro definition with 3, 2 respectively, and replace S (3, 2) with 3*2. Therefore, the assignment statement expands to:

  area=3*2;


Since C + + has added built-in functions (inline), which is more convenient than using macro definitions with parameters, the #define command is essentially no longer defined in C + +, primarily for conditional compilation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.