[C language study notes]: Internal and external functions

Declaration and definition of C language functions

A function generally consists of two parts:

  • statement part execution

  • line statement

For variables in C language, the relationship between declaration and definition is slightly more complicated. There are two situations for variables that appear in the declaration part:

  • One requires the establishment of storage space.

  • The other one does not require the creation of storage space.

The former is called a defining statement, or definition for short; the latter is called a referential statement.

Generally, for the convenience of description, the statement that creates a storage space is called a definition, and the statement that does not need to create a storage space is called a statement.

The declarations of variables that appear in a function, except those declared with extern, are definitions. The declarations of other functions in the function are not definitions of the function.

C language internal functions

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 in front of the function name and function type:

static type name function name (formal parameter list);

Internal functions are also called static functions because they are declared with static.

Using internal functions can limit the scope of the function to the file where it is located. Even if there are internal functions with the same name in different files, they will not interfere with each other.

Usually, functions and external variables that can only be used by this file are placed at the beginning of the file, and static is added in front to make them local, indicating that other files cannot reference them.

C language external functions

When defining a function, add the keyword extern to the leftmost end of the function header, then the function is an external function and can be called by other files.

C language stipulates that if extern is omitted when defining a function, it defaults to an external function.

C language internal function external function case

#include<stdio.h>
extern int maxNumber(int num1,int num2)//External function
{   int max;   max=num1>num2?num1:num2;   return max; } static float minNumber(float num1,float num2)/ /Internal function {   float min;   min=num1<num2?num1:num2;   return min; } int main() {   printf("%d\n",maxNumber(10,11));   printf("%f\n ",minNumber(10,11));   return 0; }















Compile and run results:

11
10.000000

--------------------------------
Process exited after 0.07334 seconds with return value 0
Please press any key to continue. . .

Guess you like

Origin blog.csdn.net/Jiangziyadizi/article/details/129676739