Java Basics - Functions 2

Parameters and Arguements.

Parameters are variables in the parentheses of our function defintion, that we can be used inside a function.

For example:

public void greeting(String location){
        //greet a specific location
        System,out.println("Hello, " + location);
}
_________________________________

Arguments are specific values passed into our function call.

For example: 

greeting("Australia");

___________________________________

How to list multiple parameters in a function:

For example: 

public void boxVolume (int height, int width, int depth){
//Code goes here
}
____________________________________

Return Values
In the following example:  public void functionName(). The void (return type) means not returning any data  outside the function  we can only complete an action.

In the following example: public boolean functionName(). The boolean means we can return a boolean value outside the function definition

To return any data, we need to change the return type (like done above) and have a return statement..

For example;
public int randomFunction(int denisty){
//execute code
return size;
}

Now we can store the data in a variable for example.

int randomVariable = randomFunction(50);

As you can see the return value lets you use values in other function calls or programs.
_____________________________________

Comments

Popular posts from this blog

Java Basics - Variable and Data Types Part 1

Java Basics - Variable and Data Types Part 2

OOP - Objects and Classes