Java Essentials Note (3)----function

Source: Internet
Author: User

    1. Preface
      Before the function is introduced, all the code is written in main function, too much code, code redundancy, and poor readability. After the

      introduces a function, a function is a block of code that implements a particular function. Multiple functions can be defined in a class, and each function and main main function are side-by-side relationships.

    2. function:
      1. syntax: public static Returns a value type function name (parameter list) {    //function declaration part.

        //function

        The three elements of the

        }

      2. function--> return value type, function name, parameter list
        1. return value type:
          1. void----means that the function finishes executing, without a return value.
          2. Eight basic types, reference types. --> indicates that the function must have a return value. The function must have a return XXX statement, and the XXX type must be the same as the return value type.
        2. function Name:--> Hump naming method: First letter all lowercase, followed by the first letter of the word capital.
        3. parameter list:--> when defining a function, the type, quantity, and order of the parameters
    3. functions are called:-> Calls to functions within the function.
      1. argument list: Used to specify the value of a parameter when a function is called.
      2. return Value: If the return value type is not void, you can return the value and use it later.
      3. procedure:
        1. jumps to the position defined by the function when the function is called;
        2. arguments assign values to parameters
        3. Execute function Body language The
        4. encountered a return or the end of the program, returning to the location of the function call with the return value. The

        code is as follows:

Package com.lvsling.test;

Public class test1 {

Public staticvoid main (string[] args) {

int s1=testfunction(2, 3);

System. out. println (S1);

int s2=TestFunction2(2, 3);

System. out. println (S2);

}

Public staticint testfunction (int A,int b) {

int i=a+b;

return i;

}

Public staticint TestFunction2 (int a , int b) {

int i=a*b;

return i;

}

}

    1. Self-summary

      These are the advantages of the function and the specific use of the function, pay attention to the three elements of the function can be, return value type, function name, parameter list.

      Expansion and Promotion:

      1. The parametric name of the function is equivalent to the local variable of the function and can only be used inside the function.
      2. The main function is a static function, the static function can only call the static function directly, may not directly mobilize the dynamic function
      3. Recursive function--the > function has called its own function inside, note to avoid infinite recursive call.

      The code follows the recursive invocation of the:--> function.

Package com.lvsling.test;

Import Java.util.Scanner;

/*

* calculates the factorial of a number

*/

Public class test2 {

Public staticvoid main (string[] args) {

System. out. println (" Enter an integer: ");

Scanner Scan = new Scanner (System. inch );

int x = Scan.nextint ();

int result = recursion(x);

System. out. println (result);

}

// Recursive Functions

Public staticint recursion (int x) {

if (x<=0) {

return 1;

}Else{

return x*recursion(x-1);

}

}

}

Java Essentials Note (3)----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.