From f75b408a3b1b20f0a0b14718dafe62c95f20cf33 Mon Sep 17 00:00:00 2001 From: Weiszhaar William Date: Fri, 8 Sep 2023 10:48:47 -0400 Subject: [PATCH] wrote functions.md --- Java/Functions.md | 33 ++++++++++++++++++++++++++++++++- Java/Variables.md | 6 +++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Java/Functions.md b/Java/Functions.md index 071fb76..0e391f9 100644 --- a/Java/Functions.md +++ b/Java/Functions.md @@ -2,4 +2,35 @@ order: 98 --- -# Functions \ No newline at end of file +# Functions +A `Function` is a way of packaging code so that we can reuse it. A function can have inputs and outputs, so they can be quite a useful tool in Java programming. + +Here is a simple example: +```java +boolean robotIsOn = false; + +void turnRobotOn() { + robotIsOn = true; +} + +turnRobotOn(); +``` +There is a lot going on here but we will focus on the function declaration. +to declare a function, we simply need a type and a name. The type tells us what the function will return, in this case that is `void`, which is nothing. The name of the function is how we reference it. After the name comes the parentheses, which hold the `paramters` of the function, we will go into depth about parameters later. In this case there are no parameters, so it's just the parentheses. Lastly, we have the curly brackets. These tell Java where the code for the function starts and ends. + +## Parameters and Returns + +Sometimes, we want a function to do something based on an input, and give an output. Thats where `parameters` and `returns` come in. +Here is an example of parameters and returns + +```java +int addTwoNumbers(int a, int b) { + return a + b; +} +int result = addTwoNumbers(3, 4); +``` + +To add parameters to a function, simply give the parameter a type and a name and add a comma in between each parameter. +you can user parameters as a kind of local variable that only exists inside the function declaration. To return something from a function, use the `return` keyword. You can pass either a variable or an expression (as we have in this case). Notice how we changed the functions type to reflect what we are returning. +To call any function, just write the name with a pair of parentheses in which you add any parameters the function has. +For more information on types when working with functions, check out [Types](./Types.md) \ No newline at end of file diff --git a/Java/Variables.md b/Java/Variables.md index b8445fb..c2df989 100644 --- a/Java/Variables.md +++ b/Java/Variables.md @@ -19,9 +19,9 @@ To create a variable, you need 4 things: Type | Value --- | --- -`int` | whole numbers (such as 1, 2, -3, etc.) -`double` | numbers with a decimal (such as 1.234) -`boolean` | can be true or false +`int` | Whole numbers (such as 1, 2, -3, etc.) +`double` | Numbers with a decimal (such as 1.234) +`boolean` | Can be true or false More information on booleans is available at [Control Flow](./ControlFlow.md). More information on types is available at [Types](./Types.md).