diff --git a/Grade 10/Computer Science/ICS4U1/Methods.md b/Grade 10/Computer Science/ICS4U1/Methods.md index 12586ad..444a7c8 100644 --- a/Grade 10/Computer Science/ICS4U1/Methods.md +++ b/Grade 10/Computer Science/ICS4U1/Methods.md @@ -6,53 +6,63 @@ A java method can be interpreted as a subprogram. It is a collection of statemen ## Built- in vs User-defined Methods -Built-in: -BUild-i nmethods arep art of hte complier package such as system.out.println +**Built-in:** -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 -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: -pubic static return type method name (parameter 1) +**Function (return)- type:** +```java +public static return type method-name (parameter 1) +``` -procedure-type method: +**Procedure-type method:** + +```java public static void method-name (paramenter 1) - +``` ## 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) - **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 -- **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() - -## Method declaration +- **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 body:** It is enclosed between braces. the code that you need to be execute to perfrom your intended operations ```java public int max(int x, int y) { - + if(x > y) { + return x; + } + return y; +} ``` ## How to call a method (method invocation To invoke a method you need the method name with the parameter list defined between parentheses -Exammple: +**Exammple:** +``` Method Name (paramter list) - +``` Must have () parentheses when calling a method, even with no passing parameters ## Pass-By-Value -**What happens:** When a method is called ,a copy of the value of each argument is passed to the method +**What happens:** When a method is called, a copy of the value of each argument is passed to the method **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; double decimal = 5.2; NumberManeuvers(num decimal); @@ -64,8 +74,32 @@ public static void numberManeuvers(int i, double j) { i = 12; } } -// output = num = 10 and decimal = 5.2 +// output: num = 10 and decimal = 5.2 ``` ## 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.