Function classification of C language:
1. Library function:
1.1 IO function
1.2 string operation function
1.3 character operation function
1.4 memory operation function
1.5 time / date function
1.6 mathematical functions
1.7 other library functions
How to learn to use other unfamiliar library functions?
Search site: cplusplus.com - The C++ Resources Network
Baidu once, you know (doge)
Example:
Have you learned? Let's give it a try~
#include<stdio.h> #include<string.h> int main() { char arr1[] = "bit"; char arr2[20] = "#####"; strcpy(arr2, arr1); printf("%s\n", arr2); return 0; }
How to learn to use library functions?
Do you need to remember it all? Leak! Just use the query tool:
MSDN(Microsoft Developer Network)
cplusplus.com - The C++ Resources Network
2. User defined function
Like library functions, custom functions have function names, return value types, and function parameters. But we need to design it ourselves.
Function composition:
ret_type fun_name(paral, *)
{
statement;// Statement item
}
ret_type return type
fun_name function name
Parameter of paral lel function
Give it a try:
Function for finding the maximum value
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; }
Write another function
Functions that exchange values:
Does this code exchange the values of a and b?
int main() { int a = 10; int* pa = &a;//pa pointer variable *pa = 20;//dereference operator printf("%d\n", a); return 0; }
void Swap(int* pa, int* pb)//void means null, that is, there is no return value { int tmp = 0; tmp = *pa; *pa = *pb; *pb = tmp; } int main() { int a = 10; int b = 20; Swap(&a, &b); printf("a=%d,b=%d\n", a, b); return 0; }
Here are some knowledge points:
Formal parameter (formal parameter)
The parameter appearing in the function definition can be regarded as a placeholder. It has no data and can only receive the data passed in when the function is called. Therefore, it is called formal parameter for short.
Argument (actual parameter)
The parameters given when the function is called contain real data and will be used by the code inside the function, so they are called actual parameters for short.
The parameters x, y, pa and pb in the above function are formal parameters, and num1 and num 2 passed to the swap function in the main function are actual parameters.
Note: when an argument is passed to a formal parameter, the formal parameter is actually a temporary copy of the argument, and the modification of the formal parameter will not change the argument.
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.
Value passing call: Value Passing 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 real relationship between the function and the variables outside the function, that is, the variables outside the function can be directly operated inside the function.
Value passing call
Address calling
practice:
Write a function to judge whether it is a prime number
int is_prime(int n) { int j = 0; for (j = 2; j < n; j++) { if (n % j == 0) return 0; } 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; }
Binary search
int binary_search(int arr[], int k, int sz) { //Implementation of algorithm int sz = sizeof(arr) / sizeof(arr[0]); 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 the following table that returns this number is found, if not, return - 1 int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; int k = 7; int sz = sizeof(arr) / sizeof(arr[0]); int ret = binary_search(arr, k, sz); if (ret == -1) { printf("The specified number cannot be found\n"); } else { printf("Found, subscript:%d\n", ret); } return 0; }
Nested call and chained access of functions
Functions and functions can be organically combined
Nested Call
void new_line() { printf("hehe\n"); } void three_line() { int i = 0; for (i = 0; i < 3; i++) { new_line(); } } int main() { three_line; return 0; }
Chain access
Take the return value of one function as the parameter of another function
int main() { int len = 0; //1 len = strlen("abc"); printf("%d\n", len); //2 printf("%d\n", strlen("abc")); return 0; }
practice:
What is the output value of the following code?
int main() { printf("%d", printf("%d", printf("%d",43))); return 0; }
answer; four thousand three hundred and twenty-one
Declaration and definition of functions
The declaration of a function is usually placed in In the h header file, function calls can be placed in c source file. Functions should be declared before use.
Recursion of function
Recursive function is a function of "calling yourself", which can be called directly or indirectly. Indirect recursion means that a function calls another function (then possibly a third function, etc.) and finally the first function. Because a function cannot call itself all the time, a recursive function must have an end condition. The main way of thinking about recursion is to make big things small.
Necessary conditions for recursion:
1. There is a constraint. When this constraint is met, recursion will not continue.
2. Get closer to this limit after each recursive call.
practice:
Accept an integer value (unsigned) and print its bits in order. For example: input 123, output 1 2 3
Reference code:
void print(int n) { if (n > 9) { print(n / 10); } printf("%d ", n % 10); } int main() { unsigned int num = 0; scanf("%d", &num); print(num); return 0; }
Exercise 2:
Writing a function is not allowed to create a temporary variable to find the length of the string
/*int my_strlen(char* str) { int count = 0; while (*str != '\0') { count++; str++; } return count; }8*/ int my_strlen(char* arr) { if (*str != '\0') return 1 + my_strlen(str + 1); else return 0; } int main() { char arr[] = "bit"; int len = my_strlen(arr);//arr 'is an array. The array passes parameters. Instead of the entire array, it passes the address of the first element printf("len = %d\n", len); return 0; }
Recursion and iteration
Describe the nth fimonache sequence
int Fib(int n) { if (n <= 2) return 1; else return Fib(n - 1) + Fib(n - 2); } int main() { int n = 0; int ret = 0; scanf("%d", &n); //TDD Test Driven Development ret = Fib(n); printf("ret = %d\n", ret); return 0; }
Classic topic of function recursion (you can study it independently if you are interested)
1. Tower of Hanoi
2. Frog jumping steps