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


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

--

Legend:

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

    v1 v2  
    1 Modern 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. 
     1Modern programming languages are very small, but they allow you to extend the language by creating new command words. [[BR]]
     2In 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]]
     3A function is an abstraction: it does something, but you don't have to know how it does it in order to use it.[[BR]]
     4You 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)
    28
    39Every Java function has 4 primary characteristics:
     
    612* a body (the code that defines what the function does)
    713* an optional output (called a return value)
    8 Consider the following example of a function:
     14
     15Consider the following function:
    916{{{
    10    int squared(int a) {
     17   int square(int a) {
    1118       return a * a;
    1219   }
    1320}}}
    14 The 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.
     21The function's name is 'square' 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.
    1522
    1623Functions 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:
    1724{{{
    18    void sayit(String msg, int times) {
     25   void echo(String msg, int times) {
    1926        for (int n=0; n<times; n++) {
    2027            System.out.println(msg);
     
    2330}}}
    2431
    25 Open your Hello World project folder and modify the Hello.java program as follows:
     32Create a new EchoExample project folder and in it create an Echo program as follows:
    2633{{{
    27 public class Hello {
     34public class Echo {
    2835
    29     static void sayit(String msg, int times) {
     36    static void echo(String msg, int times) {
    3037        for (int n=0; n<times; n++) {
    3138            System.out.println(msg);
     
    3340   }
    3441    public static void main(String args[]) {
    35         System.out.println("Hello world");
    36         sayit("howdy", 10);
     42        echo("howdy", 10);
    3743    }
    3844}