Functions in Java

Agenda

  • Definition and Syntax

  • Arguments

  • Return types

  • Scope of a variable

  • Call stack & Heap Memory

  • Variable Arguments

Introduction

Java functions (also known as methods) are blocks of code that are only executed when they are called. 

We can pass values to methods, known as parameters, and return values from them as well.

Benefits of functions/methods:

- They allow us to divide our code in logical blocks.

- They allow us to reuse code.

class Main {
    public static void main(String[] args) {
        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");
    }
}
class Main {
    public static void main(String[] args) {
        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");

        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");

        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");
    }
}
class Main {
    public static void main(String[] args) {
        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");

        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");

        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");
    }
}
class Main {
    public static void main(String[] args) {
        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");

        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");

        // Making tea
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");
    }
}
static void tea() {
    System.out.println("Boil water with tea leaves and sugar");
    System.out.println("Add milk and boil for a few minutes");
    System.out.println("Serve the milk tea");
}
class Main {
    public static void main(String[] args) {
        // Making tea
        tea();

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");

        // Making tea
        tea();

        // Making tea
        tea();

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");
    }
}
static void tea() {
    System.out.println("Boil water with tea leaves and sugar");
    System.out.println("Add milk and boil for a few minutes");
    System.out.println("Serve the milk tea");
}
class Main {
    public static void main(String[] args) {
        // Making tea
        tea();

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");

        // Making tea
        tea();

        // Making tea
        tea();

        // Making coffee
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");
    }
}
static void tea() {
    System.out.println("Boil water with tea leaves and sugar");
    System.out.println("Add milk and boil for a few minutes");
    System.out.println("Serve the milk tea");
}
static void coffee() {
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");
}
class Main {
    public static void main(String[] args) {
        // Making tea
        tea();

        // Making coffee
        coffee();

        // Making tea
        tea();

        // Making tea
        tea();

        // Making coffee
        coffee();
    }
}
static void tea() {
    System.out.println("Boil water with tea leaves and sugar");
    System.out.println("Add milk and boil for a few minutes");
    System.out.println("Serve the milk tea");
}
static void coffee() {
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");
}
class Main {
    static void tea() {
        System.out.println("Boil water with tea leaves and sugar");
        System.out.println("Add milk and boil for a few minutes");
        System.out.println("Serve the milk tea");
    }

    static void coffee() {
        System.out.println("Boil some milk");
        System.out.println("Put some coffee in the cup");
        System.out.println("Pour the milk into the cup");
    }

    public static void main(String[] args) {
        // Making tea
        tea();

        // Making coffee
        coffee();

        // Making tea
        tea();

        // Making tea
        tea();

        // Making coffee
        coffee();
    }
}

Syntax of a function

void hello(String name) {
	System.out.println("Hello World");
}

Methods can accept values while being called. These values are stored in local variables known as parameters or arguments. 

A method can have multiples or none arguments

The data type of each argument must be defined in the function signature.

Parameters and Arguments

void introduce(String name, int age, String[] hobbies) {
    System.out.println("My name is " + name);
    System.out.println("I am " + age + " years old");
    System.out.println("My hobbies are :");
    for (String hobby : hobbies) {
      	System.out.println("- " + hobby);
    }
}

Parameters and Arguments

void introduce(String name, int age, String[] hobbies) {
    System.out.println("My name is " + name);
    System.out.println("I am " + age + " years old");
    System.out.println("My hobbies are :");
    for (String hobby : hobbies) {
      	System.out.println("- " + hobby);
    }
}
public static void main(String[] args) {
        String name = "Peter Parker";
        int age = 20;
        String[] hobbies = {"Photography", "Fighting Crime"};
        introduce(name, age, hobbies);
}

Parameters and Arguments

void introduce(String name, int age, String[] hobbies) {
    System.out.println("My name is " + name);
    System.out.println("I am " + age + " years old");
    System.out.println("My hobbies are :");
    for (String hobby : hobbies) {
      	System.out.println("- " + hobby);
    }
}
public static void main(String[] args) {
        introduce("Peter Parker", 20, new String[]{"Photography", "Fighting Crime"});
}

Square of a number

Write a function which computes and prints the square of a number

 

Sample Input

N = 5

 

Sample Output

25

Square of a number

Write a function which computes and prints the square of a number

 

Sample Input

N = 5

 

Sample Output

25

class Main {
    public static void main(String[] args) {
        square(5);
    }

    static void square() {
        
    }
}

Square of a number

Write a function which computes and prints the square of a number

 

Sample Input

N = 5

 

Sample Output

25

class Main {
    public static void main(String[] args) {
        square(5);
    }

    static void square(int num) {
        
    }
}

Square of a number

Write a function which computes and prints the square of a number

 

Sample Input

N = 5

 

Sample Output

25

class Main {
    public static void main(String[] args) {
        square(5);
    }

    static void square(int num) {
        System.out.println(num * num);
    }
}

- Java methods can also optionally return some values back to the caller using the return statement. 

- The data type of the value being returned must be specified in the method signature beforehand.

- The return type can be any valid data type in Java.

- In case no value needs to be returned, specify the return type as void.

Return Types

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

The return type of this function is int.

The function returns the square value of num.

Return type

Return statement

Method overloading is the act of having multiple methods having same name but different parameters.

It increases the readability of the program.

Method Overloading

class Main {
    static int add(int a, int b) {
        System.out.println("Inside first add");
        return a + b;
    }

    static String add(String a, String b) {
        System.out.println("Inside second add");
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(5, 4));
        System.out.println(add("Hello ", "World"));
    }
}

The call stack is what a program uses to keep track of method calls. The call stack is made up of stack frames—one for each method call.

Call Stack

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Call stack

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Call stack

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Call stack

main()

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Call stack

main()

int x = 0;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
 

Call stack

main()

int x = 0;

first()

int x = 10;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
 

Call stack

main()

int x = 0;

first()

int x = 10;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
Inside first(), x = 10

Call stack

main()

int x = 0;

first()

int x = 10;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
Inside first(), x = 10

second()

int x = 20;

Call stack

main()

int x = 0;

first()

int x = 10;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
Inside first(), x = 10
Inside second(), x = 20

second()

int x = 20;

Call stack

main()

int x = 0;

first()

int x = 10;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
Inside first(), x = 10
Inside second(), x = 20

second()

int x = 20;

third()

int x = 30;

Call stack

main()

int x = 0;

first()

int x = 10;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
Inside first(), x = 10
Inside second(), x = 20

Inside third(), x = 30

second()

int x = 20;

third()

int x = 30;

Call stack

main()

int x = 0;

first()

int x = 10;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
Inside first(), x = 10
Inside second(), x = 20

Inside third(), x = 30

second()

int x = 20;

Call stack

main()

int x = 0;

first()

int x = 10;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
Inside first(), x = 10
Inside second(), x = 20

Inside third(), x = 30

Call stack

main()

int x = 0;

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
Inside first(), x = 10
Inside second(), x = 20

Inside third(), x = 30

Call stack

public class Main {
    public static void main(String[] args) {
        int x = 0;
        System.out.println("Inside main(), x = " + x);
        first();
    }

    static void first() {
        int x = 10;
        System.out.println("Inside first(), x = " + x);
        second();
    }

    static void second() {
        int x = 20;
        System.out.println("Inside second(), x = " + x);
        third();
    }

    static void third() {
        int x = 30;
        System.out.println("Inside third(), x = " + x);
    }
}

Inside main(), x = 0
Inside first(), x = 10
Inside second(), x = 20

Inside third(), x = 30

The scope of a variable is the region of the program where it is accessible.

There are two types of scope levels in Java

- Class level scope (covered later on in OOPS section)

- Block level scope

Scope of a variable

class Main {
    public static void main(String[] args) {
        int a = 5;
        if (true) {
            a = 10;
            System.out.println("Inside the if, a = " + a);
        }
        System.out.println("Outside the if, a = " + a);
    }
}

In Java, an argument of a method can accept arbitrary number of values. This argument that can accept variable number of values is called varargs.

 

Syntax:

 

Variable Arguments - varargs

return_type methodName(dataType... args) {
    // body
}

Variable Arguments - varargs

Write a function that takes in integer numbers and returns their average.

Functions

By Tarun Luthra

Functions

  • 23