Current location - Loan Platform Complete Network - Foreign exchange account opening - MT4 Programming: How to Return Functions to Arrays?
MT4 Programming: How to Return Functions to Arrays?
Methods 1: Using global variable analysis: As a knowledge point of C language, although we all know its characteristics, global variables have not been widely used in the actual teaching process. Because the scope of global variables is from the beginning of defining variables to the end of the program, it is considered to define multiple values to be returned as global variables when writing C language functions with multiple return values. When the function is called, the global variable is changed, and then we apply the changed global variable value to the tonic function. The global variable values changed after the function is called are several return values of the function. The application of this method is illustrated by an example. Example 1: Write a function to find the maximum and minimum values of three numbers. Methods: The maximum and minimum values were defined as two global variables, max and min, respectively, and the maximum and minimum values were assigned to the global variables, max and min, respectively, in the user-defined function. After the function is called, the maximum and minimum values of global variables store the values that the function needs to return. The program reference code is as follows: # include "stdio.h" # include "conio.h" intmax, min/* Define the return value of two global variable storage functions */Voidmax _ min (INTA, INTB, INTC)/* Define the function for finding the maximum and minimum values */{max = min = a; /* Initialize the maximum and minimum values */if (maxif (maxif (min >; b)min = b; if(min & gt; c)min = c; }main(){intx,y,z; Printf ("Please enter 3 integers: \ n"); scanf("%d,%d,%d ",& ampx & amp; y & amp; z); max_min(x,y,z); /* Call the function of finding the maximum and minimum values */printf ("The maximum value among the three numbers is:% d;; Minimum value is %d ",maximum value, minimum value); /* Output the maximum and minimum values */getch (); Debugging results are as follows: Please enter three integers: 5, -6, 2, and the maximum value is: 5; The minimum value is: -6 Note: Although this method can realize functions with multiple return values, it should be used with caution, because global variables cannot guarantee the correctness of the values (because their scope is global, their values can be modified within the scope of the program, and it will be difficult to find errors), and global variables increase the coupling between modules in the program. Method 2: Transfer array pointer analysis: In the teaching process, we know that the transfer methods of function parameters in C language include value transfer and address transfer. When passing the value, the tonic function copies the value of the real parameter to the shape parameter, and the shape parameter obtains the value passed by the tonic function to run the function. The change of the parameter value of the called function in the process of value transfer cannot lead to the change of the actual parameter value. However, in the case of address passing, because the address is passed from the argument in the passing process, the change of formal parameter value in the called function will directly lead to the change of argument value. Therefore, we can consider defining multiple return values as array elements in the form of an array, and taking the address of the array as the formal parameter of the function to pass the array parameters by address. After the function is called, the change of the formal parameter group elements leads to the change of the real parameters, and then we get multiple return values of the function from the changed real parameter array elements. The following example demonstrates the application of this method. Example 2: Write a function to find the maximum and minimum values of a one-dimensional integer array, and return the maximum and minimum values to the main tone function. Method: Pass the address of a one-dimensional array with a pointer, then exchange the maximum value of the array with the first element of the array and the minimum value with the last element of the array. After the function is called, the first element in the independent variable array is the maximum value of the array, and the last element in the independent variable array is the minimum value of the array, thus realizing the function of returning the maximum and minimum values of the array. The program reference code is as follows: # include "stdio.h" # include "conio.h" void max _ min (int * ptr, intn)/* Define the function to find the maximum and minimum values of the array, and pass the array pointer */{inti, j, k; /*j saves the position of the maximum value, and k saves the position of the minimum value */int * temp; /* Used to exchange positions */* temp = * ptr; for(I = 0; I{if(*ptr*(ptr+i))/* The minimum value is exchanged with the last element */{j = i; * temp = *(ptr+n- 1); *(ptr+n- 1)= *(ptr+j); *(ptr+j)= * temp; }}}/* Call the max-min function */main(){intA[6], i; for(I = 0; I<6; i++)scanf("%d ",& ampa[I]); max_min(A,6); printf("max=%d,min=%d\n\n ",A[0],A[5]); getch(); } The debugging results are as follows: Please enter 6 integers separated by spaces: 58932-64max=32, min=-6 Note: This method is suitable for the case that the data types of multiple return values are consistent. This method is not applicable when the data types of the return values are inconsistent.