An overview of the Java_ function

Source: Internet
Author: User
Tags define function

definition of a function : A separate applet defined in a class that has a specific function. Also known as the bit method
Functions can only be called in function, cannot define function
function is just one of the ways to improve code reusability .
The format of the function:
Modifier returns a value of type Han Shuming (parameter type form parameter 1, parameter type parameter 2, ...)
{
Execute the statement;
return value;
}


If you need a feature, you also need to define a separate piece of code to represent it.
Find print a rectangle, print once to write this code once, very troublesome. Poor reusability, Odd difference
To improve reusability, enclose the code of the printed rectangle, name it, and the next time you print a rectangle,
Just look for the code that corresponds to this name, no need to re-write it.


Encapsulation of code

Draw
{//This is a standalone function that completes the action of drawing a rectangle, and the next one is renamed with a direct call.
In Java, independent functions are represented by functions, which means that in Java there is a specific format to embody.
for (int x=0;x<3; x + +)
{
for (int y=0;y<4; y++)
{
System.out.print ("*");
}
System.out.println ();
}
}


the format of functions in Java:

Modifier returns a value type function name (parameter type form parameter 1, parameter type parameter 2, ...)
{
Execute the statement;
return value;
}

  modifier : Adds the specified meaning to the modified content by keyword.
  return value : function is a function, usually after the end of the function, there is a result, and this result is the return value.
  return value type : The data type of the return value. Any data in Java has a type, so the return value also has a type.
  function name : Functions are a code interval that is to be used and must have a name, which is the function name.
  parameter : is the data that participates in the operation. This function needs to participate in the operation of the data is uncertain, through the parameters to reflect, in fact, is a variable.
 : Refers to the data type of the participating operation, which is actually the data type of the variable. Indicates the specific data type to receive.
 return return value : When the function ends, the specific result is returned to the caller.
  return : keyword, you can return the specific results, and end this function (function)
 
  function definitions generally have results, of course, there are special cases, function has no specific results .
  At this point, how is the return value type defined? In Java, a special keyword is used to .


public static void Draw ()//NULL parameter
{
for (int x=0;x<3; x + +)
{
for (int y=0;y<4; y++)
{
System.out.print ("*");
}
System.out.println ();
}
return,//If the return value type of the function is void, then the return statement in the function can be omitted from the write.
//If the function has a specific return value type, you must write the return statement, along with the specific return value .
}
*/

1 classFunctiondemo2 {3 Public Static voidMain (string[] args)4  {5//the function is defined and will only be executed when called. 6Draw (6,8);//Call Function: Define the function name and define the function's parameters. 6,8 The actual arguments, when the function is called, the specific data passed in7   8  }9 /*Ten Requirement: Defines a function for drawing rectangles, but the rows and columns to be drawn are undefined and are made by the caller.  One   A  */ - Public Static voidDrawintAintb//A function with two parameters -  { the for(intX=0;x<a; x + + ) -   { - for(intY=0;y<b; y++ ) -    { +System.out.print ("*"); -    } + System.out.println (); A   } atreturn; -  } - } - /  * execution Order  - 1 Executes the main function first, discovers the calling function, and formulates two actual parameters - 2 virtual machines find the function in this class and read the position of the function, judge the data type of the actual parameter, and assign the value to the formal parameter in 3 code to execute the called function

An overview of the Java_ 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.