Function Definition 1

Source: Internet
Author: User

What is a function?

A function is an independent applet defined in a class with specific functions. (The smallest package in Java)
Functions in Java are also called methods.

 

Function Format:

Modifier .. return value type function name (parameter type form parameter 1, parameter type form parameter 2 ,){
Execute the statement;
Return return value; // do not simply print the returned value.
}

Return Value Type: the data type of the result after the function is run.
Parameter type: it is the data type of a formal parameter.
Form parameter: a variable used to store the actual parameters passed to the function when a function is called.
Actual parameter: the specific value passed to the form parameter.
Return: Used to end a function.
Return Value: this value is returned to the caller.

 

Features:

The defined function can encapsulate the function code. The smallest Java encapsulation body is the function.

This feature is easy to reuse.

The function is executed only when it is called.

The emergence of functions improves code reuse.

If the function does not return a specific value, the return value type is represented by the key word void. If the return statement in the function is omitted in the last line, it is automatically added if it is not written. If the function does not return a specific value, the return value type is represented by the key word void. If the return statement in the function is omitted in the last line, it is automatically added if it is not written.

 

Note:

Only functions can be called. functions cannot be defined within a function.
When defining a function, the function result should be returned to the caller for processing. This is a good design method.

 

Public ClassFunction_define1 {Public Static VoidMain (string [] ARGs ){IntX = getresult (3); System. Out. println ("X =" +X );}//It must be modified using static because it is called by the main function. Variable name and function name: when multiple words are made up, the first letter of the first word is lowercase, and the second word starts with the first letter of each word.    Public Static IntGetresult (IntNum ){ReturnNum * 3 + 5;}}
/*When there is no specific return value after function operation, this is the return value type. A special keyword is used to identify this keyword as void, void indicates that no specific return value exists. When the return value type of the function is void, the Return Statement in the function can be omitted without class functiondemo {public static void getresult (INT num) {system. out. println (Num * 3 + 5);} public static void main (string [] ARGs) {getresult (3 );}}*/

Function Definition 1

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.