What are function parameters and how to pass function parameters?

Source: Internet
Author: User
Functions include parameter functions and non-parameter functions. Of course, function parameters only include parameter functions. Function parameters are the constant values, variables, expressions, or functions in parentheses after the function name is written. When we define a function, this is a form parameter, which cannot be a constant value. When we reference or call this function, this parameter is called the actual parameter (real parameter ). The type description of formal parameters can be between the function body {} and () followed by the function name, or within (). Currently, the popular style should be in parentheses. This is a style issue. You can follow your personal habits.

In most cases, there is a data transfer relationship between the main function and the called function. It is complicated to transfer data, but remember that the parameter type must be matched, generally, the number is equal (except for some database functions that use time-saving parameters). The complex cases of concentration are described as follows:

1. Forced type conversion. These types must be available in C. For example, if the parameter description is int and the real parameter is float, the processing is based on float.
2. Data type conversion. Includes a string. When a function is called, the current address of the real variable array is passed, or the first pointer of the string.
3. When using a function or expression as a parameter, an expression or function in the form of a real parameter must have a definite value that matches the parameter type,
In addition, it should be noted that, in C, the data transmission of the parameter variables to the shape parameter variables is one-way in local conditions, and can only be transmitted to the shape parameter, you cannot pass the form parameter to the real parameter. However, if the real and shape parameters are both global variables, the change of the form parameter will affect the change of the real parameter, this is a very useful method in some scenarios. For example, we compile a function for multiplying two matrices. When a matrix multiplication function in the form of two real parameters calls a function, the result of multiplication (two matrices multiplied) will be returned to the called function, this is a compromise method using global variables, but the best method is to use the method directly returned by function parameters. below is the multiplication between arrays using the global variables method, this parameter is used to indicate the transmission of parameters.

/*************** A = B * C ***************** */# include <stdio. h>
# Include <conio. h>
# Define x 3
# Define y 3

Int A [x] [Y];
Int B [x] [Y];
Int C [x] [Y];

Void matrix (int B [] [X], int C [] [Y]);
Main ()
{
Int I, j, temp;
Clrscr ();
Printf ("lease input int matrix B [% d] [% d]" N ", x, y );
For (I = 0; I <Y; I ++)/* input the array to be computed */For (j = 0; j <Y; j ++ ){
Scanf ("% d", & temp );
B[J] = temp;
}
Printf ("lease input int matrix C [% d] [% d]" N ", x, y );
For (I = 0; I <X; I ++)/* input the array to be computed */For (j = 0; j <Y; j ++ ){
Scanf ("% d", & temp );
C[J] = temp;
}
Matrix (B, c );
Printf ("Now print resource matrix B [% d] [% d] =", x, y );
For (I = 0; I <X; I ++ ){
Printf ("N ";
For (j = 0; j <Y; j ++)
Printf ("% d", B[J]);
}
Printf ("N ";
Printf ("Now print resource matrix C [% d] [% d] =", x, y );
For (I = 0; I <X; I ++ ){
Printf ("N ";
For (j = 0; j <Y; j ++)
Printf ("% d", c[J]);
}/* Completes the corresponding input, that is, the input array is typed out */printf ("" N ": lol;
Printf ("Now printm multiply results matrix A [% d] [% d] = B * C:", x, y );
For (I = 0; I <X; I ++ ){
Printf ("N": lol;
For (j = 0; j <Y; j ++)
Printf ("% d",[J]);
}/* Output result */getch ();
Return 0;
}
/********************* Calculate the array multiplication subfunction *********** * ***********/void matrix (int B [] [X], int C [] [Y])
{
Int I, J, K, temp;
For (I = 0; I <X; I ++)
For (j = 0; j <Y; j ++ ){
For (k = 0; k <Y; k ++)
A[J] + = B[K] * C [k] [J];
}
}

As you can see, function parameters are like functions in mathematics. in mathematics, y = f (x) is a basic function expression, and X can be considered as a parameter, Y can be considered as the return value. z = f (x, y) is a binary function that has two parameters. In C, it corresponds to two parameter functions. In this case, you may understand the function and function parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.