The world of C language (7)

foreword

Hello, C language (function)

1. Function (processing factory)

1. Function definition

The first time I heard this was in math class, a function in C refers to a program or code that can be directly referenced by another program or code. 

In the C language, we often use functions, such as printf (output function), scanf (input function), etc. These are the library functions that the C language gives us, and if we need a function with a larger size, we should How to do it?

#include<stdio.h>
int Compare(int x, int y);//函数使用需要声明!
int main()
{
    int a = 0;
    int b = 0;
    scanf("%d %d", &a, &b);
    int c = Compare(a, b);
    printf("%d", c);
    return 0;
}

int Compare(int x, int y)
{
    int z = (x) > (y) ? x : y;
    return z;
}

 As we can see from the image above, I created a Compare function to compare the size of two numbers.

In the C language, users can define their own functions to meet the needs of users.

The main function passes the actual parameter ( the actual parameter appears in the calling function, after entering the called function, the actual parameter variable cannot be used. ) The formal parameter to the sub-function (the formal parameter appears in the definition of the sub-function, and can be used in the entire function body After the sub-function runs, the result is returned (return) to the main function caller. In the main function, a variable needs to be set to receive the return value. (In this program, we set the c variable)

In layman's terms, a C language function is like a processing factory. We send the raw materials to the processing factory (sub-function), and let the processing factory process the product (return value) to us (the main function).

Emphasize one point: In C language, functions cannot be nested and defined, and the function definition statement is not added ; functions can be nested.

Functions cannot have the same name, cannot be defined repeatedly, but can be declared repeatedly.

2. Functions modified by static

We define a subfunction in another file. After running, we can see that the function is called normally.

 

Let's try the static modifier function

 

 A warning window pops up.

After the function is modified by static, the external link attribute becomes the internal link attribute, so our function can only be used in the current source file (the Compare.c file in the figure), and other source files cannot be used.

3. Types of functions

What type is the return value of a function, what type is the function.

In the above, we returned that Z is an integer, so our function is an integer function.

There are also character functions, empty functions (void), double-precision floating-point functions (double), single-precision floating-point functions (float), and so on.

Thank you for reading this. I am a computer science student, and there are bound to be mistakes in the blog post. I hope you can correct me.

Guess you like

Origin blog.csdn.net/m0_60653728/article/details/122076175