C language: function

Posted by Rowno on Mon, 03 Jan 2022 03:01:39 +0100

1. What is the function

Wikipedia is defined as: subroutine

2. Library function

The header file must be referenced when using library functions

C + + library function interpretation link
Here, the library function string assignment function strcpy is parsed

//String assignment function strcpy
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main() {
	char str1[20] ="xxxxxxxxx";
	printf("%s\n", str1);
	char str2[20]= "hello";
	strcpy(str1, str2);
	printf("%s\n", str1);
	return 0;
}

String end flag '\ 0' does not take up length but space

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main() {
	char str[] = "hello bit";
	memset(str, 'x', 5);
	printf("%s\n", str);

}

3. User defined functions

Library functions cannot solve all requirements, so you need to customize functions
Like library functions, there are return types, function names, and function parameters
The following is a custom function for exchanging two numbers

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void swap(int a, int b);
int main() {
	int a = 10;
	int b = 20;
	swap(a, b);
	printf("%d %d",a,b);
	return 0;
}
void swap(int a, int b) {
	a = a - b;
	b = a + b;
	a = b - a;
	printf("%d %d\n", a, b);
}

As a result of the above program running, we found that although our custom swap function realizes the exchange and output of two numbers, the A and b output in the main function are not exchanged. This requires the call of the following function to explain why there is no exchange in the main function and how to implement it

4. Parameters of function

4.1 actual parameters (actual parameters)

The parameters 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 making a function call in order to pass these values to the formal parameters.

4.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 when the function call is completed. Therefore, formal parameters are only valid in functions.
From the execution of the above function code, we can know that the formal parameter is a temporary copy of the argument. Changing the value of the formal parameter will not change the value of the argument.

5. Function call

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.

Address call

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 true relationship between the function and the variables outside the function, that is, the variables outside the function can be directly operated inside the function.
The above function is a value passing call. Such a call will not change the value of the argument due to the temporary copy of the formal parameter knowledge argument. However, the value of the argument can be changed through the address call.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void swap(int a, int b);
int main() {
	int a = 10;
	int b = 20;
	swap(&a, &b);//Pass the addresses of a and b
	printf("%d %d", a, b);
	return 0;
}
void swap(int *a, int *b) {
	*a = *a - *b;
	*b = *a + *b;
	*a = *b - *a;
}

lower
The following is the execution result. We can see that the values of a and b have been exchanged.

6. Nested call and chained access of functions

Functions and functions can be organically combined.

Nested Call

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void funtion1(int x, int y) {

}
void funtion2(int x, int y) {
	funtion1(x, y);
}
void function3(int x, int y) {
	function2(x, y);
}
int main(){
	int a, b;
	function3(a, b);
	return 0;
}

The above is the nested use of functions

Chain access

Take the return value of one function as an argument to another function.

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

Here, we directly input the strlen return value as a parameter into the printf function.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    printf("%d", printf("%d", printf("%d", 43)));
    //What's the result?
    return 0;
}

Output 4321;

7. Declaration and definition of functions

Function declaration

  1. Tell the compiler what a function is called, what the parameters are and what the return type is. But it doesn't matter whether it exists or not.
  2. The declaration of a function usually appears before the use of the function. To meet the requirements of declaration before use.
  3. The declaration of the function is usually placed in the header file.

Function definition

      The definition of function refers to the specific implementation of the function and explains the function implementation of the function.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void swap(int x, int y) {

}
int main() {
	int a=10, b=20;
	swap(a,b);
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void swap(int x, int y);
int main() {
	int a=10, b=20;
	swap(a,b);
	return 0;
}
void swap(int x, int y) {

}

Note: function definition code blocks cannot be written in other functions.

8. Function recursion

What is recursion

The programming technique of a program calling itself is called recursion. As an algorithm, recursion is widely used in programming languages. A process or function has a method to call itself directly or indirectly in its definition or description. 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 number of programs, which greatly reduces the amount of code of the program. The main way of thinking about recursion is to make big things small. For example, when seeking factorial and N, we only need to find n-1, and N-1 only needs n-2. Finally, we program to find 1. The code is as follows

int fact(int n) {
	if (n <= 1)
		return 1;
	else
		return n * fact(n - 1);
}

Can you just run

#include<stdio.h>
int fact(int n) {
	if (n <= 1)
		return 1;
	else
		return n * fact(n - 1);
}
int main() {
	int ret = fact(5);
	printf("%d", ret);
	printf("\n\n\n\n\n\n\n");
	return 0;
}

recursive thinking

As long as the recursive idea is solved, it is easy to write a program. For example, in the tower of Hanoi, the number of times to remove n is f(n), and the number of times to remove n-1 is f(n-1). What is the relationship between the two? Obviously, f(n)=2f(n-1)+1, so it is easy to write recursive programs.

Topics: C Programming