Understanding Parameterized Functions in Java

Understanding Parameterized Functions in Java

In this tutorial, we will explore the concept of parameterized functions in a class by using a simple example of a Calculator class. This will help in understanding how to pass values (parameters) to a class and its methods, manage attributes, and perform operations such as addition, subtraction, and multiplication. We will walk through the code step by step to understand its structure and working. You can visit the detailed tutorial here.

What is a Parameterized Function?

A parameterized function is a function that takes input values, known as parameters, to perform some operations. These parameters are passed when calling the function. By making functions parameterized, we can reuse the same function for different inputs, making the code more flexible and dynamic.

Class with Parameterized Functions: Calculator Example

Let’s begin by defining a Calculator class that can perform basic arithmetic operations (sum, subtract, and multiplication) on two numbers. We will use parameters to pass these numbers into the class methods.

Here’s the code:

public class Calculator {

    // Attributes of the class
    private int a;
    private int b;


    // Method for sum
    public int sum(int a, int b) {
        return a + b;
    }

    // Method for subtraction
    public int subtract(int a, int b) {
        return a - b;
    }

    // Method for multiplication
    public int multiply(int a, int b) {
        return a * b;
    }

    public static void main(String[] args) {
        // Creating an instance of the Calculator class with values for a and b
        Calculator calc = new Calculator();

        // Calling methods and printing the results
        System.out.println("Sum: " + calc.sum(10, 5));
        System.out.println("Subtraction: " + calc.subtract(10, 5));
        System.out.println("Multiplication: " + calc.multiply(10, 5));
    }
}

Step-by-Step Explanation

Class Declaration

The Calculator class is declared with two private attributes a and b, representing the two numbers on which the arithmetic operations will be performed.

public class Calculator {
    private int a;
    private int b;
}

Methods for Arithmetic Operations

Sum: This method returns the sum of the two attributes a and b. public int sum(int a, int b) { return a + b; }

Subtraction: This method returns the result of subtracting b from a. public int subtract(int a, int b) { return a - b; }

Multiplication: This method multiplies a and b and returns the result. public int multiply(int a, int b) { return a * b; }

These methods do not take any parameters because they use the class attributes a and b, which have already been initialized by the constructor.

Main Method

The main method is where the execution of the program begins. Here, we create an instance of the Calculator class.

Calculator calc = new Calculator();

Once the object is created, we can call the methods to perform the operations. For example, to calculate the sum of 10 and 5, we call the sum() method:

System.out.println("Sum: " + calc.sum(10, 5));

Similarly, we call the subtract() and multiply() methods to perform subtraction and multiplication:

System.out.println("Subtraction: " + calc.subtract(10, 5));
System.out.println("Multiplication: " + calc.multiply(10, 5));

Program Execution and Output

When the program is run, it will display the following output:

Sum: 15
Subtraction: 5
Multiplication: 50
  • The sum(10, 5) method adds 10 + 5 and prints 15.
  • The subtract(10, 5) method subtracts 5 from 10 and prints 5.
  • The multiply(10, 5) method multiplies 10 * 5 and prints 50.

Key Points to Note:

  • Parameterized methods allow us to pass values to functions, making the code reusable for different inputs.
  • Class attributes store the values that can be used across different methods within the class.
  • The main method is where the object is created, and the methods of the class are called to perform specific tasks.
Visit the presentation slides here.

6 thoughts on “Understanding Parameterized Functions in Java

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this:
Verified by MonsterInsights