Changes between Version 4 and Version 5 of ControlSystems/SoftwareTeam/Training/GettingStarted/Methods
- Timestamp:
- Aug 3, 2020, 5:49:07 PM (5 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/Training/GettingStarted/Methods
v4 v5 1 Modern programming languages are very small, but they allow you to extend the language by creating new command words. [[BR]] 2 In Java, a new word is called a ''function'' or ''method''. Functions are a powerful way to keep your code readable and easy to maintain.[[BR]] 3 A function is an abstraction: it does something, but you don't have to know how it does it in order to use it.[[BR]] 4 You can think of a function as a machine. All machines have some function (do something): 5 * Some machines (like a meat grinder) require input of raw materials and output (return) processed materials. 6 * Other machines (like a music box) produce output (music) without any specific input. 7 * Still other machines have neither inputs nor outputs but affect other things (like pressing the button on your car remote control) 1 Modern programming languages are very small, but they allow you to extend the language by creating new command words. [[BR]] In Java, a new word is called a ''function'' or ''method''. Functions are a powerful way to keep your code readable and easy to maintain.[[BR]] A function is an abstraction: it does something, but you don't have to know how it does it in order to use it.[[BR]] You can think of a function as a machine. All machines have some function (do something): 2 3 * Some machines (like a meat grinder) require input of raw materials and output (return) processed materials. 4 * Other machines (like a music box) produce output (music) without any specific input. 5 * Still other machines have neither inputs nor outputs but affect other things (like pressing the button on your car remote control) 8 6 9 7 Every Java function has 4 primary characteristics: 10 * a name followed by parentheses 11 * optional inputs (called parameters) placed inside the parentheses 12 * a body (the code that defines what the function does) 13 * an optional output (called a return value) 8 9 * a name followed by parentheses 10 * optional inputs (called parameters) placed inside the parentheses 11 * a body (the code that defines what the function does) 12 * an optional output (called a return value) 14 13 15 14 Consider the following function: 15 16 16 {{{ 17 17 int square(int a) { … … 19 19 } 20 20 }}} 21 22 * The function's name is '''square''' (notice that function names are typically verbs because they ''do'' something). 23 * It takes an input parameter that is an integer and is referred to within the function as a. 24 * The function returns an integer that it computes by multiplying a by itself. 21 * The function's name is '''square''' (notice that function names are typically verbs because they ''do'' something). 22 * It takes an input parameter that is an integer and is referred to within the function as a. 23 * The function returns an integer that it computes by multiplying a by itself. 25 24 26 25 You could use the square function as follows: 26 27 27 {{{ 28 28 int a = 9; … … 30 30 }}} 31 31 32 The two pieces of code above can be used to create the following program. 33 34 {{{ 35 public class function { 36 static int square(int a) { 37 return a * a; 38 } 39 public static void main(String args[]) { 40 int a = 9; 41 int b = square(a); 42 System.out.println("b=" + b); 43 } 44 } 45 }}} 46 32 47 Functions can take more than one parameter, but they can only return 1 value (or no values). Consider this function that takes two input parameters and returns no value: 48 33 49 {{{ 34 50 void echo(String msg, int times) { … … 38 54 } 39 55 }}} 56 Create a new !EchoExample project folder and in it create an Echo program as follows: 40 57 41 Create a new !EchoExample project folder and in it create an Echo program as follows:42 58 {{{ 43 59 public class Echo { … … 53 69 } 54 70 }}} 55 56 * Save the program as Echo.java 57 * Run the program and observe the output 58 * For now, we're going to ignore the keywords public and static, we'll get to those soon 71 * Save the program as Echo.java 72 * Run the program and observe the output 73 * For now, we're going to ignore the keywords public and static, we'll get to those soon