web123456

014-JAVA function definition and use of return statement

  • package T9;
  • //calculator
  • public class Calc {
  • /*
  • The parentheses of functions (methods) are parameters (local variables)
  • * /
  • //1.No return value and no parameters
  • public void display(){
  • ("Function with no parameters and no return value");
  • }
  • //2.No return value has parameters
  • public void eat(String name) {
  • (name + "Eating");
  • }
  • //Multiple parameters are separated by commas
  • public void eat(String name,String foodName){
  • (name + "Eating" + foodName);
  • }
  • //3.There are parameters but no return value, find a+b and
  • public void add(int a,int b) {
  • ("a+b=" + (a+b));
  • //End function run
  • return;
  • }
  • //4.There are parameters and return values, return a+b+Results of c
  • public int add(int a,int b,int c){
  • return a+b+c;
  • }
  • //Flag function
  • public int flag(int x){
  • if(x>0)
  • return 1;
  • else if(x<0)
  • return -1;
  • //x=0When running this statement
  • return 0;
  • }
  • public static void main(String[] args) {
  • //Define the variables of the class (object|instance), create the object c, c represents all member variables in Calc, member methods
  • Calc c = new Calc();
  • c.display();
  • ("Zhang San");
  • ("Zhang San","steak");
  • c.add(3,5);
  • int sum = c.add(3,5,8);
  • ("sum=" + sum);
  • ("sum="+ c.add(9,8,7));
  • int y = (100);//x>0return1
  • ("y=" + y);
  • y = (-1000); //x<0,return-1
  • ("y=" + y);
  • y = (0); //x=0,return0
  • ("y=" + y);
  • }
  • }