Function (C language)

1. What is the function?

1) A function is a part of code in a large program, which is composed of one or more statement blocks. It is responsible for completing a specific task and has relative independence compared with other codes.

2) Generally, there are input parameters and return values, which provide encapsulation of the process and hiding of details. These codes are usually integrated into software libraries.

2. Library function

1) Basic knowledge of library functions

Why are there library functions?

  1. We know that when we learn C language programming, we can't wait to know the results after a code is written
    Print the result on our screen. At this time, we will frequently use a function: according to a certain grid of information
    Print to screen (printf).
  2. In the process of programming, we will frequently do some string copy (strcpy).
  3. In programming, we also calculate, always calculate the k power of n (pow).
    Like the basic functions we described above, they are not business code. Every programmer may use it in our development process,
    In order to support portability and improve program efficiency, a series of similar library functions are provided in the basic library of C language to facilitate programmers' software development.

How do you learn library functions?
Do you need to remember it all? No
You need to learn how to use query tools:
MSDN(Microsoft Developer Network) (English version) (Chinese version)
If you encounter a function you don't understand, check it

2) Some library functions

The search results at are as follows


int main()
	char ch1[] = "hello bit";
	char ch2[20] = { 0 };
	strcpy(ch2, ch1);
	printf("%s\n%s\n", ch1, ch2);
	return 0;

The search results at are as follows:

int main()
	char ch[] = "hello bit, you are strong!";
	memset(ch, 'o', 5);//Fill the five spaces behind the memory pointed to by ch with 'o'
	printf("%s\n", ch);
	return 0;

3. User defined functions

If library functions can do everything, what do programmers do?
All the more important is custom functions.
Like library functions, custom functions have function names, return value types, and function parameters.
But the difference is that these are designed by ourselves. This gives programmers a lot of room to play.

Composition of custom functions

ret_type fun_name(para1, * )
 statement;//Statement item
ret_type Return type
fun_name Function name
para1    Function parameters

For example:

#include <stdio.h>
//get_ Design of Max function
int get_max(int x, int y) {
 return (x>y)?(x):(y);
int main()
 int num1 = 10;
 int num2 = 20;
 int max = get_max(num1, num2);
 printf("max = %d\n", max);
 return 0

4. Function parameters

1) actual parameters (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.

2) 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 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 in functions
Example analysis:

//Swap the values of two numbers
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 num1 = 1;
	int num2 = 2;
	Swap1(num1, num2);
	printf("Swap1::num1 = %d num2 = %d\n", num1, num2);
	Swap2(&num1, &num2);
	printf("Swap2::num1 = %d num2 = %d\n", num1, num2);
	return 0;
above Swap1 and Swap2 Parameters in function x,y,px,py Are formal parameters.
stay main Function passed to Swap1 of num1,num2 And pass it on to Swap2 Functional&num1,&num2 Is the actual parameter.

It can be seen from this: in the past, the word of the transmission address can make the function contact with the outside of the function

5. Function call

1) 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.

2) 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 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.

3) Practice

1. Write a function to judge whether a number is a prime number

int Is_prime(int x)
	int i = 0;
	for (i = 2; i <= sqrt(x); i ++)
		if (x%i == 0)
			return 0;
	return 1;

int main()
	int num = 0;
	scanf("%d", &num);
	int ret = Is_prime(num);
	printf("%d\n", ret);
	if (ret)//0 is false, others are true
		printf("It's a prime\n");
		printf("Not prime\n");
	return 0;

2. Write a function to judge whether a year is a leap year.


3. Write a function to realize the binary search of an integer ordered array

int binary_search(int arr[], int k, int sz)
	int left = 0;
	int right = sz-1;
	while (left <= right)
		int mid = (left + right) / 2;
		if (arr[mid] > k)
			right = mid - 1;
		else if (arr[mid] < k)
			left = mid + 1;
			return mid;
	return -1;
int main()
	int arr[10] = { 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);//arr is the address of the first element of the array, k is the number to find, and len is the size of the array
	if (ret == -1)
		printf("can't find\n");
		printf("Found, subscript%d\n", ret);
	return 0;

4. Write a function. Every time this function is called, the value of num will be increased by 1

6. Nested call and chained access of functions

7. Declaration and definition of functions

8. Function recursion

Tags: C C++

Posted on Mon, 01 Nov 2021 00:07:46 -0400 by Flukey