Internal and external functions

Source: Internet
Author: User
Tags mathematical functions cmath
Internal and external functions

The function is global in nature, because a function is called by another function. However, you can also specify that the function can only be called by this file, but not by other files. Divides a function into an internal function and an external function based on whether the function can be called by other source files.

Internal functions

If a function can only be called by other functions in this file, it is called an internal function. When defining an internal function, add static before the function name and function type. The common format of the function header is

Static type identifier function name (table of parameters)

For example

static int fun(int a,int b)

 

Internal functions are also called static functions. Using Internal functions, You can restrict functions to only files. If there are internal functions with the same name in different files, they do not interfere with each other. Generally, the functions and external variables that can only be used by the same file are put in one file, and all of them are given static to make them localized. Other files cannot be referenced.

 

External Functions

(1) when defining a function, if the keyword extern is appended to the leftmost end of the function header, this function is an external function and can be called by other files.

If the function header can be written

extern int fun (int a, int b)

 

In this way, function fun can be called for other files. If extern is omitted when defining a function, the default value is an external function. All the functions used earlier in this book are external functions.

(2) In the file that needs to call this function, use extern to declare that the function used is an external function.

For example, if two integers are input, the greater one must be output. Use external functions.

File1.cpp (File 1)
# Include <iostream> using namespace STD; int main () {extern int max (INT, INT ); // declare that the max functions int A, B; CIN> A> B; cout <max (a, B) defined in other files will be called in this function) <Endl; return 0 ;}
File2.cpp (file 2) int max (int x, int y) {int Z; Z = x> Y? X: Y; return Z ;}

The running status is as follows:

7-34

7

 

In this example, we can use the extern declaration to call functions defined in other files in a file, or extend the function scope to this file. The form of the extern declaration is to add the keyword extern Based on the function prototype. Because the function is external in nature, it is often necessary to call external functions in other files in the program. To facilitate programming, C ++ allows writing extern while declaring the function. The function declaration in the main function of the previous program can be written

int max(int,int);

 

This is the prototype of the function we have used many times. The role of the function prototype can be further understood. The function prototype can extend the function scope beyond the file defining the function (you do not need to use extern ). You only need to include the function prototype in each file that uses the function. Function prototype notification compilation system: this function is defined later in this file or in another file.

The most common example of extending the function scope using the function prototype is the application of the # include command. The header file specified by the # include command contains the information required to call the library function. For example, the sin function needs to be called in a program, but the trigonometric function is not defined by the user in this file, but stored in the mathematical function library. As described above, the sin function prototype must be written in this document. Otherwise, the sin function cannot be called. The prototype of the sin function is double sin (Double X );

The program designer should first find out the prototype of the library function from the manual when calling the library function, and write it one by one in the program, but this is obviously troublesome and difficult. To reduce the difficulty of programmers, the header file cmath contains the prototype of all mathematical functions and other related information. You only need to use the following # include command:

#include <cmath>

You can. In this case, you can legally call the mathematical functions in this file.

 

 

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.