Use of functions based on Java syntax

Source: Internet
Author: User

A function is an independent applet defined in a class with specific functions. A function is also called a method.

Function
Four aspects:
Function Definition
Features of functions
Function Application
Function Overloading

I. Function Definition and features
1) What is a function?
A function is an independent applet defined in a class with specific functions. A function is also called a method.
2) Function Format in Java:
Modifier Return Value Type Function Name (parameter type form parameter 1, parameter type form parameter 2 ,..)
{Execution statement;
Return value;
}
Return Value Type: the data type of the result after the function is run.
Parameter type: it is the data type of the form 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.
Returned value: this value is returned to the caller.
3) features of functions
A) define a function to encapsulate the function code.
B) facilitates reuse of this function
C) The function is executed only when it is called.
D) functions improve code reuse.
E) If the function does not return a specific value, the return value type is expressed by the keyword void. If the return statement in the function can be omitted in the last line, the system will automatically add it.
Note:
A) only functions can be called. functions cannot be defined within a function.
B) when defining a function, the function result should be returned to the caller and handled by the caller.
C) when no specific return value exists after function operation, this is the return value type. A special keyword is used to identify this keyword as void. void: indicates that the function does not return a specific value.
D) when the return value type of the function is void, the return Statement in the function can be omitted without writing.
4) how to define a function?
A function is actually a function. Defining a function is to implement a function. It is accomplished through two definitions:
1) Clarifying the result of the operation of this function is actually clarifying the return value type of this function.
2) Is there any unknown content involved in the operation during the implementation of this function? In fact, it is necessary to clarify the parameter list of this function (parameter type & number of parameters ).
5) functions:
1) define functions.
2) It is used to encapsulate code to improve code reusability.
Note: Only functions can be called and functions cannot be defined.
6) main function:
1) Ensure that the class runs independently.
2) because it is the entry of the program.
3) because it is called by jvm.
7) Why is the function definition name?
Answer: 1) in order to mark this function, it is easy to call.
2) in order to clarify the function functions by name, in order to increase the readability of the Code.

II. Application of functions
1) Two definitions
A) What is the final result of the function to be defined?
B) Whether unknown content is required for calculation during the definition of this function
2) Example:
Example 1:

Copy codeThe Code is as follows:
Class FunctionDemo
{
Public static void main (String [] args)
{
Int x = 4;
System. out. println (x * 3 + 5 );
X = 6;
System. out. println (x * 3 + 5 );
Int y = 4*3 + 5;
Int z = 6*3 + 5;
System. out. println (y );
System. out. println (z );
}
}


We found that the above operations were repeated because of the results of obtaining different data.
To improve code reusability. Extract code. Define this part as an independent function. Convenience and future use.
The definition of functions in java is embodied in the form of functions.
Explicit function: You need to define a function to complete * 3 + 5 operations of an integer,

1. Specify the format defined by the function.
/*
Modifier Return Value Type Function Name (parameter type form parameter 1, parameter type form parameter 2 ,)
{
Execute the statement;
Return value;
}

Copy codeThe Code is as follows:
Class FunctionDemo
{
Public static void getResult (int num)
{
System. out. println (num * 3 + 5 );
Return; // can be omitted
}
Public static void main (String [] args)
{
GetResult (5 );
}
}


To sum up whether return omitting this small knowledge point:
When no specific return value exists after a function operation, this is the return value type identified by a special keyword.
This keyword is void. Void: indicates that the function does not return a specific value.
When the return value type of a function is void, the return Statement in the function can be omitted without writing.

Example 2:

Copy codeThe Code is as follows:
Class FunctionDemo2
{
Public static void main (String [] args)
{
Int sum = getSum (4, 6 );
System. out. println ("sum =" + sum );
Sum = getSum (2, 7 );
System. out. println ("sum =" + sum );
}

/* Why is there a problem with the function definition concept below? Because only the addition operation is completed, whether or not to print and,
This is the caller's business and should not be completed in this function. */
Public static void get (int a, int B)
{
System. out. println (a + B );
Return;
}
}


How to define a function?
1. Since a function is an independent function, what is the result of this function first?
This is to clarify the type of the return value of the function.
2. Determine whether unknown content is required for calculation when defining this function.
Because the parameter list of the function is specified (the parameter type and number of parameters ).

