Java Basics--functions
the basic use of a function
Definition of function
function is a very important part of computer programming, and it is one of the most basic elements in programming. A function represents a generic process that provides services to the outside world. For example, in real life, ATM machines have different functions, we can understand that there are different functions on the ATM machine can be called; we take the money on the ATM machine, we can understand that we call the "fetch" function on the ATM. In this relationship, we are callers to the "money-taking" function, and the "take-money" function provides services for us.
The code shows the following:
Package P4;
public class hanshu{public
static void Main (String [] args) {
int d=add (3,2); Call function
System.out.println (d);
}
public static int Add (int a,int b) {//Declare public static return value type function name (formal parameter)
int c=a+b; Implement return
C; return value
}
}
Ⅰ, define Location
The order of functions is irrelevant in a class, outside of any function, in a class where there can be multiple functions.
Ⅱ, statement
public static return value type function name (parameter table)
Formal parameter: the equivalent of a local variable that is valid within a function
The most important part is the function of the three elements: return value, function name, parameter table. return value
Function Name: Represents the name of the functions;
Parameter table: represents what parameters are given when the function is invoked, that is, what "input" is required to the function when calling the function.
Return value: For example, the return value of a function is "money", as in the "Take money" function, we call the "take money" function as the caller to get the return value of the function "money"; the parameter table of this function indicates the arguments we should give to this function when we call "fetch", and we Need "bank card, password, amount of withdrawal" when we withdraw money. and a series of parameters. In Java, functions (function) are also called methods (method). The two concepts are not distinguished in Java, so the "function" and "method" in this book refer to the same meaning.
The return statement can also control the jump of a process, in addition to the value that can be returned. Specifically, when the return statement is executed, the called function terminates execution and returns to the call point of the function
The return value is a void type, and no return value is required to finish the code in the code block.
Ⅲ, to achieve
{ }
Must be consistent with declaration (mainly refers to the return value type)
Ⅳ, calling a function
function name (actual parameter (argument));
Real attendees assign values to the parameters in turn
Call the function, the program flow will enter the function, the function returns, the program flow will return to the function call point
Nested calls to the ① function
In front of the content, we have introduced some basic functions of the use of. The next step is to introduce some of the more flexible applications in the function. Look at the following example of this code:
Package P4;
public class testnestedcall{/
*
First, the "Main1" is printed in the main function, and then the MA method is called in the main function,
so the process jumps from main to the MA function.
in the first statement of the MA function, the output "MA1".
*
/public static void Main (String args[]) {
System.out.println ("main1");
Ma ();
System.out.println ("main2");
}
/*
Then, in the Ma method, the MB method is called again.
*/public
static void Ma () {
System.out.println ("ma1");
MB ();
System.out.println ("Ma2");
}
public static void Mb () {
System.out.println ("MB1");
System.out.println ("MB2");
}
/* After the MA calls the MB function, the MB function outputs "MB1" and "MB2".
then, all the code in the MB function is finished, and the MB function returns its call point, which is
returned to the MA method. The Ma method then executes, outputting "Ma2". The
code for the Ma method is then also complete, so the Ma method returns the call point,
which is returned to the main function. Finally, the main function output "main2", the main function of the
code is executed, the program terminated.
*/
}
Recursive invocation of ② function
In addition to being able to call other functions, a function can call the function itself. This is the recursive invocation of the function.
If you need to write recursion, you need to pay attention to a few questions:
1, to first study a derivation formula, this formula to be related to parameters, preferably this formula can be used (n-1) The result of the call, to calculate the result of the call N. For example, the n! above = (n-1)! * N is a good example.
2, to write out the convergence of recursion conditions. For example, the above is a judgment of whether N is 1, and this part explains when recursion terminates.
Functions of function
Package P4;
public class testseperator{public
static void Main (String args[]) {
System.out.println ("Hello World");
Print separator--------------------for
(int i = 0; i < i++) {
System.out.print ("-");
}
System.out.println ();
System.out.println ("Hello, World");
Print separator--------------------for
(int i = 0; i < i++) {
System.out.print ("-");
}
System.out.println ();
System.out.println ("Bonjour tout le monde");
Print separator--------------------for
(int i = 0; i < i++) {
System.out.print ("-");
}
System.out.println ();
System.out.println ("Hallo Welt");
}
1, avoid redundant code
Let's analyze the code above. Because you want to output three delimiters, you need three identical print statements. So, we can write a function: Printseperator (), which is specifically used to print delimiters. The modified code is as follows:
Package P4;
public class testseperator{public
static void Main (String args[]) {
System.out.println ("Hello World");
Printseperator ();
System.out.println ("Hello, World");
Printseperator ();
System.out.println ("Bonjour tout le monde");
Printseperator ();
System.out.println ("Hallo Welt");
}
public static void Printseperator () {for
(int i = 0; i < i++) {
System.out.print ("-");
}
System.out.println ();
}
2, improve the maintainability of the program
If there is a change in demand now, when the output delimiter is expected to be able to output a row "+" instead of a line "-", then you must modify the code. If you do not use a function, you must change the statement of all output delimiters in the main function. In this particular example, the code in the main function has to be modified in three places. The modified code fragment is as follows:
System.out.println ("Hello World");
Print separator +++++++++++++++++++ for
(int i = 0; i < i++) {
System.out.print ("+");
}
System.out.println ();
System.out.println ("Hello, World");
Print separator +++++++++++++++++++ for
(int i = 0; i < i++) {
System.out.print ("+");
}
System.out.println ();
System.out.println ("Bonjour tout le monde");
Print separator +++++++++++++++++++ for
(int i = 0; i < i++) {
System.out.print ("+");
}
System.out.println ();
System.out.println ("Hallo Welt");
3, improve the reusability of the program
If you use a function, the call to the function does not need to change when the function implementation changes, because it is just a multiple call to the same function in the main function. We only need to modify the implementation of the function once, we complete our work. The modified code is as follows:
public static void Main (String args[]) {
System.out.println ("Hello World");
Printseperator ();
System.out.println ("Hello, World");
Printseperator ();
System.out.println ("Bonjour tout le monde");
Printseperator ();
System.out.println ("Hallo Welt");
}
public static void Printseperator () {for
(int i = 0; i < i++) {
System.out.print ("+");
}
System.out.println ();
}
4, improve the flexibility of the program
Using functions, you can make your code more flexible by changing the parameters of the function. For example, suppose you now have different requirements for the length of the separator, requiring the first delimiter length of 20, the second separator length of 25, and the third delimiter length of 30. Such a requirement, if the function is not applicable, you have to modify the three output, and each place of the output statements are more complex. If you use a function, you can add a parameter to the function that represents the length of the separator. The modified Printseperator function is as follows:
public static void Printseperator (int n) {for
(int i = 0; i < n; i++) {
System.out.print ("+");
}
System.out.println ();
}
So, in the main function, you can call it three times by passing in a different parameter to the function. The complete main function is as follows
public static void Main (String args[]) {
System.out.println ("Hello World");
Printseperator (a);
System.out.println ("Hello, World");
Printseperator (a);
System.out.println ("Bonjour tout le monde");
Printseperator (a);
System.out.println ("Hallo Welt");
}