OOP: What are parameters and when to use them

Parameters is a very important topic in Object-Oriented Programming and other programming paradigms. It enables you to define input for algorithms implementations in a certain programming language.

During many years of teaching programming, I observed that many students struggle to fully grasp this concept and use it when required.

In this article, I’ll give a practical explanation and relate it to our day-to-day life.

What is a parameter in programming?

I won’t give a formal definition, rather a practical view of what a parameter is.

When you studied algorithms (you can find a short course in this link), you should learn that an algorithm is a finite sequence of steps that solve a certain problem. Also, they have an input (the data that is needed to solve the problem) and an output (the solution to the problem).

In short, parameters are the input of an algorithm.

So, anytime you are implementing an algorithm that has an input, you must use parameters to store the input of the algorithm.

Parameters in Object-Oriented Programming

In OOP, we are going to use parameters mostly in class methods.

“Another part of the class is operations (or methods). Those operations are actions that one can carry on using the information (attributes) related to the specific class.” You can find this explanation in context, at this link.

As you can see above, we use class methods to implement actions. The actions usually (not always) act upon the values stored in the class attributes. However, sometimes extra information is needed to carry out those actions. If that is the case, then we must use parameters.

Parameters on our day-to-day life

Did your mom ever ask you to go to the shop? Your answer will probably be yes.

If yes, she told you what to do there, that is the input you got. In programming, we translate that input into parameters.

Let’s see another example.

You are in the shop, you got everything you want to buy in a basket, and you go to the cashier. You stop there because you want to pay. But how much are you going to pay?

You have to wait until the cashier tells you what the price is. That price is your input and, we will translate that input into a parameter while implementing that action using a programming language.

Another example, you call a taxi. Obviously, it is because you want to go somewhere. But, when the taxi stops, what is the first thing that usually happens? The driver will ask you where you want to go. That is his input for him to tell you if he can help you or not.

As you can see, our day-to-day life is full of examples where we need an input to carry out some action. You can leave a comment with an activity that you usually do so we can see if you fully grasp the idea.

Those inputs that we use in our day-to-day, are translated to parameters when we talk about programming.

Don’t forget that we use programming, especially Object-Oriented Programming, to model situations from the real world.

Example of when to use parameters

Imagine that we have the class Laptop below, and we want to modify the price of the laptop.

class Laptop{ 
  private int screenSize; 
  private float price; 
  Laptop(int size, float price){ 
    this.screenSize = size; 
    this.price = price; 
  } 
}

To do that, we first must think/design an algorithm that can change the price of the laptop. This is a trivial algorithm, that will assign a new value to the attribute price.

Now, what is the new price? We don’t know and, that is because the price is the input of the algorithm to change the old price for a new price. This is a clear indication that the new price is a parameter in the new method we must implement.

void setPrice(float newPrice){
  Price = newPrice;
}

Let’s see another example.

We have a class that represents a University Department. One of its attributes is an array of students.

We want to know if a specific student is registered in that department.

Again, the first thing we should do is to design an algorithm capable of giving the solution to the previous problem.

In this case, we can use the searching basic algorithm to find out if a specific element (a student) is in an array.

So, what do we need apart from the array of students to solve this problem?

Yes, you are right, we need to know information about the specific student. It should something that identifies uniquely the student. It can be the ID number, the student number or any other depending on the information about the available students.

Once we have that information, we can go through the array of students asking if the information of the current student equals the input we have. If it is true we return that we found the student.

So, we already have an algorithm, and we know what the input is. Now, that input is a parameter of the method that will implement the algorithm we just designed.

Summary

The key point here is what we need as input to implement an algorithm is translated into a parameter when we implement the algorithm in a specific programming language.

If the algorithm does not have any input, then we won’t have parameters in the method that implements the algorithm.

However, if the algorithm has an input, that will be a parameter in the implementation of the algorithm.

H@ppy Coding!