FUNCTIONS
Advanced Programming in Java
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
Agenda
■ Why?
■ What?
■ How?
■ Examples..
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
Why Use Functions?
■ Longer programs are harder to organize
and read.
■ Using modular programming techniques
help you break a long program into
individual modules, each to perform
certain tasks.
■ Modular programming helps you use a
top-down approach to program design.
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
Why Use Functions?
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
What are Functions?
■ They can be called functions, methods,
and subroutines.
■ Difference? Basically a method is a
Java function that is a part of a class,
and a subroutine is a function that
returns no value.
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
What are Functions?
■ A function is a block of organized, reusable code that is used to perform a single
related action. - tutorialspoint.com
■ We’ve used functions before (printf, nextInt, main) these are called built-in functions
that are provided by the language itself, but now we can write/define our own
functions.
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
How to Define a Function?
■ General form:
return_type function_name ( parameter(s) ) Function Header
{
Function Signature
//code goes here
Function Body
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
How to Define a Function?
■ General form:
return_type function_name ( parameter(s) )
{
Data type of the value
// code goes here returned at the end of the
function. If no value to be
returned, then it is void.
}
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
How to Define a Function?
■ General form:
return_type function_name ( parameter(s) )
{
A name for the function
// code goes here that will be used when
calling it.
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
How to Define a Function?
■ General form:
return_type function_name ( parameter(s) )
{
The list of parameters, used as placeholders
// code goes here for values sent to the function as required.
This list of parameters would be followed in
terms of type, order, and number of
} parameters.
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
How to Define a Function?
■ General form:
return_type function_name ( parameter(s) )
{
Actual implementation of the function, the
// code goes here group of statements that will be executed
each time the function is called.
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
How to Define a Function?
■ A function that prints any message surrounded by stars:
public void printMessage ( String msg ) {
System.out.println(“** “+ msg + “ **” );
}
■ A function that doubles an integer value:
public void doubleValue ( int value ) {
value = value * 2;
}
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt
How to Call a Function?
■ A function is called by its name, with values sent for the required parameters. If a
value to be returned, a variable should be defined to receive the returned value.
printMessage(“Hello There”);
doubleValue( 2 );
■ Arguments passed to a function are passed by value, which means that a copy of
the passed value is given to the function.
■ Then how the function doubleValue should be corrected?
Advanced Programming, © Amira Sayed A. Aziz – Future University in Egypt