Changes between Version 4 and Version 5 of ControlSystems/SoftwareTeam/Training/GettingStarted/Methods


Ignore:
Timestamp:
Aug 3, 2020, 5:49:07 PM (5 years ago)
Author:
Angelina Zhou
Comment:

added full program for first function example

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)
     1Modern 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)
    86
    97Every 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)
    1413
    1514Consider the following function:
     15
    1616{{{
    1717   int square(int a) {
     
    1919   }
    2020}}}
    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.
    2524
    2625You could use the square function as follows:
     26
    2727{{{
    2828   int a = 9;
     
    3030}}}
    3131
     32The two pieces of code above can be used to create the following program.
     33
     34{{{
     35public 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
    3247Functions 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
    3349{{{
    3450   void echo(String msg, int times) {
     
    3854   }
    3955}}}
     56Create a new !EchoExample project folder and in it create an Echo program as follows:
    4057
    41 Create a new !EchoExample project folder and in it create an Echo program as follows:
    4258{{{
    4359public class Echo {
     
    5369}
    5470}}}
    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