C language: function 1.0

1. What is the function?

Wikipedia definition of function: subroutine

Subroutine is a part of code in a large program, which is composed of one or more statement blocks. It is responsible for completing a specific task and has relative independence compared with other codes.

Generally, there are input parameters and return values, which provide encapsulation of the process and hiding of details. These codes are usually integrated into software libraries.

2. C lassification of functions in C language:

1. Library functions: functions provided by C language manufacturers that can be used directly

2. Custom function: a function created and implemented by yourself that is not available in C language

2.1 library functions:

In the early C language, there was no library function and programmers had to write it by themselves every time, which would cause code redundancy, low development efficiency and non-standard. Therefore, when we frequently use a function, in order to support portability and improve program efficiency, a series of similar library functions are provided in the basic Library of C language to facilitate programmers to carry out software development and integrate it into the library. (that is, unified standards)

The library functions commonly used in C language are:

1. IO function input / output

2. String operation function, such as strlen

3. Character operation function

4. Memory operation function

5. Time / date function

6. Mathematical function

7. Other library functions

The above functions can be found in the file: http://www.cplusplus.com/reference/

Now let's introduce how to read the file

For example:

Header file:

 size_t -- > the return value type of unsigned int sizeof is size_t

2.2 user defined functions

If library functions can do everything, what do programmers do? All the more important is custom functions. Like library functions, custom functions have function names, return value types, and function parameters. But the difference is that these are designed by ourselves. This gives programmers a lot of room to play.

Function composition:

Example 1 find the maximum of two integers

#include <stdio.h>
//get_ Design of Max function
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;
}

The naming of functions and the definition of variables should have practical significance as far as possible

Example 2 exchange the contents of two shaping variables

#include <stdio.h>
//Implemented as a function, but cannot complete the task
//When an argument passes data to a formal parameter, the formal parameter is a temporary copy of the argument and will not affect the change of the argument
void Swap1(int x, int y)
{
 int tmp = 0;
 tmp = x;
 x = y;
 y = tmp;
}
//Correct version
void Swap2(int *px, int *py)
{
 int tmp = 0;
 tmp = *px;
 *px = *py;
 *py = tmp;
}
int main()
{
 int num1 = 1;
 int num2 = 2;
 Swap1(num1, num2);
 printf("Swap1::num1 = %d num2 = %d\n", num1, num2);
 Swap2(&num1, &num2);
 printf("Swap2::num1 = %d num2 = %d\n", num1, num2);
 return 0;
}

The above switching operation is actually this principle

int mian()
{
    int a = 10;
    int* pa = &a;
    *pa = 20;
    return 0;
}

3. Parameters of function

3.1 actual parameters (actual parameters)

The parameters actually passed to the function are called arguments.

Arguments can be constants, variables, expressions, functions, etc.

No matter what type of arguments are, they must have definite values when making a function call in order to pass these values to the formal parameters.

3.2 formal parameters (formal parameters)

Formal parameters refer to the variables in parentheses after the function name. They are called formal parameters because they are instantiated (memory units are allocated) only when the function is called.

Formal parameters are automatically destroyed after the function call is completed. Therefore, formal parameters are only valid in functions.

(no memory is opened up without calling)

We can simply think that after the formal parameter is instantiated, it is actually equivalent to a temporary copy of the argument.

4. Function call

4.1 value transfer call (swap1)

The formal parameters and arguments of the function occupy different memory blocks respectively, and the modification of the formal parameters will not affect the arguments.

4.2 address transfer call (swap2)

Addressing call is a way to call a function by passing the memory address of the variable created outside the function to the function parameters. This parameter transfer method can establish a real relationship between the function and the variables outside the function, that is, the variables outside the function can be directly operated inside the function.

When the array passes parameters, it passes not the entire address, but the address of the first element, so it can be received with a pointer

Formal parameters: for example, int arr[] = int* arr

5. Nested call and chained access of functions

Functions and functions can be combined according to actual needs, that is, they call each other

Note: functions can be called nested, but cannot be defined nested

Nested call example

#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;
}

5.2 chain access

Take the return value of one function as the parameter of another function.

#include <stdio.h>
#include <string.h>
int main()
{
    char arr[20] = "hello";
 int ret = strlen(strcat(arr,"bit"));    //strlen is the length of the calculated string up to \ 0
 printf("%d\n", ret);
 return 0;
}
#include <stdio.h>
int main()
{
    printf("%d", printf("%d", printf("%d", 43)));
    //What's the result?
    //Note: the return value of printf function is the number of characters printed on the screen
    return 0;
}

Think about it. The following chain code prints the result

printf("%d", printf("%d", printf("%d", 43)));

Note: the return value of printf is the number of int printed characters

Keywords: C Back-end

Added by jimmy2gurpreet on Mon, 21 Feb 2022 14:06:17 +0200