Topic 8: Functions (Pass by value)

A function (also called methods or behavious) is a block of code grouped with a name and performs a task. Every java program contains at least one function i.e. the main function. Some functions are built-in functions, defined in libraries eg. System.out.println(), abs()-Returns absolute value, charAt() Method (Returns a character by index position), compareTo() - compares two strings. Others, called program-defined functions, are written by programmers.

Functions allow for blocks of code to be used many times in a program without having to duplicate code. It also allows large, complex programs to be broken down into small, manageable sub-tasks. Each sub-task is solved by a function, and thus different people can write different functions.

Many functions can then be combined into a single program. Typically, the main is used to call other functions, but any function can call any other function.

All functions are either value-returning or void. All value-returning functions perform a task and then return precisely one value. This value is returned to the statement that is called the function. The returned value can be assigned to a variable, used in a comparison or calculation or could be printed on the screen.

User-Defined Functions

A program-defined value-returning function definition is composed of a header and a body. Header (first line) contains return data type, name of the function, and an optional parameter List.

A method must be declared within a class.

Syntax:
...
Figure 8.1

Rules for function names are the same as for variables. Good idea to use meaningful names that describe the function’s purpose. Verb is more preferable.

Memory locations in parameterList are called formal parameters - each store an item of information passed to the function when it is called.

The function body contains instructions for performing the function’s assigned task. Surrounded by braces ({}).

The last statement is usually the return statement - returns one value (must match return data type in function header). After the return statement is processed, program execution continues in the calling function. Note that return statement is not necessary for void fuctions.

A method must be declared within a class.

Example
  • ...
    Figure 8.2

Static denotes that the method is a member of the Main class and not one of its objects. Later in this course, you will learn more about objects and how to access methods using them.

Calling a Function

A function must be called (invoked) to perform its task. The main function is automatically called when the program is run while other functions must be called by a statement.

Syntax:
functionName([argumentList]);
  • argumentList is list of actual arguments (if any)
  • An actual argument can be a variable, named constant, literal constant, or keyword

Void functions

Void functions also perform tasks like value returning functions however it does not return a value. A void function may be used to do something like display information on the screen. The function header begins with the keyword void, instead of a return data type. The function body does not contain a return statement. Call a void function by including its name and actual arguments (if any) in a statement. A call to a void function appears as a self-contained statement, not part of another statement because void functions do not return values.

Syntax:
  1. static void functionName([parameterList])
  2. {
  3. statement;
  4. }
Example: To call the following function simply say displayCompanyInfo();
    1. static void displayCompanyInfo()
    2. {
    3. System.out.println(“XYZ Company Ltd”);
    4. System.out.println(“Fiji Islands”);
    5. }