1
0
mirror of https://gitlab.com/magicalsoup/Highschool.git synced 2025-01-24 00:21:45 -05:00

Update Methods.md

This commit is contained in:
James Su 2019-09-18 16:29:04 +00:00
parent 9c10a3791b
commit 185786e85e

View File

@ -6,45 +6,55 @@ A java method can be interpreted as a subprogram. It is a collection of statemen
## Built- in vs User-defined Methods ## Built- in vs User-defined Methods
Built-in: **Built-in:**
BUild-i nmethods arep art of hte complier package such as system.out.println
Function (return) - Type: it calcualtes and return a value Built-in methods are part of the complier package such as `System.out.println`
**Function (return) - Type:** it calcualtes and return a value
```java ```java
public static int claculate(int number){ public static int calculate(int number){
return 1;
}
``` ```
procedure-type: executes some commands **Procedure-type:** executes some commands
Function (return)- type: **Function (return)- type:**
pubic static return type method name (parameter 1) ```java
public static return type method-name (parameter 1)
```
procedure-type method: **Procedure-type method:**
```java
public static void method-name (paramenter 1) public static void method-name (paramenter 1)
```
## How to create a method ## How to create a method
In general, method declarations has 5 basic components (figure 2.): In general, method declarations has 5 basic components:
- **Modifier:** defines access type of the method i.e. from were it can be accessed in your application (For example: public) - **Modifier:** defines access type of the method i.e. from were it can be accessed in your application (For example: public)
- **The return type:** the data type of the value returned by the method or void if it does not return a value (procedure and function type) - **The return type:** the data type of the value returned by the method or void if it does not return a value (procedure and function type)
- **Method name:** a specific names that identifies the method that can be used to invoke it later - **Method name:** a specific names that identifies the method that can be used to invoke it later
- **Parameter list:** Comma separated list of the input parameter are defined preceded with their data type, within the enclosed parentheses, If there are no parameters, you must use empty parenteses() - **Parameter list:** Comma separated list of the input parameter are defined preceded with their data type, within the enclosed parentheses, If there are no parameters, you must use empty parentheses()
## Method declaration
- **Method body:** It is enclosed between braces. the code that you need to be execute to perfrom your intended operations - **Method body:** It is enclosed between braces. the code that you need to be execute to perfrom your intended operations
```java ```java
public int max(int x, int y) { public int max(int x, int y) {
if(x > y) {
return x;
}
return y;
}
``` ```
## How to call a method (method invocation ## How to call a method (method invocation
To invoke a method you need the method name with the parameter list defined between parentheses To invoke a method you need the method name with the parameter list defined between parentheses
Exammple: **Exammple:**
```
Method Name (paramter list) Method Name (paramter list)
```
Must have () parentheses when calling a method, even with no passing parameters Must have () parentheses when calling a method, even with no passing parameters
## Pass-By-Value ## Pass-By-Value
@ -52,7 +62,7 @@ Must have () parentheses when calling a method, even with no passing parameters
**In the second method:** This copy can be changed inside the method, however such a change has no effect on the actual argumnet **In the second method:** This copy can be changed inside the method, however such a change has no effect on the actual argumnet
``` ```java
int num=10; int num=10;
double decimal = 5.2; double decimal = 5.2;
NumberManeuvers(num decimal); NumberManeuvers(num decimal);
@ -64,8 +74,32 @@ public static void numberManeuvers(int i, double j) {
i = 12; i = 12;
} }
} }
// output = num = 10 and decimal = 5.2 // output: num = 10 and decimal = 5.2
``` ```
## Pass-By-Reference ## Pass-By-Reference
**What happens:** When an object (Array, String) is passed to a method, its memory location address (rreference point) is used
**The object:** Arrays & trings behave like objects
**In the second method:** When their memory location is passed to the method the oject can be manipulated in the method resulting in actual changes to the object (Array, String)
```
int [] nums = {1, 2, 3};
testingArray(num);
System.out.println("num[0] = " + num[0] + ", num[1] = " + num[1] + ", num[2] = " + num[2]);
public static void testingArray(int[] value) {
value[0] = 4;
value[1] = 5;
value[2] = 6;
}
}
```
## Benefits to methods
There are many advantages of using methods. Some of them are listed below:
- It makes the program well structured
- methods enhance the readability of the code.
- It provides an effective way for the user to reuse the existing code.
- Allows for easier debugging.