Function-C Language (Elementary)

Table of contents

1. What is a function

2. Classification of functions

        2.1 Library functions

        2.2 Custom functions

3. Function parameters

        3.1 Actual parameters (actual parameters)

        3.2 Formal parameters (formal parameters)

4. Function call

        4.1 Call by value

        4.2 Call by address

5. Nested calls and chained access of functions

        5.1 Nested calls

        5.2 Chain access

6. Function declaration and definition

        6.1 Function declaration

        6.2 Definition of functions

7. Function recursion


1. What is a function

       A function is a portion of code in a large program, consisting of one or more statement blocks . It is responsible for completing a specific task and is relatively independent from other code. Generally, there will be input parameters and return values, providing encapsulation and hiding of details of the process.

2. Classification of functions

        Functions can be divided into library functions and custom functions .

        2.1 Library functions

        When we are learning C language programming, when we want to know the result after writing a code and want to print the result to the screen, we will frequently use a function: print the information to the screen in a certain format (printf). In the process of programming, we will frequently do some string copying (strcpy), calculate n to the kth power (pow), and find the absolute value (abs).

        Like the basic functions described above, they are not business codes and may be used by every programmer during the development process. In order to support portability and improve program efficiency, the basic library of C language provides a series of similar library functions to facilitate programmers
to develop software.
        Reference website for learning library functions: www.cplusplus.com

        Commonly used library functions in C language include IO functions, string operation functions, memory operation functions, time/date functions, mathematical functions, etc. When using library functions, you must include the corresponding header file #include.

        2.2 Custom functions

        Custom functions are the same as library functions. They have function names, return value types and function parameters . You can design your own functions to meet business needs.

ret_type fun_name(para1, para2,....)
{         statement;//statement item } ret_type return type fun_name function name para1, para2 function parameters




        Example: Design a function to find the maximum value of two integers

#include <stdio.h>
int get_max(int x, int y)
{
        return (x>y)?(x):(y);
}
int main()
{
        int num1 = 10;
        int num2 = 20;
        int max = get_max(num1, num2);
        printf("max = %d\n", max);
        return 0;
}

3. Function parameters

        3.1 Actual parameters (actual parameters)

        The parameters that are actually passed to the function are called actual parameters, which can be constants, variables, expressions, functions, etc. No matter what type the actual parameters are, they must have certain values ​​when making function calls to transfer these values ​​to formal parameters.

        3.2 Formal parameters (formal parameters)

        Formal parameters refer to the variables in parentheses after the function name. Because formal parameters are only instantiated (memory units allocated) when the function is called, they are called formal parameters. When the function call is completed, the formal parameters are automatically destroyed, so the formal parameters are only valid in the function. After the formal parameters are instantiated, they are actually equivalent to a temporary copy of the actual parameters .

4. Function call

        4.1 Call by value

        The formal parameters and actual parameters of a function occupy different memory blocks respectively, and modifications to the formal parameters will not affect the actual parameters.

        4.2 Call by address

        Calling by address is a way of calling a function by passing the memory address of a variable created outside the function to a function parameter . The variable outside the function can be directly manipulated inside the function.

5. Nested calls and chained access of functions

        Functions can be combined according to actual needs.

        5.1 Nested calls

#include <stdio.h>
void new_line()
{
        printf("hehe\n");
}
void three_line()
{
        int i = 0;
        for(i=0; i<3; i++)
        {
                new_line();
        }
}
int main()
{
        three_line();
        return 0;
}

        Functions can be called nested, but not defined .

        5.2 Chain access

        Use the return value of one function as a parameter of another function .

#include <stdio.h>
#include <string.h>
int main()
{
        char arr[20] = "hello";
        int ret = strlen(strcat(arr,"bit"));
        printf("%d\n", ret);
        return 0;
}

6. Function declaration and definition

        6.1 Function declaration

        Tell the compiler what the function is called, what the parameters are, and what the return type is. The declaration of the function generally appears before the use of the function. To meet the requirement of declaring first and then using, the declaration of the function should generally be placed in the header file.

        6.2 Definition of functions

        The definition of a function refers to the specific implementation of the function to realize the function of the function.

7. Function recursion

        Recursion: A program calling itself is called recursion. Recursion as an algorithm is widely used in programming languages. A process or function has a method of directly or indirectly calling itself in its definition or description. The recursive strategy only requires a small amount of program code to describe the multiple repeated calculations required for the problem-solving process, greatly reducing the complexity of the program. The main way to think about the amount of code and recursion is to make big things small .

        Necessary conditions for recursion: There is a restriction condition. When this restriction condition is met, the recursion will no longer continue; each recursive call gets closer and closer to this restriction condition.

        Example: Enter an integer value (unsigned) and print each bit of it in order

#include <stdio.h>
void print(int n)
{
        if(n>9)
        {
                print(n/10);
        }
        printf("%d ", n%10);
}
int main()
{
        int num;

        scanf("%d",&num);
        print(num);
        return 0;
}

Guess you like

Origin blog.csdn.net/qq_64668629/article/details/132431194