Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Functions

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Functions in java

Methods

A program that provides some functionality can be long and contains many statements

A method groups a sequence of statements and should provide a well-defined, easy-


to-understand functionality

a method takes input, performs actions, and produces output

In Java, each method is defined within specific class


CREATING A METHOD

In general, a method has the following syntax:

modifier returnValueType methodName(list of parameters) {

// Method body;

}
Method Declaration

Modifier-: Defines access type of the method i.e. from where it can be accessed in your application. In Java, there 4 type of the access specifiers.

public: accessible in all class in your application.

protected: accessible within the class in which it is defined and in its subclass(es)

private: accessible only within the class in which it is defined.

default (declared/defined without using any modifier) : accessible within same class and package within which its class is defined.

The return type : The data type of the value returned by the the method or void if does not return a value.

Method Name : the rules for field names apply to method names as well, but the convention is a little different.

Parameter list : Comma separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis. If there
are no parameters, you must use empty parentheses ().

Exception list : The exceptions you expect by the method can throw, you can specify these exception(s).

Method body : it is enclosed between braces. The code you need to be executed to perform your intended operations.
Basic Syntax
Method signature

It consists of method name and parameter list (number of parameters, type of


the parameters and order of the parameters). Return type and exceptions are
not considered as part of it.
Method Signature of above function:

max(int x, int y);


How to name a Method?

: A method name is typically a single word that should be a verbin lowercase or multi-word, that
begins with a verb in lowercase followed by adjective, noun….. After the first word, first letter
of each word should be capitalized. For example, findSum,
computeMax, setX and getX

Generally, A method has a unique name within the class in which it is defined but sometime a
method might have same name as other method name within the same class as method
overloading is allowed in Java.
Method Declaration: Header

A method declaration begins with a method header

class MyClass
{ …
static int min ( int num1, int num2 )

method parameter list


name
The parameter list specifies the type
return and name of each parameter
type
The name of a parameter in the method
properties declaration is called a formal argument
Two Types of Methods

1. Return a value
next = keyboard.nextInt();

keyboard is the calling object.

2. Don’t return a value, called void method


System.out.println(“Enter data:”);

System.out is the calling object


Anyone explain to me in which instance a void method is used vs. a value
returning method (or vice versa)

It just depends on what you want to accomplish! You create a method when you want to group a block of
code together that usually does a certain task and can be used multiple times which prevents code
repetition. You would only need to 'return' a value when you need to get something from a method as
values that are created in methods are stored there locally.

For example, if I wanted to create a method which would print a String that I pass in as a parameter, I would write

public static void printStr(String str) {

System.out.println(str);

In this case, there is no value that I would need to use somewhere else in the program.
Anyone explain to me in which instance a void method is used vs. a value
returning method (or vice versa)

Now, say I wanted to create a method that would take the sum of two numbers that I pass in as parameters and then be able to use that result, I would write

public static int findSum(int x, int y) {

return x + y;

In this case, I could assign the method findSum(3, 5) to a variable or use it in any other way I would need and it would be equal to whatever it is returning.

public static void main(String[] args) {

x = findSum(5, 2); // x would be 7

System.out.println(findSum(4, 3) + 3); // Output would be: 10

}
Difference between loops and functions

functions: a function is a piece of code you can run over and over again simply by calling
it. All methods are functions, but not all functions are methods. You can have a function
that isn't tied to a class and is sort of free-floating in your code and will execute every
time you call it. But all functions defined within a class are called methods.

Loops: A loop is a piece of code that runs over and over again until the condition is false.
Or in the case of a for in loop until the number of items to iterate over has reached an
end. A loop can be just about anywhere in your code. It can even be inside a function or
method.
public void bar(int num1,int num2) {

System.out.print(num1+num2);

int a = 3; int b = 5;
bar(a, b);

You can picture in your head that when bar(a, b) is run, it's like in the beginning of bar the following two lines are written:

And only then the rest of the method is run.


This means that a value get copied to num1 and b value get copied to num2. Changing the values of num1 and num2 will not affect a and b.
If the arguments were objects, the rules remain the same, but it acts a bit differently.

You might also like