Take you to easily play with C language functions

content

 

What is a function?

Classification of functions in C language

Examples of library functions

strcpy() function

 memset() function

custom function

function arguments

Actual parameters (actual parameters):

Formal parameters (formal parameters):

function call:

call by value

call by address

example

 Write a function to check if a number is a leap year

 Write a function to determine whether a year is a prime number

Write a function that implements a binary search of an integer sorted array. 

boolean type

nested calls

chained access

function return value

Parameters of main function


 

What is a function?

In mathematics, we often see the concept of functions. But do you understand functions in C language? Wikipedia's definition of a function: Subroutine In computer science, a subroutine (English: Subroutine, procedure, function, routine, method, subprogram, callable unit) is a portion of code in a large program consisting of one or more A block of statements. It is responsible for completing a specific task and is relatively independent from other code. Usually there are input parameters and return values, providing encapsulation and hiding of details of the process. These codes are usually integrated as software libraries.

Classification of functions in C language

1. Library functions (library functions provided by C language) 2. Custom functions (functions written by yourself)

Examples of library functions

Click here to view all library functions

strcpy() function

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

Features:

copy string

Copies the C string pointed to by source into the array pointed to by destination , including the terminating null character (and stopping at that point). Copy (a,b) b to a, including \0 of b, will overwrite the content of a

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

 memset() function

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

Function: Set  the first num bytes of  the memory block pointed to by ptr to the specified value (interpreted as an unsigned character), and set the num bytes backward of ptr to value

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

custom function

ret_type fun_name(para1, * )

{ statement; // statement item }

ret_type return type

fun_name function name

para1 function parameter

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

The functions are not swapped at this point because: when the actual parameter is passed to the formal parameter, the formal parameter is a temporary copy of the actual parameter

, modifications to the formal parameters do not affect the actual parameters.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

If we want the formal parameter to have an effect on the actual parameter, we should make a pass-by-address call , so that we can correctly implement the function we want 

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

function arguments

Actual parameters (actual parameters):

The actual parameters passed to the function are called actual parameters. Arguments can be: constants, variables, expressions, functions, etc. Regardless of the type of quantities the actual parameters are, they must have definite values ​​in order to pass those values ​​to the formal parameters when the function is called.

Formal parameters (formal parameters):

Formal parameters refer to the variables in parentheses after the function name, because the formal parameters are only instantiated (allocated memory units) in the process of the function being called, so they are called formal parameters. Formal parameters are automatically destroyed when the function call is complete. So formal parameters are only valid within functions.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_15,color_FFFFFF,t_70,g_se,x_16

When an actual parameter is passed to a formal parameter, the formal parameter is a temporary copy of the actual parameter, and modifications to the formal parameter cannot change the actual parameter

function call:

call by value

The formal parameters and actual parameters of the function occupy different memory blocks respectively, and the modification of the formal parameters will not affect the actual parameters.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

In this case, a call by value is performed. When entering the function after the call by value, two different variables will be automatically created, and these two variables will be operated within the function. When the function ends, these two variables will be released. , so modifying the formal parameter will not have any effect on the actual parameter

call by address

Call by reference is a way to call a function by passing the memory address of a variable created outside the function to the function parameter. This method of passing parameters allows the function to establish a real connection with the variables outside the function, that is, the function can directly operate the variables outside the function.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

 This function is called by reference. After the call by reference, when the function is executed, the variable will not be automatically created, but the address of the variable passed in will be passed to the function. At this time, the content of the original actual parameter address will be operated. , the modification of the formal parameter will affect the value of the actual parameter

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

Because it is a pass-by-reference call, but if the formal parameter * is not given inside the function after the call, when the function is executed, we find that the actual parameter does not change. This is because px and py are both int * types, pointing to num1 And the address of num2, if you operate on px py, it is to exchange their addresses, but it has no effect on the content of their addresses.

example

 Write a function to check if a number is a leap year

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

#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++)
	{
		//判断year是不是闰年
		if (is_leap_year(year))
		{
			printf("%d ", year);
		}
	}
	return 0;
}

 Write a function to determine whether a year is a prime number

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

 

#include <stdio.h>
#include<math.h>
int is_prime(int n)
{
	int j = 0;
	for (j = 2; j <= sqrt(n); j++)
	{
		if (n % j == 0)
		{
			return 0;
		}
	}

	return 1;
}

int main()
{
	int i = 0;
	int count = 0;
	for (i = 101; i <= 200; i+=2)
	{
		if (is_prime(i)==1)
		{
			printf("%d ", i);
			count++;
		}
	}
	printf("\ncount = %d\n", count);
	return 0;
}

Write a function that implements a binary search of an integer sorted array. 

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

#include <stdio.h>
int binary_search(int a[],int x,int y,int z)
{
	
	while (x <= y)
	{
		int  mid = x + (y - x) / 2;
		if (z ==a[ mid])
		{
			return mid;
		}
		else if (z>a[ mid])
		{
			x = mid + 1;
		}
		else if (z < a[mid])
		{
			y = mid - 1;
		}
	}
	if (x > y)
		return -1;
}

int main()
{
	int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int left = 0;
	int right = sizeof(a) / sizeof(a[0]);
	int k = 0;
	scanf("%d", &k);
	int h=binary_search(a, left, right,k);
	if (h == -1)
		printf("没找到");
	else
		printf("找到了,下表是:%d", h);
	return 0;
}

 When we calculate the size of the array inside the function, the size of the array will become 1. This is because the address of the first element is passed when the array is passed the parameter. The int a[] of the function parameter seems to be an array, but it is actually pointer, which accepts the address of the first element

So int y = sizeof(a) / sizeof(a[0])

sizeof(a) is actually the size of the pointer variable, sizeof a[0] is the size of the first element, the size of the pointer is 4 or 8, and a[0] is 4, so the final result is 1

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

 This is a wrong demonstration

The array parameter actually passes the address of the first element of the array

instead of the whole array

So it is unreliable to calculate the number of elements of an array in the parameter part of a function inside a function

int binary_search(int a[])//The formal parameter a looks like an array, which is essentially a pointer variable

boolean type

bool: variable used to represent true and false

such as bool flag

There are only two cases for flag to return true or return false

Header file #include<stdbool.h>

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_14,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

nested calls

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

#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();
    return 0;
}

 Functions can be called nested, but not defined nested

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

 If the definition is nested, the function will report an error

chained access

The return value of a function, as an argument to other functions

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_16,color_FFFFFF,t_70,g_se,x_16

function return value

The function does not write a return value, and the default return type is int. When a function with an int type has no return value, on some compilers, the program will return the return value of the last statement by default.

 watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_19,color_FFFFFF,t_70,g_se,x_16watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_17,color_FFFFFF,t_70,g_se,x_16

 

This way of writing is not recommended

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_17,color_FFFFFF,t_70,g_se,x_16

 watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_20,color_FFFFFF,t_70,g_se,x_16

 

 

100 has been passed, but it is not used. If you want to reject this 100, you can add void. At this time, the compiler will generate an alarm, but the program will still run successfully.

Parameters of main function

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aS05Y-R5rKh5pyJ5Luj56CB5aSa,size_10,color_FFFFFF,t_70,g_se,x_16

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_49449676/article/details/124229655