Some Tips of function

In mathematics, we often see functions, but what are the functions in C language? In Wikipedia, Functions are interpreted in this way: functions are also called subroutines (Subroutine, procedure, function, routine, method, subprogram, callable unit) 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. General functions have input parameters and return values, providing encapsulation of the process and hiding of details . These codes are usually integrated into software libraries.
In C language, functions are divided into library functions and user-defined functions: library functions such as printf, strcpy and other commonly used basic functions. In order to realize their functions, if they need to be customized, such code functions will become diverse. Therefore, C language puts forward the concept of library function, He encapsulates some functions of basic functions into the basic library of C language. Programmers do not have to write them again, but can call them directly, which not only saves time, but also makes the program more standardized.
However, it is impossible to realize all functions in the C language library. When programmers need to meet their additional needs, they need to customize functions at this time. 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.
Composition of custom functions:
ret_type fun_name(para1, * )
{
statement;// Statement item
}
ret_type return type
fun_name function name
Param1 function parameters

One advantage of a user-defined function is that after it is written, it can be called directly in the process of use to realize its function. For example, we want to find the larger value of two numbers

#include<stdio.h>
int get_max(int a, int b)
{
	return (a > b) ? a : b;
}

int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	//Find the larger value
	int max = get_max(a, b);
	printf("max = %d\n", max);

	return 0;
}

With get_ After a function such as Max, when you need to find a larger value in the future, you only need to declare it first and then call it directly.

**

Arguments to function:

**
1. Actual parameters (arguments): the parameters that are really 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 calling the function, so as to pass these values to the formal parameters.
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 (allocating memory units) only when the function is called. Formal parameters are automatically destroyed after the function call is completed. Therefore, formal parameters are only valid in the function.
Specific examples are as follows:

#include <stdio.h>
void Swap1(int x, int y)
{
 int tmp = 0;
 tmp = x;
 x = y;
 y = tmp;
 }
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 parameters x, y, px and py in the above Swap1 and Swap2 functions are formal parameters. Num1 and num2 passed to Swap1 in the main function and & num1 and & num2 passed to Swap2 function are actual parameters.
The memory of function arguments and formal parameters is analyzed as follows:

Here we can see as like as two peas, Swap1 and x have their own space, and have the same content as the real reference, when the function of the y is called. So we can simply think that after the formal parameter is instantiated, it is actually equivalent to a temporary copy of the argument.

**

Function call:

**
1. Value passing call: 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.

#include<stdio.h>
void Swap(int a, int b)
{
	int c = 0;
	c = a;
	a = b;
	b = c;
	printf("After exchange:a = %d,b = %d\n", a, b);
}
int main()
{
	int a = 10;
	int b = 20;
	printf("Before exchange:a = %d,b = %d\n", a, b);
	//Exchange function
	Swap(a, b);//Value passing call
	//When arguments a and b are passed to a parameter, the parameter is a temporary copy of the argument
	//Changing the formal parameter variable will not change the argument
	return 0;
}


The running results show that the values of a and b are not exchanged, and this is the value passing call

2. Address calling: address calling 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.

#include<stdio.h>
void Swap(int* px, int* py)
{
	int z = 0;
	z = *px;
	*px = *py;
	*py = z;
}


int main()
{
	int a = 10;
	int b = 20;
	printf("Before exchange:a = %d,b = %d\n", a, b);
	//Exchange function
	Swap(&a, &b);//Address call
	printf("After exchange:a = %d,b = %d\n", a, b);
	return 0;
}


At this time, it is the address call. When calling the function, the address is passed in, so the real connection can be established inside and outside the function, and the external variables can be modified inside the function.

Nested access and chained calls to functions:

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

Nested calls are called another sub function in a sub function, but it should be noted that functions can be nested but not nested, that is, another function is defined in the function.

2. Chained access:

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

Chain access refers to taking the return value of one function as the parameter of another function, for example:

#include <stdio.h>
int main()
{
    printf("%d", printf("%d", printf("%d", 43)));
    //What's the result?
    //Note: the return value of the printf function is the number of characters printed on the screen
    return 0;
 }

The return value of printf is the number of characters. In chain access, the first printf prints 43, and then passes in the next printf. At this time, the return value is 2, and then passes in the next printf. Therefore, the final print result is 4321

Keywords: C

Added by noaksey2000 on Thu, 23 Dec 2021 12:25:37 +0200