Copy codeThe Code is as follows:
Class FunctionDemo2
{
Public static void main (String [] args)
{
/*
Int sum = getSum (4, 6 );
System. out. println ("sum =" + sum );
Sum = getSum (2, 7 );
System. out. println ("sum =" + sum );
*/
// Get (4, 5 );
Int x = getSum (4, 4 );
Int y = getSum (7,9 );
Int num = getMax (x, y );
}
// Requirement: Define a function. Complete 3 + 4 operations. Return the result to the caller.
/*
1. Define the function result: it is the sum of integers.
2. Is there any unknown content involved in the operation during the implementation of this function.
In fact, the two functions are clearly defined.
1. define the type of the return value of the function.
2. Define the parameter list of the function (the type and number of parameters ).
*/

Public static int getSum ()
{
Return 3 + 4;
}

/*
The results of the above functions are fixed, without scalability.
To facilitate user needs. You can specify the number of adders and the number of adders. In this way, the function makes sense.
Ideas:
1. The function result is a sum. The return value type is int.
2. Unknown content is involved in the operation. There are two. The two unknown content types are int.
*/
Public static int getSum (int x, int y)
{
Return x + y;
}

/*
Requirement: Determine whether the two numbers are the same.
Ideas:
1. Define the function result: the result is boolean.
2. Whether unknown content is involved in the operation. Yes. There are two integers.
*/
Public static boolean compare (int a, int B)
{
/*
If (a = B)
Return true;
// Else
Return false;
*/
// Return (a = B )? True: false;
Return a = B;
}
/*
Requirement: define a function to compare two numbers. Obtain a large number.
*/
Public static int getMax (int a, int B)
{
/*
If (a> B)
Return;
Else
Return B;
// Or use the following ternary Operator
*/
Return (a> B )? A: B;
}
}


3) exercises:
1. Define a function to print a rectangle.
2. Define a function for printing 99 multiplication tables.

Copy codeThe Code is as follows:
Class FunctionTest
{
Public static void main (String [] args)
{
Draw (5, 6 );
PrintHr ();
Draw (7, 9 );
PrintHr ();
Print99 ();
}
/*
Defines a function to print a rectangle.
Ideas:
1. Confirm the result: No, because it is printed directly. Therefore, the return value type is void.
2. Is there any unknown content? There are two, because the rows and columns of the rectangle are not sure.
*/
Public static void draw (int row, int col)
{
For (int x = 0; x <row; x ++)
{
For (int y = 0; y <col; y ++)
{
System. out. print ("*");
}
System. out. println ();
}
}
Public static void printHr ()
{
System. out. println ("------------------------------");
}
/*
Defines a function for printing 99 multiplication tables.
*/
Public static void print99 ()
{
For (int x = 1; x <= 9; x ++)
{
For (int y = 1; y <= x; y ++)
{
System. out. print (y + "*" + x + "=" + y * x + "t ");
}
System. out. println ();
}
}
}


Iii. Function overload)
Concept of heavy load:
In the same class, more than one function with the same name is allowed, as long as they have different numbers of parameters or parameter types.
Features of heavy load:
It has nothing to do with the type of the returned value. It only looks at the parameter list.
Benefits of heavy load:
It facilitates reading and optimizes program design.
Overload example:
Returns the sum of two integers.
Int add (int x, int y) {return x + y ;}
Returns the sum of three integers.
Int add (int x, int y, int z) {return x + y + z ;}
Returns the sum of two decimal places.
Double add (double x, double y) {return x + y ;}
When to use overload?
When the defined functions are the same, but the unknown content involved in the operation is different.
Then, a function name is defined to indicate the function to facilitate reading. Multiple Functions with the same name are distinguished by different parameter lists.
Heavy Load example:

Copy codeThe Code is as follows:
Class FunctionOverload
{
Public static void main (String [] args)
{
Add (4, 5 );
Add (4, 5, 6 );
Print99 ();
}
Public static void print99 (int num)
{
For (int x = 1; x <= num; x ++)
{
For (int y = 1; y <= x; y ++)
{
System. out. print (y + "*" + x + "=" + y * x + "t ");
}
System. out. println ();
}
}
// Print the 99 multiplication table
Public static void print99 ()
{
Print99 (9 );
}
// Define an addition operation to obtain the sum of two integers.
Public static int add (int x, int y)
{
Return x + y;
}
// Define an addition to obtain the sum of three integers.
Public static int add (int x, int y, int z)
{
Return add (x, y) + z;
}
}


Exercise: identify whether it is heavy load

Copy codeThe Code is as follows:
Void show (int a, char B, double c ){}
The following differences between a, B, c, d, e, f and the above sentence:
A.
Void show (int x, char y, double z) {}// no, because it is the same as the original function.
B.
Int show (int a, double c, char B) {}// overload, because the parameter type is different. Note: There is no relationship between overload and return value type.
C.
Void show (int a, double c, char B) {}// overload, because the parameter type is different. Note: There is no relationship between overload and return value type.
D.
Boolean show (int c, char B) {}// overload, because the number of parameters is different.
E.
Void show (double c) {}// overload, because the number of parameters is different.
F.
Double show (int x, char y, double z) {}// No. This function cannot exist in the same class as the given function.


How to differentiate overload: When a function has the same name, it only looks at the parameter list. It has nothing to do with the return value type.

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.