Basic operation of C language function

Article catalogue

preface

This paper introduces the basic knowledge of C language functions, including function classification, definition and declaration, nested call, chain access and recursion. Each call is accompanied by a simple program.

Tip: the following is the main content of this article. The following cases can be used for reference

1, What is a function?

stay computer science Subroutine( English : 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.

Function is equivalent to a subroutine. Some library functions are provided in C language. Users can also customize functions to realize some functions. When the general program needs to execute this function, you can call the function directly.

2, Function classification and call

Library function: a series of statements provided in the basic library of C language, which are convenient for programmers to develop, such as printf() printout, scanf() input, etc. To put it bluntly, the function statements we usually use in development are library functions, which have been defined in C language and can be used directly, but some functions need to introduce header files before use, such as #include < stdio h>.

Common library functions include: IO function, character operation function, string operation function, memory operation function, time / date function, mathematical function, etc.

For library function learning, you can log in to www.cplusplus.com COM, which contains C and C + + library functions. Direct search will have corresponding introduction, header files, usage methods and examples.

User defined function: this type of function is designed by the user to realize some functions. When defined, it contains four elements: return type, function name, incoming parameter and function body.

#include<stdio.h>

int Getmax(int x, int y)//Return type int, function name Getmax, passed in parameters x, y
{
	int z = 0;//Function body
	if (x >= y)
		z = x;
	else
		z = y;
	return z;
}

int main()
{
	int result=Getmax(5, 10);//Function call to get the maximum values of 5 and 10
	printf("%d", result);
	return 0;
}

Based on the above program, introduce the formal parameters and arguments. When calling a custom function, you need to pass parameters, which are divided into arguments (5 and 10 of the above program) and formal parameters (A and b),

Arguments can be constants, variables, expressions and functions. No matter what type, they must have a definite value. When the function is called, the value is passed to the user-defined function.

Only when the function is called, the formal parameters are divided into memory units to store the incoming values. After the function is called, they are automatically destroyed. Therefore, the formal parameters are only meaningful in the function.

The parameter transfer process is divided into value transfer and address transfer. The above program belongs to value transfer call. Refer to the following program for the difference between the two.

#include<stdio.h>

void Exchange1(int x, int y)//void has no return value and cannot be transferred
{
	int a = 0;
	a = x;
	x = y;
	y = a;
	printf("The value after exchange is%d and%d\n", x, y);
}
void Exchange2(int* x, int* y)//void, no return value, address transfer
{
	int a = 0;
	a = *x;
	*x = *y;
	*y = a;
	printf("The value after exchange is%d and%d\n", *x, *y);
}

int main()
{
	int a = 5;
	int b = 10;
	Exchange1(a,b);//Pass in the values of a and b
	printf("a The value of is%d,b The value of is%d\n", a, b);//Output the values of a and b after exchange
	Exchange2(&a, &b);//Pass in a and b addresses
	printf("a The value of is%d,b The value of is%d\n",a,b);
	return 0;
}

Call by value: the formal parameter takes up different memory blocks, and the change of the formal parameter will not affect the actual parameter. For example, exchange 1 () passes the values of a and b to the user-defined function, and the operations x and y in the function do not actually change the values of a and b,

Address calling: it is to pass the memory address of the variable to the function parameters. This way can make the function establish a real connection with the variables outside the function, that is, the function can operate the variables outside the function. If exchange 2 () passes the addresses of a and b to the user-defined function, the operation in the function will change the values of a and b.

The result of the above code is as follows:

 

1. Nested call of user-defined functions

Call another function in a function. Function can not be nested but can be nested. An example is as follows: exchange 1() is nested in exchange 2().

#include<stdio.h>

void Exchange1(int x, int y)//void has no return value and cannot be transferred
{
	int a = 0;
	a = x;
	x = y;
	y = a;
	printf("The value after exchange is%d and%d\n", x, y);
}
void Exchange2(int* x, int* y)//void, no return value, address transfer
{
	int a = 0;
	int b = 0;
	a = *x;
	b = *y;
	Exchange1(a, b);
}

int main()
{
	int a = 5;
	int b = 10;
	Exchange2(&a, &b);//Pass in a and b addresses
	printf("a The value of is%d,b The value of is%d\n",a,b);
	return 0;
}

2. Chain access of user-defined functions

Chain access is to take the return value of one function as the parameter of another function. Take two library functions as an example: strlen() is a library function for calculating the length of character segments, and pass its return value as a parameter to printf() function for output.

#include<stdio.h>

int main()
{
	int len = strlen("abc");
	printf("Count Reg%d\n", len);
	printf("Count Reg%d\n", strlen("abc"));
	return 0;
}

 

3. Recursive call of user-defined functions (key points)

The programming skill of the program calling itself is called recursion. Recursion as a algorithm stay Programming language Widely used in. A process or function In its definition or description, there is a method to call itself directly or indirectly. It usually transforms a large and complex problem layer by layer into a smaller problem similar to the original problem. The recursive strategy can describe the multiple repeated calculations required in the problem-solving process with only a small amount of program, which greatly reduces the amount of code of the program. The important idea is to make big things small.

Two necessary conditions for recursion:

There is a limiting condition. When this condition is not met, recursion will not continue

Each recursive call is getting closer to the limiting condition, that is, the program cannot recurse indefinitely

The function calls itself repeatedly, and "passes" down layer by layer until the limit conditions are met, and then "returns" back layer by layer. The example procedure is as follows:

Define the Math() function to output the integer passed in bit by bit. If 1235 is just passed in and is greater than 9, divide by 10, and then go to the next layer 123, and divide by 10 again; Until the fourth layer is 1, if it is not greater than 9, execute the following program to print 1, return to the third layer, print 12 divided by 10 and 2, return to the second layer, print 123 divided by 10 and 3, return to the fourth layer, and print 1234 divided by 10 and 4.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>


void Math(int a)
{
	if (a > 9)
	{
		Math(a / 10);
	}	
	printf("%d\n", a % 10);
}

int main()
{
	printf("Please enter an integer:\n");
	int number = 0;
	scanf("%d", &number);//Receive input value
	Math(number);
	return 0;
}

 

summary

The above is the basic content of C language functions. If you provide some help, you will not be responsible for this editing. Next, the application examples of functions will be updated.

Keywords: C

Added by Right Wing Liberal on Tue, 15 Feb 2022 07:31:47 +0200