当前位置:网站首页>C language ---18 function (user-defined function)

C language ---18 function (user-defined function)

2022-06-24 14:17:00 Try!

Before this record , First of all, let's make a point , That's it The main function is also a function .

1、 Composition of custom functions

I learned about library functions before , I learned that Library functions are the return types of containing functions 、 Function name and function parameter . for instance ( Let's use the previous example ), Copy of the contents of the array , The code is as follows :

#include <stdio.h>
#include <string.h>
int main()
{
    
	char arr1[20] = {
     0 };
	char arr2[] = "abc";
	strcpy(arr1,arr2);
	printf("%s\n",arr1);
	return 0;
}

This code is to pass the library function strcpy Implement the array 2 Copy the contents of to the array 1, Before using library functions , You need to query the header files required for the use of library functions .

strcpy Format of library functions :char *strcpy(char *strDestination,const char *strSource)

among ,strcpy Is the function name ;(char *strDestination,const char *strSource) It's a function parameter ;char * Is the return type of the function .
Custom functions are the same as library functions , There's a function name 、 Return value type and function parameters . But the difference is that these are designed by ourselves , It will give us a lot of room to play , As the saying goes “ There are a thousand Hamlets in a thousand people's eyes ”, Everyone has their own style of writing code .
Composition of custom functions :

ret_type fun_name(para1, *)
{
    
	statement;// Statement item 
}
ret_type  Return type 
fun_name  Function name 
para1  Function parameter 
{
    } Inside is the structure 

2、 Examples of custom functions

Example 1 : Write a function to find the maximum value of two integers

 Definition of function 
int get_max(int x, int y)
{
    
	int z = 0;
	if (x > y)
		z = x;
	else
		z = y;
	return z;// return z-- Returns the larger of the two ,z The type of is integer , therefore get_max The type of is also an integer 
}

int main()
{
    
	int a = 10;
	int b = 20;
	// Function call 
	int max = get_max(a,b);
 	printf("max = %d\n",max);
	return 0;
}

The operation results are as follows :

max = 20

Example 2 : Write a function that can exchange the contents of two integer variables
The code is as follows :

void Swap(int x, int y)
// Only need to a as well as b Just swap the values of , There is no need to return a value 
// Some functions when things are done , Nothing needs to be returned , Write the return type of the function as void
// The return type of the function is written as void, Indicates that this function does not return any value , There's no need to go back 
{
    
	int tmp = 0;
	tmp = x;
	x = y;
	y = tmp;
}
int main()
{
    
	int a = 10;
	int b = 20;
	// Write a function , Exchange the values of two integer variables 
	printf(" Exchange before :a=%d b=%d\n", a, b);
	Swap(a,b);
	printf(" After exchanging :a=%d b=%d\n", a, b);
	return 0;
}

Run it to see the results :

 Exchange before :a=10 b=20
 After exchanging :a=10 b=20

Obviously , There's a problem with the program , Did not a and b The value of , Let's analyze the code .
 Insert picture description here
 Insert picture description here  Insert picture description here  Insert picture description here
 Insert picture description here
Through the monitoring window, you can know a and b These two spaces and x and y These two spaces are completely independent , So for x and y The change will not affect a and b Of . The place where the function is called has nothing to do with the two variables inside the function , So it won't affect each other .
That's when it's used “ The pointer ” Knowledge points of , Any variable needs space in memory . Let's review our knowledge of pointers .

int main()
{
    
	int a = 10;// At this point, it is equivalent to opening up four bytes of space in the memory 
	// The four byte space has an address ,&a You can get its address ; take a Save your address , Put it in a variable pa Inside 
		//pa The type of int*, Inside pa It's a pointer variable .
	int* pa = &a;//pa It's a pointer variable 
	// hold a The address of is entrusted to pa,pa And a There is a connection between , You can go through *pa To find the a To operate 
	//*pa;// adopt pa Address inside , To find the pa Object referred to (*pa Is refers to a)
	
	*pa = 20;// This is the time a The value of becomes 20  Add a sentence “printf("%d\n",a)” To verify the results 
	printf("%d\n", a);
	return 0;
}

The operation results are as follows : You can see a The value of is changed to 20 了 .

20

//*pa;// adopt pa Address inside , To find the pa Object referred to (*pa Is refers to a)
According to the nature of the pointer , Correct the error code just now , The code is as follows :

void Swap(int* pa, int* pb)
//a After you give me your address , The address is an integer address , So it's better to take an integer pointer to receive the parameter 
{
    
	//*pa It is the following that is accessed a, *pb It is the following that is accessed b
	int z = 0;
	z = *pa;// here z What's in it is a Value 
	*pa = *pb;//a It's in there b Value 
	*pb = z;//b What's in it is a
}
int main()
{
    
	int a = 10;
	int b = 20;
	// Write a function , Exchange the values of two integer variables 
	printf(" Exchange before :a=%d b=%d\n", a, b);
	Swap(&a,&b);// take a and b Give this function the address of 
	printf(" After exchanging :a=%d b=%d\n", a, b);
	return 0;
}

The operation results are as follows :

 Exchange before :a=10 b=20
 After exchanging :a=20 b=10

Successfully a and b The values of are exchanged .

3、 Function parameter

The parameters of a function are divided into formal parameters and actual parameters .

  • The actual parameter ( Actual parameters ): The parameters actually passed to the function are called arguments . The argument can be : Constant 、 Variable 、 expression 、 Functions, etc . Whatever the type of argument is , When you make a function call , They all have to have certain values , In order to pass these values to the parameter .
     Insert picture description here

  • Formal parameters ( Shape parameter ): Formal parameters refer to the variables in brackets after the function name , Because formal parameters are instantiated only when the function is called ( Allocate memory units ), So it's called formal parameter . Formal parameters are automatically destroyed when the function call is completed ( namely : Its life cycle is in function calls ), So formal parameters are only valid in functions .
    In example 2 ,x、y、pa、pb Are formal parameters ; stay main Function passed to Swap Medium a、b as well as &a、&b Is the actual parameter . stay Swap When a function is called , The actual parameter is passed to the formal parameter . In fact, a formal parameter is a temporary copy of an argument . That is to say, in the wrong part of the code ,x and y It's just a copy a and b Value , Create an independent space for it , Their addresses are different . So changing the formal parameter will not affect the argument , Naturally in that code a and b The value of is not exchanged .

原网站

版权声明
本文为[Try!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241206095372.html