Dark Horse Programmer--java Foundation-function

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

One: function

1. Function definition

2. function format

3. Function action

A, what is called a function?

is a section of code that has a special effect.

b, the format of the function

Modifier return type function name (variable list)//variable list includes variable type and number of variables

{

Code

return;

}

C, why use a function?

To improve the reusability of the procedure and improve the efficiency, the so-called demand will appear.

D, function suitable for which occasions to use?

When a function is to be reused, the function becomes extraordinarily efficient.

E, how to set a function?

Two clear:

1. The result of the operation of this function is to clarify the return type of the function.

2. In the process of implementing this function, whether there is an unknown content involved in the operation, in fact, is to clarify the parameter list of the function.

F, what function does it have?

1. function defines the function

2. Function encapsulation Code improves function reusability

G, what is the function definition name?

1, in order to mark this function, convenient to call

2, in order to use the name can be clear function functions, in order to increase the readability of the Code

H, what is the nature of the function?

Overload

What is the function of the main function?

1. Ensure the independent operation of this class

2, because he is the entrance to the program

3, because he was called by the JVM (virtual machine)

What is overloading?

In a class, if two or more of the same-name functions appear, they can be called function overloads as long as the number of their arguments, or their type, is different.

How do I differentiate between overloads?

When the function name is the same as the argument list, he has no relation to the return value type.

For example, to print a rectangle:

public class Functiondemo{public static void Main (String args[]) {for (int i=0, i<5; i++) for (int j = 0; j<6; j + +) {Syst Em.out.print ("*");} System.out.println ();}}
butand to print a 6*6 rectangle, what do you do? You can add one more time in the main function:

public class Functiondemo{public static void Main (String args[]) {for (int i=0, i<5; i++) for (int j = 0; j<6; j + +) {Syst Em.out.print ("*");} System.out.println (); for (int i=0, i<6; i++) for (int j = 0; j<6; j + +) {System.out.print ("*");} System.out.println ();}}}

But that's a lot of trouble, and then you can call the function:

public class Functiondemo{public static void Main (String args[]) {Hanshu (5, 6); Hanshu (6,6);} public static void Hanshu (int n, int. m) for (int. i=0; i<n; i++) for (int j = 0; j<m; j + +) {System.out.print ("*");} System.out.println ();}}
This can also be done with the same functionality as the previous section of code.

Overloading is a major feature of functions, and overloading can improve the efficiency of your code:

For example, print the 99 multiplication table:

public class Functiondemo{public static void Main (String args[]) {function99 ();} public static void Functiondemo99{for (int i=0, i<9; i++) for (int j=0; j<i; J + +) {System.out.print (x+ "*" +y+ "=" +x*y+ " \ t ");} System.out.println ();}}

Overloading is a good example when there are new requirements:

public class Functiondemo{public static void Main (String args[]) {function99 (); function99 (4);} public static void Functiondemo99{for (int i=0, i<9; i++) for (int j=0; j<i; J + +) {System.out.print (x+ "*" +y+ "=" +x*y+ " \ t ");} System.out.println ();} public static void functionDemo99 (int n) {for (Int. i=0; i<n; i++) for (int j=0; j<i; J + +) {System.out.print (x+ "*" +y+ "=" +x*y+ "\ t");} System.out.println ();}

This allows you to define your own values, to be more flexible to operate, and, of course, to go further:

public class Functiondemo{public static void Main (String args[]) {function99 (); function99 (4);} public static void Functiondemo99{function99 (9);} public static void functionDemo99 (int n) {for (Int. i=0; i<n; i++) for (int j=0; j<i; J + +) {System.out.print (x+ "*" +y+ "=" +x*y+ "\ t");} System.out.println ();}}

In this way, the code is much simpler.

In short, the function is to improve the reusability of the whole program, so that the whole program looks more beautiful, when defining a function only need to pay attention to two o'clock OK, one is the function of the return value, the other is the number of variables.

Dark Horse Programmer--java Foundation-function

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.