Functions are snippets of code that can be reused.
Library function (if it is written by others, you can use it directly)
Standard library functions printf, strlen,system
System library function Sleep
Many third-party library functions can be found on github
Write your own function
Function composition:
ret_type fun_name(para1, * ) { statement;//Statement item } ret_type Return type fun_name Function name para1 Function parameters
Take a chestnut~
//Write a function to find the maximum value of two integers #include<stdio.h> int getMax(int x, int y){ //Formal parameter if(x > y){ return x; }else{ return y; } } int main(){ int ret = getMax(10,20); //Argument printf("ret = %d\n",ret); return 0; }
When the program runs to getMax, it will return to the getMax function we set, assign 10 and 20 to x and y respectively, and then run to get the result.
Every time a function is called, there is no influence on each other.
//Write a function that exchanges two numbers #include<stdio.h> void swap(int* x,int* y){ //Dereference operation ~ (proximity access) //x = &a //Here * x and a are equivalent //y = &b //Here * y and b are equivalent int tmp = *x; *x = *y; *y = tmp; } int main(){ int a = 10; int b = 20; swap(&a,&b); printf("a = %d,b = %d\n",a,b); return 0; }
int integer
int * pointer
Relationship between arguments and formal parameters:
A formal parameter is a copy of an argument~
If we modify the formal parameter, it has no effect on the argument~
We can use pictures to describe the operation process of the above functions.
First, the address of a and b is taken, so the address 0x100 of a is stored in X and the address 0x200 of b is stored in y. first, assign * x to tmp. According to the address stored in X, a can be found and the value of tmp is 10
Then assign the value of * y to * x, and b can be found through the address stored in y. therefore, the value of b is given to A. at this time, a = 20, b = 20, TMP = 10
Then assign the value of tmp to * y, and then you can get a = 20 and b = 10. Through the operation of * x, * y indirect access, the exchange of a and b is completed. x. Y itself has not changed.
Pointer must be combined with "memory" to understand!!!
practice:
- Write a function to judge whether a number is a prime number.
//Suppose you give a number num, if it is a prime number, from 2 to num. at this time, no integer in the middle can be divided by num. Once you find a number that can be divided by num, the num is not a prime. #include<stdio.h> //The return value is set to int. if it is a prime number, it returns 1. If it is not, it returns 0 // is it a prime number? int isPrime(int num){ if(num == 0 || num == 1){ return 0; } for(int i = 2;i < num;i++){ if(num % i == 0){ //This situation is a counterexample return 0; } } //There is no counterexample at the end of the loop. At present, it is considered to be prime return 1; } int main(){ printf("%d\n",isPrime(19)); return 0; }
- Write a function to judge whether a year is a leap year.
#include<stdio.h> int isLeapYear(int year){ if(year % 100 ==0){ if(year % 400 == 0){ return 1; } return 0; }else{ if(year % 4 ==0){ return 1; } return 0; } } int main(){ print("%d\n",isLeapYear(2021)); return 0; }
- Write a function to realize the binary search of an integer ordered array.
//int arr [] in the parameter is converted to pointer int* //The second amazing bug in C #include<stdio.h> int binarySearch(int* arr,int size, int toFind){ int left = 0; int right = size-1; while(left <= right){ int mid =(left + right) / 2; if(toFind < arr[mid]){ right = mid - 1; }else if(toFind > arr[mid]){ left = mid + 1; }else{ return mid; } } //If the above loop fails to find the appropriate element in the end, it is considered that it has not been found. //Returns an invalid subscript, return -1; } int main(){ int arr[] = {1,2,3,4,5,6,7}; int size = sizeof(arr) / sizeof(arr[0]); int ret = binarySearch(arr,size,6); printf("ret = %d\n",ret); return 0; }
The array in C is easily converted into a pointer (pointer to the first element).
The second amazing bug in C.
Many times, pairs will appear, when functions pass parameters and participate in operations
- Write a function. Every time you call this function, you will increase the value of num by 1.0
#include<stdio.h> void func(int* num){ (*num)++; } int main(){ int num = 0; func(&num); printf("%d\n",num); return 0; }
Declaration and definition of functions
//If the function is written elsewhere, it must be declared and defined. c language compilation is compiled from top to bottom //First code void func(int* num){ (*num)++; } //Second code //If we want to use the first code in the second code, we just need to add extern void func(int* num); //That's it
Function recursion
A function calls itself, which is called recursion.
Recursive code has a feature. The code looks like only a few lines. In fact, the execution process may be very complex.
You can use drawing + debugging when understanding
Recursive example.
//Print out 1 2 3 4 in order for an integer 1234 #include<stdio.h> void printNum(unsigned int num){ if(num > 9){ printNum(num / 10); } printf("%d ",num % 10); } int main(){ printfNum(1234); return 0; }
It can be understood by drawing
#include<stdio.h> int myStrlen(char* str){ //Start from the beginning of the string and look back once, \ 0 //Each time a character is encountered, count if it is not \ 0++ //When \ 0 is encountered, the cycle ends and count is returned int count = 0; while(str[count] != '\0'){ count++; } return count; } int main(){ printf("%d\n",myStrlen("abcd")); return 0; }
//If you don't want to create local variables or use loops, you can consider using recursion #include<stdio.h> int myStrlen2(char* str){ if(str[0] == '\0'){ return 0; } return 1 + myStrlen2(str + 1); } int main(){ printf("%d\n",myStrlen2("abcd")); return 0; }