function
1. What is the function
2. Library function
3. User defined functions
4. Function parameters
5. Function call
6. Nested call and chained access of functions
7. Declaration and definition of functions
8. Function recursion
catalogue
Nested calls and chained access to functions
Declaration and definition of functions
Classification of functions in C language:
1. Library function
2. User defined function
The function is placed before the main function without + declaration
Library functions commonly used in C language
- IO function (input / output function)
- String operation function
- Character manipulation function
- Memory operation function
- Time / date function
- Mathematical function
- Other library functions
strcpy:
#include <stdio.h> #include <string.h> int main() { char arr1[] = "bit"; char arr2[20] = "############"; // Bit - > bit \ 0 ############# since \ 0 is the end flag, the following # will not be printed strcpy(arr2, arr1); printf("%s\n", arr2); //strlen - string length - string length //strcpy - string copy - string copy return 0; }
memset:
memory + set
#include <stdio.h> #include <string.h> int main() { char arr[] = "hello world"; memset(arr, '*', 5); //The array stored in '*' is the ACSll code value, 42, which is also an integer printf("%s\n", arr); return 0; }
Custom functions:
Function composition:
ret_type fun_name(para1, * ) { statement;//Statement item } ret_type Return type fun_name Function name para1 Function parameters
example:
#include <stdio.h> //get_ Design of Max function int get_max(int x, int y) { if(x>y) return x; else return y; } int main() { int a = 10; int b = 20; int max = get_max(a, b); printf("max = %d\n", max); return 0; }
Swap two integers with a function:
#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 a = 10; int b = 20; printf("a = %d b = %d\n", a, b); Swap1(a, b); //Call Swap1 function - pass value call Swap2(&a, &b); //Call Swap2 function - address call printf("a = %d b = %d\n", a, b); return 0; }
Parameters of function
Actual parameter (actual parameter):
The parameters 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.
Formal parameter (formal parameter):
Formal parameters refer to the variables in parentheses after the function name. They are called formal parameters because they are instantiated (memory unit allocation) only in the process of function dual-use. Formal parameters are automatically destroyed when the function call is completed. Therefore, formal parameters are only valid in functions.
The parameters X. y. PX in the above Swap1 and Swap functions Py are formal parameters. Num1 and num2 passed to Swap1 in the mian function and & num1 and & num2 passed to Swap2 function are actual parameters
Function call:
Call by value:
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.
Note: the value transfer function only needs the final result value, while the address transfer function only needs some practical operations on the arguments, such as exchange
practice:
1. Write a function to judge whether a number is a prime number
#include <stdio.h> #include <math.h> //A prime number returns 1, not a prime number returns 0 int is_prime(int n) { //2->n-1 int j = 0; for(j=2; j<=sqrt(n); j++) { if(n%j == 0) return 0; } //if(j == n) return 1; } int main() { int i = 0; for(i=100; i<=200; i++) { //Judge whether i is prime if(is_prime(i) == 1) printf("%d ", i); } return 0; }
2. Write a function to judge whether a year is a leap year
#include <stdio.h> int is_leap_year(int y) { if((y%4 == 0 && y%100 != 0) || (y%400 == 0)) return 1; else return 0; } int main() { int year = 0; for(year= 1000; year<=2000; year++) { //Determine whether yaer is a leap year if(1 == is_leap_year(year)) { printf("%d ", year); } } return 0; }
3. Write a function to realize the binary search of an integer ordered array
#include <stdio.h> //Essentially, arr is a pointer int binary_search(int arr[], int k, int sz) { //Implementation of algorithm int left = 0; int right = sz-1; while(left<=right) { int mid = (left+right)/2;//Subscript of intermediate element if(arr[mid] < k) { left = mid-1; } else if(arr[mid] > k) { right = mid - 1; } else { return mid; } } return -1; } int main() { //Binary search //Find a specific number in an ordered array //If found, return the subscript of the book. Return - 1 not found int arr[] = {1,2,3,4,5,6,7,8,9,10}; int k = 7; int sz = sizeof(arr)/sizeof(arr[0]); //The address of the first element of arr [] is passed in the past int ret = binary_search(arr, k, sz); if(ret == -1) { printf("The specified number could not be found\n"); } else { printf("eureka,The subscript is:%d\n",ret); } return 0; }
4. Write a function. Every time this function is called, the value of num will be increased by 1
#include <stdio.h> void Add(int* p) { (*p)++; } int main() { int num = 0; Add(&num); printf("num = %d\n", num); Add(&num); printf("num = %d\n", num); Add(&num); printf("num = %d\n", num); return 0; }
Nested calls and chained access to functions
Functions and functions can be organically combined.
Nested Call
#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(); getchar(); return 0; }
Chain access
Take the return value of one function as an argument to another function.
#include <stdio.h> #include <string.h> int main() { //1 int len = 0; len = strlen("abc"); printf("%d\n", len); //2 printf("%d\n", strlen("abc")); getchar(); return 0; }
#include <stdio.h> int main() { printf("%d", printf("%d", printf("%d", 43))); //4321 //The printing order is from the innermost right bracket to the left //1. Print out 43 first, //2. Because 43 is 2 digits, the return value is 2. The second print 2 is after 43, that is, 432 at this time //3. Because 2 is 1 digit, the return value is 1. The third time 1 is printed after 2, so the final print result is 4321 return 0; }
Declaration and definition of functions
Function declaration
- 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
- The declaration of a function usually appears before the use of the function (it should be declared before use)
- 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
Header file h release function declaration, source file c put the definition of the function and run it in multiple files with function calls