• 15 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 3/28/24

Understand parameters and return values

Since the beginning of this course, we have been using functions (or methods), such as  System.out.println(). In this chapter, you will learn the different components of a method declaration (parameters and return value) and the difference between a value type and a reference type.

What is a method declaration composed of?

Let's say you have a 6 (length) by 4 (width) rectangle and you want to print its perimeter.

public static void displayPerimeter() {
    int perimeter = 2*(1 + 2);
    System.out.println(perimeter); // -> 6
}

This is a correctly implemented method, but let's agree, quite useless as you'll always end up working with the same numbers.

Functions with parameters

To address this limitation, you need to make your function accept the numbers from the outside. This can be done by defining a function with parameters.

In Java, the variable name and type of the parameter are part of function declaration. Here's what it looks like:

public static void displayPerimeter(int length, int width) {
    int perimeter = 2 * (length + width);
    System.out.println(perimeter); // -> depends on the values of length and width
}

Parameters are variables listed in the function declaration that are specified inside () by the name and type. Now you can call this function and pass any values you want to it:

displayPerimeter(10, 11); // -> 42
displayPerimeter(2, 2); // -> 8

Each value is assigned to a parameter in the order they are defined.

Let's get deeper into the terminology. Parameters are the variables declared in a function. The values passed to that function are called arguments.

This is great, we've added some purpose to our function.

Now, what can we do about the result?

Often, the code that called the function needs an answer to perform its own work. This answer can be provided by the return value of the function.

Define a return value

To define a return value, you need to:

  1. Alter the declaration of the function to indicate that it's expected to return a result.

  2. End the function with the return keyword.

Here is how to turn the displayPerimeter into a calculatePerimeter function. This will return the result of the calculation so that it can be used by the calling function:

public static int sum(int a, int b) 
{   
    int calc = a + b;
    return calc;
}

You could also forgo the sum variable and return directly the result of the calculation expression:

public static int sum(int a, int b){
    return a + b;
}

Once your function is defined, you can use it as many times as you want. Here are a couple of variations:

int sumOfSmallNumbers = sum(3,7); 
System.out.println(sumOfSmallNumbers); // -> 10
int sumOfLargerNumbers = sum(sumOfSmallNumbers,999);
System.out.println(sumOfLargerNumbers); // -> 1009

In the example above, we used the result of the first calculation as a parameter for the next one.

Another thing you need to understand: some variables store a value directly, while others store a reference. This has an impact on how they are sent as arguments to a function.

Differentiate value types and reference types

You've learned about variables types which can be simple - like numbers or strings, or, more complex, like classes.

Each of those types are classified as either a value type or a reference type. 

Let's start with the value type.

Value types

Let's use integers as an example. If you create an integer variable and then assign it to another variable, the value of the first variable will be copied to the second one:

// declare variable a as an int and initialize it with a value of 10
int a = 10;
// declare variable b as an int and initialize it with a copy of the value of a
int b = a;

 
Since int is a value type, b is assigned a copy of the value of a. That means that if you later change the value of  a , the value of  b will not be changed.

System.out.println(a); // -> 10 
System.out.println(b); // -> 10
a = 15;
System.out.println(a); // -> 15 
System.out.println(b); // -> 10

 
That's pretty straightforward. Next comes reference types.

Reference types

We'll work with classes in this section. When you create a variable and assign an instance of class to it, the object gets created, but the variable itself holds a reference to that object:  the location of where it's stored in the memory. Then, when you assign the value of that variable to another variable, the object itself will not be copied, and the new variable will hold a reference to it - the same address.

 
Here's an example:

public class Car {
    String color = "red"; 
}
Car car = new Car();
Car carToPaint = car;


 
If you want to paint the car which is accessible by the second variable, the first variable will be also affected as both point to the exact same instance of the car class:

public class Car {
    String color = "red" 
    
}
Car car = new Car();
Car carToPaint = car; 
System.out.println(car.color); // -> red 
System.out.println(carToPaint.color); // -> red
carToPaint.color = "yellow";
System.out.println(car.color); // -> yellow 
System.out.println(carToPaint.color); // -> yellow

 
See the difference? ⚡️This is very useful as it saves lots of memory space when you need to pass around the same object along your program. However, this can also be dangerous as changing an object in one part of the program will affect all other parts of the program working with the same object.

This is something you'll need to pay attention to with parameters of functions. When you pass reference types as parameters, all modifications you perform on them within those functions modify the original objects:

class Car {
    String color = "red"; 
    
}
public void paint( Car car, String color) { 
    car.color = color; 
}

Car car = new Car(); 
System.out.println(car.color); // -> red
paint(car, "green"); 
System.out.println(car.color); // -> green 

Hmm...how about parameters of the functions being constants in Java? Parameters of a function in Java are indeed constants and cannot be modified, which means you cannot assign a new value to the whole new object (or, a reference to an object). But you CAN modify attributes of that object, which is exactly what we did in the example above.

Lot's of new knowledge here, you are progressing so fast!

Summary

In this chapter, you've learned:

  • Functions can have parameters and return values.

  • A return value is a result of the function's execution. It can be returned to the block of code that called the function and then used as needed.

  • Parameters are the input for a function that are necessary for the it to be executed and produce a result.

  • Parameters are variables defined by name and type. They are specified in the function's declaration.

  • When calling a function, you are passing values to those variables. Those values are called arguments.

Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best
Example of certificate of achievement
Example of certificate of achievement