Parameters and Arguments: passing information for methods

In our Java course, so far, our methods were very specific things, such as displaying a string, a menu or performed mathematical calculations.
Therefore, in practice, is not very useful.



Parameters and arguments are the means in which we pass data to the method, and these methods will work specifically upon this information we give.
And, generally, we will return differents returns for differents inputs that they received.

Declaring methods with parameters in Java


Whenever a method to receive data, it will be received by parameters.
In Java, as in C or C + +, you need to make it clear which and what type of data you will pass.
In some languages, such as Perl, this is not necessary.

This has to do with typing, memory allocation and other things in lowest level of information.
(When we talk about low-level Java or computing, we are referring to the level of abstraction. The lower, nearest the hardware or the bits we're talking about).

The syntax of the statement looks like this:
[data_method] return_type method_name (type parameter_name1, type parameter_name2){
    // code
    // of your
    // method
}

A function that takes an integer and returns an integer would have the next 'face':
public int methodName(int number){
    //code
    return intNumber;
}



This type in case 'int', which comes just before the method name refers to the type of return.
If your method returns float, should be: public float method Name(....)
If String should be: public static String methodName(...)

The static will be explained later in our Progressive Java course section about Object Orientation.
Until then we are using because we're calling methods from the main, which is a static method.

Example: Passing arguments to a method: Function that calculates the square of a number


For the statement, we see that the following method receives an integer, the 'num' and returns an integer too.
Within methods we can declare types.
Should we declare type 'sqr', which receives the value of 'num' squared, ie: num*num
and returns 'sqr'.

    public static int square(int num){
        int sqr;
        sqr = num * num;
        return sqr;
    }



For we use a method that takes an integer parameter we need to pass as an argument to the method. By argument, understand a value, something real.
In the program, the method call is as follows: square (number);

In past examples we put inside the brackets because the methods we created in do not needed any parameter. Now, we have to pass a number, would look something like: square(2), square(3) etc..

See how code this method on a Java application that prompts the user for an integer and returns the square of this value:

import java.util.Scanner;

public class square {
    
    public static int square(int num){
        int sqr;
        sqr = num * num;
        return sqr;
    }
    
    public static void main(String[] args) {
        int number, numberSquare;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter with a integer: ");
        number = input.nextInt();
        
        numberSquare=square(number);
        
        System.out.printf("Square of %d is %d", number, numberSquare);
    }
}



Note that the 'main' declare value 'number', we made it receive an integer and this integer value passed to the method.
You may find it strange that we use 'number' in the 'main' and 'a' in method 'Square ()'. But it is anyway.

In the 'main', the number that the user supplied is stored in the variable 'number', but when we pass this value to the method, we are passing a number.
This number now will be stored in the variable 'num', in method 'square()'.

We call this variable, 'num', local variable because it only exists inside the method. If you try to use it outside the method, get an error.
Test, add the following line to the program:
System.out.println (num)

You will fail to compile, since for the 'main', there is not 'num' variable.

Declare within the method 'squar ()' variable 'sqr' only for educational purposes, to show that we can declare variables inside a method, but the rigor, it is not necessary, we could have simply done:

    public static int square(int num){
        return num * num;
    }



In this example, 'a' would be a parameter and the value of 'number' would be the argument.

Example: passing a parameter list for a method - Calculation of BMI


In the article on Mathematical operations in Java, we pass an exercise to calculate BMI (body mass index). That is nothing but a number, which is calculated by the weight (in kilograms) divided by square of height (in meters) of the person.

Our method takes two float values ​​BMI, weight and height, and returns another float, which is the value of BMI. The declaration of a method with more than one parameter would look like this:
public static float BMI (float weight, float height)

Our program would look like this:



import java.util.Scanner;

public class BMI {
    
    public static float BMI(float weight, float height){
        float bmi;
        bmi = weight/(height*height);
        return bmi;
    }
    
    public static void main(String[] args) {
        float weight, altura, imc;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Type your weight, in kg: ");
        weight = input.nextFloat();
        
        System.out.print("Type your height, int meters: ");
        height = input.nextFloat();
        
        bmi = BMI(weight, height);
        
        System.out.printf("Your BMI is: %.2f",bmi);

    }
}



Note now we call the method and pass two arguments to BMI method: weight and height
Note that the 'main', used exactly the same variables as used in the method (IMC), I did it to show that there are no problems in doing so, because variables method are internal, only exist there.

However, when we use these variables within the 'main', the values ​​are those that the user typed. Within the method, these values ​​can be changed and may represent other values​​.
Be careful not to confuse. Although they have the same name, we are using in different places of our Java application.

Example: calling a method inside the other - Calculation of BMI

Remember I talked several times about the organization, making simple and direct methods?
Yeah, we will show in practice how to do this.

Did you note that in calculating the BMI we elevate one variable to square, height?
Did you note that the first method calculates a square?

You must be thinking:
'Now, let's use the first example to make a calculation in the second.'

Almost. Note that the first example uses integers! But then, in the second example we use in this article, refers to float!
Then we slightly modify our function that calculates the square of a number, to receive and return decimals and use within the BMI method.

The methods would thus:

    public static float square(float num){
        return num*num;
    }    

    public static float BMI(float weight, float height){
        float bmi;
        imc = weight/square(height);
        return bmi;
    }


And out Java program code will be this:

import java.util.Scanner;

public class BMI {
    
    public static void main(String[] args) {
        float weight, height;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Type your weight, in kg: ");
        weight = input.nextFloat();
        
        System.out.print("Type your height, in meters: ");
        height = input.nextFloat();
        
        System.out.printf("Your BMI is: %.2f", BMI(weight, height));

    }
    
    public static float IMC(float weight, float height){
        return weight/square(height);
    }
    
    public static float square(float num){
        return num*num;
    }
}



There,we use a method - square() - inside another,  BMI().
Note that the order in which the methods declared within a class does not matter.







No comments: