Java

TOPIC 28 – FUNCTIONS, Part 1

 

 

LESSON NOTE

 

 

INTRO

 

We have already used some built-in functions from the Math class to help us do some calculations. In this unit, we will learn how to create our very own functions.

 

WHAT EXACTLY IS A FUNCTION?

 

A function is a group of programming statements represented by one name – the function’s name.  A programmer can make use of the function by calling it and passing all required parameters (information).  Most functions use the provided information to do calculations and return a value to the location where they were called.

 

ADVANTAGES OF USING FUNCTIONS

 

Using functions allows for:

  • Code reusability – Statements that will be used over and over can be typed once only.

  • Simplifying complexity – Once a function is created, the programmer no longer needs to consider how it works and can focus on the bigger picture.  More on this later.

  • Organization – Functions allow the programmer to hide all the small details required to make a program operate.  This tends to simplify but also organize the code and the thinking process.  More on this late.

 

FUNCTION = STATIC METHOD

 

Note that Java refers to functions as static methods.  Java also has instance methods that we will learn about at a later time.  We will also learn more about the difference between the two types of methods later.

 

For now, just know that the words method and function are interchangeable.  In fact, we will often refer to functions as static methods.

 

FUNCTION CALLS (BRIEF REVIEW)

 

When we call a function (static method), we must provide the class in which it is located. For example, to call the square root method, we use:

 

Similarly, the DummiesIO functions are also called using the class name in front of them.

We will see later that for instance methods, we do not put the class name in front of the call.

METHOD PROTOTYPES

 

All functions/methods start with their method prototype.  You have been typing the prototype for the main function all along:

 

     static void main(String[] args)

 

All method prototypes have the following form:

 

 

When we create any method, the first thing to decide on is the method prototype.   

 

The method prototype tells us how the method interfaces with other parts of the program.

 

TEMPLATE FOR CLASS CONTAINING FUNCTIONS (STATIC METHODS)

 

Classes that contain only static methods (such as Math and DummiesIO) have the following look:

 

public class ClassName

{

      public methodPrototype1

      {

            methodBody1

      }

 

      public methodPrototype2

      {

            methodBody2

      }

 

      .

      .

      .

 

      public methodPrototypeN

      {

            methodBodyN

      }

}

 

Note that later we will see that we have other options instead of the word “public”.

 

TEMPLATE FOR A FUNCTION

 

Here is the general template for static method:

 

method prototype

{

      //method body (return statement usually at bottom)

}


 

EXAMPLE 1 - DETERMINING THE METHOD PROTOTYPE

Give the method prototype for a method called square that gets a number and returns the square of that number. Therefore, if the number 8.0 is provided, the method returns 64.0.


SOLUTION

A method prototype has these four sections that we need to figure out:

            a) method type

            b) return type

            c) method name

            d) argument list


Here is the information for each section:

            a) The method type is static.

            b) The method will return a number that can have a decimal. Therefore, the return type is             double.

            c) The method name is square (given to us).

            d) The method requires one argument – a number of type double. We will call this variable             n.

 

Therefore, the method prototype is:

 

static double square(double n)

 

IMPLEMENTING A FUNCTION

 

Writing the code that will make a function work is called implementing a function.

 

EXAMPLE 2 – IMPLEMENTING OUR FIRST FUNCTION

 

Implement the square method from above. Place it in a class called OurMath.

SOLUTION 1

 

public class OurMath

{

      public static double square(double n)

      {

            double nSquared = n * n;

            return nSquared;

      }

}

 

SOLUTION 2

 

public class OurMath

{

      public static double square(double n)

      {

            return n * n;

      }

}

 

EXAMPLE 3 – ANOTHER FUNCTION

 

In the class OurMath, add a method called cube that will get a number and return its cube.

 

SOLUTION

 

1 – We can place our new method above or below the square method.

 

           

           


2 – This is what the solution looks like if we pick the bottom location.

 

public class OurMath

{

      public static double square(double n)

      {

            double nSquared = n * n;

            return nSquared;

      }

 

      public static double cube(double n)

      {

            double nCubed = n * n * n;

            return nCubed;

      }

}

 

Note: Notice that we use the variable n in both methods. They are two completely different variables. Remember that a variable only exists within its block (method, if statement, loop).

 

EXAMPLE 4 – CALLING OUR OWN FUNCTIONS


We have created two functions. Now it’s time to make use of them!

 

In a class called OurMathTester, place the main function (just like before). Inside main, get a number from the keyboard, use the functions to get the values of that number squared and cubed and output the results to screen.

 

SOLUTION

 

public class OurMathTester

{

      public static void main(String[] args)

      {

            System.out.println("Enter a number");

            double value = DummiesIO.getDouble();

 

            double square = OurMath.square(value);

            double cube = OurMath.cube(value);

 

            System.out.println("The square is " + square);

            System.out.println("The cube is " + cube);

      }

}