The use of Java syntax based functions

  • 2020-04-01 02:09:35
  • OfStack

function
Four aspects:
Definition of function
Characteristics of the function
Application of functions
Overloading of functions

I. definition and characteristics of functions
1) what is a function?
A function is a small independent program defined in a class with a specific function. A function is also called a method
2) format of functions in Java:
Modifier returns the function name of the value type (parameter type formal parameter 1, parameter type formal parameter 2,.)
{  Execute statement;
  Return returns the value;
}
Return value type: the data type of the result after the function is run
Parameter type: is the data type of the formal parameter
Formal argument: is a variable that stores the actual arguments passed to a function when it is called
Actual parameter: the specific value passed to the formal parameter
Return: used to close the function
Return value: this value is returned to the caller
3)   Characteristics of the function
A)   Defining functions encapsulates functional code
B)   Facilitate the reuse of this function
C)   A function is executed only if it is called
D)   The appearance of functions improves the reusability of code
E)   If 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 in the last line, the system will automatically add.
Note:
A)   You can only call functions in a function, you cannot define functions inside a function.
B)   When a function is defined, the result of the function should be returned to the caller for processing.
C)   When the function returns no specific value after operation, the return value type identifies the keyword with a special keyword: void. Void: represents the case where the function returns no specific value.
D)   When the return value type of a function is void, the return statement in the function can be omitted and not written.
4)   How do I define a function?
      Function is actually a function, the definition of the function is to achieve the function, through two explicit to complete:
            1) to specify the result of this function is to specify the return value type of this function.
            2) whether there is any unknown content involved in the operation during the implementation of this function is actually to specify the parameter list of this function (parameter type & parameter number).
5)   Function:
1) used to define functions.
2) used for encapsulating code to improve code reusability.
Note: functions can only be called, not defined.
6)   Main function:
      1) ensure the independent operation of this class.
      2) because it is the entry point of the program.
      3) because it is being called by the JVM.
7)   What is the function definition name?
Answer: 1), in order to mark the function, convenient to call.
        2) in order to clarify the function of the function through the name, in order to increase the readability of the code.

Second, the application of the function
1)     Two clear
A)   What is the end result of specifying the functionality to be defined?
B)     It is clear whether unknown contents are required to participate in the operation during the definition of this function
2)     Example:
Example 1:


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);
 }
}

The above operations are found, because the results of the operations to obtain different data, the code appears to repeat.
To improve code reusability. Extract the code. Define this part as a separate function. Convenient for future use.
The definition of functionality in Java is in the form of functions.
Clear function: need to define function, complete an integer *3+5 operation,

1. Specify the format of the function definition.
      / *
      Modifier return value type function name (parameter type formal parameter 1, parameter type formal parameter 2,)
      {
                    Execute statement;
                    Return returns the value;
      }


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 summarize the little knowledge about whether return is omitted:
When a function is evaluated without a specific return value, the return value type is identified with a special keyword.
The key is void. Void: represents a case where a 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 and not written.

Example 2:


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);
 }

 
 public static void get(int a,int b)
 {
  System.out.println(a+b);
  return ;
 }
}

How do you define a function?
      1. Since the function is an independent function, what is the result of the operation of this function
            Because this is specifying the return value type of the function.
      2. In the process of defining this function, it is clear whether unknown contents are needed to participate in the operation.
            Because you are specifying the function's argument list (the type of argument and the number of arguments).

class FunctionDemo2 
{
 public static void main(String[] args) 
 {

  //get(4,5);
  int x = getSum(4,4);
  int y = getSum(7,9);
  int num = getMax(x,y);
 }
 //Requirement: define a function. I'm going to do 3 plus 4. And returns the result to the caller.
 

 public static int getSum()
 {
  return 3+4;
 }

 
 public static int getSum(int x,int y)
 {
  return x+y;
 }

 
 public static boolean compare(int a,int b)
 {
  /*
  if(a==b)
   return true;
  //else
   return false;
  */
  //return (a==b)?true:false;
  return a==b;
 }
 
 public static int getMax(int a,int b)
 {
  /*
  if(a>b)
   return a;
  else
   return b;
   //Or use the following ternary operator
   */
  return (a>b)?a:b;
 }
}

3)     Practice:
1. Define a function for printing rectangles.
2. Define a function that prints 99 times tables.

class  FunctionTest
{
 public static void main(String[] args) 
 {
draw(5,6);
  printHr();
  draw(7,9);
  printHr();
  print99();
 }
 
 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("------------------------------");
 }
 
 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. Overload of function
Concept of overloading:
More than one function of the same name is allowed in the same class, as long as they have different number or types of arguments.
Features of heavy loading:
Regardless of the return value type, just look at the parameter list.
Benefits of reloading:
Easy to read, optimized the program design.
Overloaded 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 decimals
Double add(double x,double y){return x+y; }
When to use reload?
When the function defined is the same, but the unknown content involved in the operation is different.
At this point, a function name is defined to represent the function for easy reading, and multiple functions of the same name are distinguished by the parameter list.
Overloaded example:

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 times 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 get the sum of three integers.
 public static int add(int x,int y,int z)
 {
  return add(x,y)+z;
 }
}

Exercise: distinguish between overloads

void show(int a,char b,double c){}
 The following a,b,c,d,e,f The difference with the above sentence: 
a.
void show(int x,char y,double z){}//No, because it's the same thing.
b.
int show(int a,double c,char b){}//Overloading because of different parameter types. Note: overloading has nothing to do with return value types.
c.
void show(int a,double c,char b){}//Overloading because of different parameter types. Note: overloading has nothing to do with return value types.
d.
boolean show(int c,char b){}//Overloading, because the number of arguments is different.
e.
void show(double c){}//Overloading, because the number of arguments 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 distinguish overloads: when a function is the same name, just look at the argument list. It has nothing to do with the return value type.


Related articles: