Java Syntax Basics-functions

Source: Internet
Author: User

What is a function?

A function is a separate applet defined in a class that has a specific function.

functions are also called methods.


The format of the function:

modifier Returns a value type function name ( parameter type form parameter 1, parameter type parameter 2,...)

{

execute the statement ;

return value ;

}


1. return value type: The data type of the result after the function is run.

2. parameter type: is the data type of the formal parameter.

3. Formal parameters: is a variable that is used to store the actual arguments passed to the function when the function is called.

4. actual parameters: The specific values passed to the formal parameters.

5. return: used to end the function. returns the specific function result to the caller. Ends the function. is an end tag.

6. return value: The result of the function operation, which is returned to the caller.

 


Features of the function

1. define functions to encapsulate the function code

2. facilitates the reuse of this function

3. functions are executed only if they are called

4. the appearance of functions improves the reusability of code

5. for cases where the function does not have a specific return value, the return value type is represented by the keyword void , then the return statement in the function can be omitted from the last line if not written.


Note:

functions can only be called in functions, and functions cannot be defined inside a function.

When defining a function, the result of the function should be returned to the caller, which is handled by the caller.


special case of function, function return value type void

When a feature returns without a specific result, the return value type is represented by the keyword void.

When a function of type void is defined, a return directly after the function ends with a semicolon.

At the same time, the REturn statement can be omitted without writing. But the compiler will help you add .



Benefits of the function :

1, the details are encapsulated.

2, improve the reusability of the code.


function's memory run procedure

The program executes the main function and calls the functions of other functions from inside the main function. Functions cannot be defined inside functions, and functions are tied together.

In each program, space is partitioned in memory, and space in Java is broken down. When the function is called, the call ends and is freed.


function overloading

The concept of overloading :

class Functi on in the same class,

allows more than one function with the same name,

as long as they have number of arguments or parameter types different can

features of overloading:

Regardless of the return value type, see only the parameter list .

Benefits of overloading:

easy to read and optimize the program design.


Java Syntax Basics-functions

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.