JAVA Methods

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Unit 1: Programming

Unit code: D/615/1618


Unit level: 4

Content Prepared by:

School of Computing and Informatics 1


2

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed


▪ When programs become more and more
complex, it becomes big and chaotic to
handle.
▪ One of the most powerful techniques to
reduce this complexity is “Divide and
TO COPE WITH Conquer” technique which is to take a
COMPLEXITY complex task and divide it into smaller
and easily understandable tasks.
▪ In Java, we accomplish this technique
with the help of methods.

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 3


▪ A method is a block of code which
only runs when it is called.

▪ Methods are used to perform


certain actions, and they are also
known as functions

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 4


▪ To reuse code: define the code once
and use it many times.
▪ Once a task is packaged in a method,
that method is available for accessing
anywhere from the program.

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 5


▪ The general form of a function/method
is given below:

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 6


▪ A function or a method must be defined before it is used anywhere in the program.
▪ A method must be declared within a class.

public class Main


{
Modifier The Return Type Parameter list
is empty
static void myMethod()
{ The name of the method

// code to be executed
} The function body
}
7
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 8
▪ To call a method in Java, write the method's name followed by two
parentheses () and a semicolon; in the main function

public class Main


{
static void myMethod()
{
System.out.println("I just got executed!");
}

public static void main(String[] args)


{
myMethod();
}
} // Outputs "I just got executed!"
9
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed
▪ A method can also be called multiple times:
public class Main
{
static void myMethod()
{
System.out.println("I just got executed!");
}

public static void main(String[] args)


{
myMethod();
myMethod();
myMethod();
}
}
//I just got executed!
//I just got executed!
//I just got executed!

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed


10
When the method
is called, we pass
a data Parameters are
that belongs to specified after the
the parameter method name, inside
type and count, as the parentheses You can pass data into
arguments a method,
as parameters

Parameters
act as
variables inside
the method
You can add
as many parameters as you
want, just separate
them with a comma.

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 11


public class Main {

static void myMethod(String fname) // Parameter


{
System.out.println(fname + " Refsnes");
}

public static void main(String[] args)


{
myMethod("Liam"); // Argument
myMethod("Jenny");// Argument
myMethod("Anja"); // Argument
}
}
Output:
// Liam Refsnes
// Jenny Refsnes
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 12
// Anja Refsnes
Note that when you are working
with multiple parameters,
the method call must have the
same number of arguments
as there are parameters, and
the arguments must be passed
in the same order.

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 13


void means that
The void keyword, this method does
indicates that the method not have a return
should not return a value.
value.

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 14


▪ If you want the method to return a value, you can use a data type (such as int, char,
etc.) instead of void and use the return keyword inside the method

You can store the


result in a variable
or print function
call directly

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 15


Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 16
17

Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed

You might also like