0% found this document useful (0 votes)
5 views12 pages

4 Methods

Uploaded by

elishajo96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

4 Methods

Uploaded by

elishajo96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Java Methods

lt4
Java Methods
• A method is a block of code which only runs when it is called.

• You can pass data, known as parameters, into a method.

• Methods are used to perform certain actions, and they are also
known as functions.
Java Methods…
• Why use methods?
• To reuse code:
• define the code once, and use it many times.
Create a Method
• A method must be declared within a class.
• It is defined with the name of the method, followed by parentheses
(). Java provides some pre-defined methods, such as
System.out.println(), but you can also create your own methods to
perform certain actions:
Call a method
• To call a method in Java, write the method's name followed by two
parentheses () and a semicolon;

• In the following example, myMethod() is used to print a text (the


action), when it is called:
Demo Method with parameter & Tasks
• Demo Method ya Kujumlisha and 3 other
Return Values
• The void keyword, used in the examples demo, indicates that the
method should not return a value. If you want the method to return a
value, you can use a primitive data type (such as int, char, etc.)
instead of void, and use the return keyword inside the method:
Java Method Overloading
• With method overloading, multiple methods can have the same name
with different parameters:

• int myMethod(int x)
• float myMethod(float x)
• double myMethod(double x, double y)

• Demo plus int and doubles


Java Scope
• In Java, variables are only accessible inside the region they are
created. This is called scope.
Method Scope
• Variables declared directly inside a method are available anywhere in
the method following the line of code in which they were declared:
• Block Scope
• A block of code refers to all of the code between curly braces {}.

• Variables declared inside blocks of code are only accessible by the


code between the curly braces, which follows the line in which the
variable was declared:
Java Recursion
• Recursion is the technique of making a function call itself. This
technique provides a way to break complicated problems down into
simple problems which are easier to solve.
• Recursion Example
• Adding two numbers together is easy to do, but adding a range of
numbers is more complicated. In the following example, recursion is
used to add a range of numbers together by breaking it down into the
simple task of adding two numbers:
Halting Condition
• Just as loops can run into the problem of infinite looping, recursive
functions can run into the problem of infinite recursion. Infinite
recursion is when the function never stops calling itself. Every
recursive function should have a halting condition, which is the
condition where the function stops calling itself. In the previous
example, the halting condition is when the parameter k becomes 0.

You might also like