This article will introduce the related contents of C language functions
Basic concepts
A function is a part of a large code
It is composed of one or more statement blocks, which is responsible for completing a specific task and has a certain independence
Library functions encapsulate some frequently used functions into functions to improve development efficiency and have certain specifications
Custom function the function we created ourselves. The basic composition of the function is the return value function name (formal parameter 1, formal parameter 2...)
When defining a function, the parameters defined later are called formal parameters, and the actual parameters are the real values passed
When an argument is passed to a formal parameter, the formal parameter is a temporary copy of the argument
Library function example
Take two examples of library functions. Of course, you need to introduce the header file < string h>
1.strcpy(arr1,arr2) copies the contents of arr2 to arr1
Look at the code below
char arr1[20] = {0}; char arr2[] = "hello"; strcpy(arr1,arr2); printf("%s\n",arr1); //hello
2. Memset (arr, char, x, m) sets the memory block and sets the first M characters to corresponding characters
Look at the code below
char arr[] = "hello"; memset(arr,'x',2); printf("%s\n",arr); //xxllo
Examples of custom functions
1. Write a function to find the larger value of two numbers
int getmax(int a,int b){ return a>b?a:b; }
2. Write a function to exchange the values of two integer variables
Look at the following expression 1. We find that the values of a and B are not exchanged, which is the problem of calling this function by passing values
The reason is that the function reopens the space for the actual parameters passed in. In the end, the two spaces are irrelevant
Therefore, exchanging the values of two variables in the user-defined function has nothing to do with the two values in the main function
void swap1(int a,int b){ int temp = a; a = b; b = temp; }
The solution is to pass in the pointer variable (address call), which is the same memory space
Look at the writing 2
void swap2(int* a, int* b){ int temp = *a; *a = *b; *b = temp; }
Supplement: a function does not write the return type. It returns int type by default, but it should be standardized
Be sure to write the return type (void is also a return type)
3. Write a function. Every time you call this function, you will add 1 to the incoming integer num
void add(int* num){ (*num)++; }
4. Write a function to realize the binary search of ordered shaping array
Please look at the following code. Here is a small detail. Why should we pass the length of the array when passing parameters
The reason is that sizeof(arr)/sizeof(arr[0]) in the user-defined function cannot find the length of the array
Because when the passed parameter is an array, the address of the first element of the array will actually be passed in
Its essence is a pointer variable, and sizeof(arr) takes up 8 bytes on a 64 bit machine
int binary_search(int arr[],int key,int len){ int left = 0; int right = len - 1; int mid = 0; while(left <= right){ mid = (left + right)/2; if(arr[mid] == key){ return mid; } if(arr[mid] < key){ left = mid + 1; } if(arr[mid] > key){ right = mid - 1; } } return -1; }
Nested calls to functions
It is very simple to call other functions in the function again (note that nested calls are allowed in C language, but nested creation of functions is not allowed)
Please see the following code. The main function calls test1() and test2() in test1(), so "ha ha" will be printed at the end
void test2(){ printf("ha-ha"); } void test1(){ test2(); } int main(){ test1(); return 0; }
Chained access to functions
Chained access to a function is to take the return value of a function as an argument to another function
Look at the following code. This is chain access
#include<string.h> int main(){ printf("%d\n",strlen("abcd")); // 4 return 0; }
Look at an interesting piece of code
The printf() function starts from the rightmost parameter, and the return value is the number of characters printed on the screen
So this code will print 4321
int main(){ printf("%d",printf("%d",printf("%d",43))); //4321 return 0; }
Declaration and definition of functions
When the function is written below the calling function statement, the formal parameter name needs to be declared above. It can not be written (or written)
Look at the code
int sum(int,int); int main(){ printf("%d\n",sum(2,3)); //5 return 0; } int sum(int a , int b){ return a + b; }
Recursion of function
To put it simply, recursion is a function that calls itself and recurses itself
Large and complex problems can be transformed into small problems
Two necessary conditions for recursion
1. There is a restriction. When this restriction is met, the program will not continue recursion
2. Getting closer to this restriction after recursive call
Of course, pay attention to recursion and avoid stack overflow
**1. Enter a string of numbers and print each bit in sequence. For example, enter 1234 to print 1 2 3 4**
void print(unsigned int num){ //If it is a two digit number, it will be recursive if(num > 9){ print(num / 10); } printf("%d ",num % 10); }
2. Write a function to calculate the string length (simulate strlen)
Look at the following code (no recursion)
int my_strlen(char* str){ int count = 0; while(*str != '\0'){ count++; str++; } return count; }
3. It is not allowed to create temporary variables to calculate the string length when writing functions
It is not allowed to create temporary variables. You need to use recursion
int my_strlen(char* str){ if(*str != '\0'){ return 1 + my_strlen(str+1); }else{ return 0; //If yes \ nreturns 0 } }
4. Find the factorial of n
int fac(int n){ if(n == 1) return 1; return n * fac(n-1); }
Find the nth Fibonacci sequence
int fi(int n){ if(n == 1 || n == 2){ return 1; } return fi(n-1) + fi(n-2); }
Some function exercises
1. Print 99 multiplication table
void print(){ int i = 0; int j = 0; for(i = 1 ; i <= 9 ; i++){ for(j = 1 ; j <= i ; j++){ printf("%d*%d=%d\t",j,i,i*j); } printf("\n"); } }
2. Write a function to reverse the arrangement of the integer array
The idea of this problem is to traverse from both sides to the middle at the same time, and exchange the two sides in turn
void fun(int arr[],int len){ int i = 0; int j = 0; for(i = 0 , j = len - 1;i <= j; i++ ,j--){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
3. Write a recursive function, input a non negative integer and return the sum of its numbers
int fun(int n){ if(n == 0) return 0; return n % 10 + fun(n / 10) ; }
4. Write a recursive function to realize the k power of n
int fun(int n,int k){ if(k == 0) return 1; return n * fun(n,k-1); }