A detailed description of the return value types of Java functions and examples of generating random numbers

Source: Internet
Author: User
Tags types of functions

Four elements of a function: function name, input, output (return), processing.

There are two types of functions: one is a return-worthy function, and one is a function that has no return Value.

1.

Definition: a function that has no return value: ( when I do not need the result of the function to be computed, I do not need to have a return value, directly in the function of the operation to complete the processing can Be)

static void function name (parameter) {

function body

}

Call: call a function with no return value: function name (actual parameter);

The argument in the function is called the virtual parameter, and we need to give him an actual parameter to complete the whole operation FLOW.

For example: I want to print a one or two number of the and?

 public class  HS {    publicstaticvoid  main (string[] Args) {                    Jisuan (3,5); 3, 5 is the actual parameter assigned to a, B value (position must correspond!) )    }    // function with no return type    staticvoid Jisuan (int a,int  b) {        int c=a+b;        System.out.println (c);    }   }                

Of course the parameters are determined according to the situation, if there are no parameters you do not need to write parameters:
For example: randomly generate a four-digit number?
 public classHS { public Static voidmain (string[] Args) {suiji (); }    //a function with no return type generates a random four-digit number;    Static voidSuiji () {
//the outer layer defines an empty string to stitch the integer type a;String s= "";
//because it is the output of a four-bit number, so I want to loop four times each time output a number for(inti=0;i<4;i++){ intA= (int) (math.random () *100000000);//math.random () Molding method: calls all numbers between 0-1. This number must be a decimal, and//for 10, then A is the single digit.//This number is likely to be 0.00000001654 or more of 0, so try to take a larger number in the backa=a%10;//because it is always a double with a decimal number, it is forced to be converted to type int, because our purpose//because A is an int type, and I want to output a string,//is to output a 4-bit positive number. //so I'm going to define an empty string on the outer layer to stitch up a,//integer and string concatenation will automatically change to string s=s+a;s+=a; } System.out.println (s); } }

2.

Definition: a function with a return value: ( if you need to return a value to participate in another operation, the function must have a return type when it is defined!) The return type represents the type of merit to Return.

Static return type function name (parameter) {

function body

Return variable;

}

Call: call a function that has a return value: the return type variable = function name (parameter);

Example 1:: I want to print two numbers of the and?

 public classHS { public Static voidmain (string[] Args) {intA=he (5,6);    System.out.println (a); }    //a function that has a return value;    Static intHeintAintB) {        intc=a+b; returnc; }    }
One:
The same is true for printing two numbers and a few points when I need to have a return value:
1. Return type must be consistent with the type of the return value
2. Call the first write data type (return value Type) variable name = function name (parameter);
This function is then called up.

Two:
For this return type, use it flexibly. For example, or the above question, what if I want to output a string of type strings?
1. first, when defining a function, the return type will be string;
The value returned by 2.return must also be of type string
3. The data type when the function is called again is the string type
The code is as Follows:
It does not lie in: integer type after the operation or integral type, if you want to output an integer type will not need to convert.
If you want to output a string, you need to define an empty string in the function weight to splice with the result, and the integer value is automatically converted to a string during stitching.
 public  class   HS { public   Static  void   main (string[] Args) {String a<        /span>=he (5,6 //  a function with a return value;  static  String He (int  a,int          B) {String s  = ";         int  c=a+b;        s  +=c;     return   s; }} 

Example 2: randomly generate a four-digit number? (there is a return value, because it is a random 4-digit array of 4-bit strings, so the return type here is string, not int.) But can make int[] Array)

 public classHS { public Static voidmain (string[] Args) {String n=SJ ();    System.out.println (n); }    //a function with a return type, randomly generating four-digit numbers;    StaticString SJ () {string s="";  for(inti=0;i<4;i++){            intA= (int) (math.random () *10000000); A=a%10; S=s+a; }        returns; }    }

A detailed description of the return value types of Java functions and examples of generating random numbers

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.