0% found this document useful (0 votes)
11 views

JAVA Methods

This document discusses methods in Java programming. It explains that methods allow programmers to break complex programs into smaller, reusable tasks to reduce complexity. Methods are blocks of code that are defined once but can be called many times from other parts of the program. Methods may accept parameters as input and can return values. Defining methods helps programmers write cleaner, more organized code that is easier to understand and maintain.

Uploaded by

EL- Musa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

JAVA Methods

This document discusses methods in Java programming. It explains that methods allow programmers to break complex programs into smaller, reusable tasks to reduce complexity. Methods are blocks of code that are defined once but can be called many times from other parts of the program. Methods may accept parameters as input and can return values. Defining methods helps programmers write cleaner, more organized code that is easier to understand and maintain.

Uploaded by

EL- Musa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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