In the current programming language teaching materials, functions do not take a large part, especially Java programming books. However, I think functions are an important cornerstone of Program Development and actually understand functions, it is true that the function can be programmed. If the function has not been fully understood, do not do anything I/O, Gui, and thread. Some students always say that C ++ is hard to learn, in fact, I saw a variety of strange function prototypes. I don't know how to call them. I really understood the functions. I will certainly not be difficult to understand them again in the online manual.
Let's start with C ++. (C ++ and C language have different syntaxes, but they are not big, but I do not like to distinguish them. The extension name is usually CPP, still use printf, less use of cout, CIN), it can also be said to start from the C language functions. If Java code is composed of classes, the C language is composed of functions.
When I explain C language functions to students, I asked the students to remember three sentences:
1. the function parameters are the internal variables of the function (I called the variables space in my class, and I am currently compiling a space-based C and Java teaching material. The content of this article is part of the teaching material ).
2. assign a value to the parameter (which can be changed to a variable or space) during the call. Remember to assign a "value" to the variable inside the function. There is no so-called "Address Transfer ", "Reference transfer" (references in C ++ are always hard to understand, so it is not recommended to use them. Pointers are enough. I read a lot of code and few references are used)
3. When an array is passed, the first address of the array is passed in. Each value of the array cannot be passed in. When passing multi-dimensional arrays, except the first dimension, the length of other dimensions must be written in the parameter.
The following functions are available:
Void examplefunc (int A, int B, int * C, char d [], char E [] [4])
{
Int m = 0;
Int n = 0;
M =;
B = N; // The K passed in the following call is not modified.
* C = m; // Changes the value of K in the following call,
D [0] = (char);
E [1] [0] = N;
}
The following code is available for calling:
Void testexamplefunc1 ()
{
Int K = 4;
Char E [] [6] = {"ABC", "EDF "};
Char H [] = "abcdef ";
Examplefunc1 (2, K, & K, H, e );
Int * P;
P = & K;
Examplefunc1 (2, K, P, H, e); // if you have a pointer P, you can directly transfer the value of P
P = h;
Examplefunc1 (2, K, P, P, e); // the pointer P can be passed in as an array
}
Explanations:
1. the, B, C, D, E in the parameter and M, N in the function are all internal variables of the function. In a scope, values can be assigned to each other, the array writing method is different from the writing method in the function. This is the third article. This article is simple, but it can eliminate your strangeness and fear of parameters.
2. during the call, values are passed in sequence according to the function type. constants 2 to parameter A can be directly input. Values in K can be copied to parameter B. C is a pointer variable, you need an address. & K indicates the address of K. If you have a pointer P, you can directly transfer the value of P. The remaining two are arrays. The third part is about arrays. It seems very simple, but pay attention to the word "copy". If you pass a variable into the parameter, the value is copied. No matter how you modify the value in the parameter, the incoming variable will not be changed, to modify a variable through a function, it is easy to pass the address of the variable into it. Can the address of the variable be stored, the content of the pointer and pointer be changed, what memory is used, second-order pointers, in any case, are duplicate values, but they are different values. Different types of variables with the same order can be converted and assigned to each other, conversion and assignment are strictly prohibited for variables of different order, so order matching must be ensured when passing parameters.
3. D and E are two array parameters. When passing, the first address is passed. h is used independently without subscripts, which indicates the address of the first element, the meaning is exactly the same as that of & H [0]. The array in the parameter is completely different from the array defined in the function. You must consider it. Of course, the pointer P can also be passed into the array, when you want to pass an array, you can use a pointer variable as a parameter. In the parameter, char d [] and char * D are actually the same. Note the writing of two-dimensional arrays.
With this content, especially the transfer of arrays, you can understand most function prototypes, but you will also encounter some weird writing parameters. Don't be afraid, it must be a function pointer, pass a function name. Of course, the prototype of this function must be matched. It looks complicated and easy to transfer. A function pointer is also a space used to store the address of a function.
Finally, I usually give a question to see if I have mastered it:
Five functions
Void func1 (char C );
Void func2 (char * C );
Void func3 (char C []);
Void func4 (char * C []);
Void func5 (char ** C );
Two existing arrays
Char M [5];
Char * n [5];
Write the Writing Method of calling five functions using M and N as input variables. There are several types of writing methods. Do not publish the answer first. Try to write it.
After speaking of C, you can talk about Java. For Java, the function should be simpler, just remember the first two sentences.
Class Example
{
Private int A = 0;
Public int B = 0;
Void func (int m, int N [], example L)
{
A = m;
N [0] =;
L. B = a + 1;
L = new INTEGER (45 );
}
}
Call
Class testexample
{
Void testfunc (int m, int N [])
{
Example example = new example ();
Example M = new example ();
Int A [] = new int [5];
Int K = 3;
Example. func (K, A, M );
}
}
In Java, an array is an object. You can directly pass the array object into it. The key is to understand the array. I will not talk about it here. Here I will mainly talk about the parameter example L, l is the object handle, which is actually a pointer. in Java, all composite type variables are pointers, and all basic type variables are non-pointers. The value of K won't change during the call, the 0th elements of array a will change. l stores the same value as m before the value is assigned again. M and l point to the same object, L. B = a + 1 modifies the value of M's member variable B, L = new INTEGER (45); after that, l stores the address of the new object, and m does not have any relationship, to put it bluntly, it is not easy to understand the pointer and try to understand java. It is easy to understand the pointer and make it easy to understand any language. The following describes the pointer in JavaScript, in addition, it should be noted that member variables can be used in functions. This problem is not a function problem, but mainly static member variables, member variables and call relationships between static and member functions, I will not discuss it here any more.
Finally, let's talk about JavaScript. This is an explanatory language and an object-oriented language, PHP and Perl. ruby, Python, and so on are all explanatory languages. Basically, explanatory languages are weak type languages, which differ greatly from C and Java. The main feature is that variables do not need to be defined, different types are randomly assigned values and written at will. The bad thing is that type Matching errors cannot be checked and will only occur during running. In fact, the implementation principle is not complicated. in C language, although pointers have types, they occupy the same space. All pointers can be assigned values to each other. In a weak type language, all data is stored, the address of the object space is saved as a pointer and must be accessed through a pointer. All variables are actually pointers.
View code
Function jsfunc (A, B, C)
{
A. Name = "I was changed! ";
B = "test is changed ";
C [3] = 5;
}
VaR myobj = new object ();
Myobj. Name = "Fred ";
Myobj. Age = 42;
N = "test ";
K = new array ();
K [5] = 23;
Jsfunc (myobj, n, k );
Alert (myobj. Name );
Alert (N );
Alert (k );
When three parameters A, B, and C are called, three addresses are input respectively. The values of myobj are passed to parameters a, a, and myobj pointing to the same object, A modifies the attribute name, myobj's attribute name is modified, n is passed to parameter B, B is assigned a value again, and B is directed to a new object, the value of N will not change. Here, the array is also an object. K stores the address of this object, and the value of K is passed to the parameter C, so that C can access the elements in the array, and modify it. In JavaScript, objects such as string, number, and date are similar to the string class in Java. You can only modify the value by creating a new object. No member function can modify the value.
In the three languages, function parameters are transmitted in the same way. values can be data, addresses, and internal variables of the function, modifying the value of a parameter does not change the variable outside the function, but the parameter value can be used to modify the space to which the parameter points, that is, the space outside the function.