Changes between Version 23 and Version 24 of ControlSystems/SoftwareTeam/Training/GettingStarted/IntroJava


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

--

Legend:

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

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