Changes between Initial Version and Version 1 of ControlSystems/SoftwareTeam/Training/GettingStarted/Methods


Ignore:
Timestamp:
Nov 4, 2019, 3:40:31 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/Methods

    v1 v1  
     1Modern programming languages are very small, but they allow you to extend the language by creating new command words.  In Java, a new word is called a function or method.  Functions are like machines that have an input and an output; you put something(s) into the function execute it, and the function does something that might include providing an output. 
     2
     3Every Java function has 4 primary characteristics:
     4* a name followed by parentheses
     5* optional inputs (called parameters) placed inside the parentheses
     6* a body (the code that defines what the function does)
     7* an optional output (called a return value)
     8Consider the following example of a function:
     9{{{
     10   int squared(int a) {
     11       return a * a;
     12   }
     13}}}
     14The function's name is 'squared' and it takes an input parameter that is an integer and is referred to within the function as a. The function returns an integer that it computes by multiplying a by itself.
     15
     16Functions 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:
     17{{{
     18   void sayit(String msg, int times) {
     19        for (int n=0; n<times; n++) {
     20            System.out.println(msg);
     21        }
     22   }
     23}}}
     24
     25Open your Hello World project folder and modify the Hello.java program as follows:
     26{{{
     27public class Hello {
     28
     29    static void sayit(String msg, int times) {
     30        for (int n=0; n<times; n++) {
     31            System.out.println(msg);
     32        }
     33   }
     34    public static void main(String args[]) {
     35        System.out.println("Hello world");
     36        sayit("howdy", 10);
     37    }
     38}
     39}}}
     40
     41* Run the program and observe the output
     42* For now, we're going to ignore the keywords public and static, we'll get to those soon