Procedures and Functions



Procedures and Functions: Key Ideas





Key Ideas



Procedures and Functions, and Scope





Nesting Procedures and Functions



What's in a Name




Java Methods



Java Methods

Java has 2 kinds of methods. What are they and how are they used?
  1. void - return type is void (ie no return type )
  2.               void printSquare(int i)
                  {
                     Sop(i*i);
                  }
                  ...
                  printSquare(3));
             
  3. non-void - return type is non-void (eg int)
  4.               int square(int i)
                  {
                     return i * i);
                  }
                  ...
                  Sop(square(3));
    
             


Ada Equivalents

  1. Procedure - like void method
  2.          procedure printSquare(i: Integer) is
             begin
                put(i * i);
             end printSquare;
             ...
             printSquare(3);
             
  3. Function - like non-void method
  4.          function square(i: Integer) return Natural is
             begin
                return i * i;
             end square;
             ...
             put(square(3));
             


Statements and Values

  1. Procedure calls are statements, functions calls are values

  2. Procedure call = a statement
  3.          procedure printSquare(i: Integer) is
             begin
                put(i * i);
             end printSquare;
             ...
             j := j + 1;
             printSquare(j);
             do_something_else();
             
  4. Function call = a value
  5.          function square(i: Integer) return Natural is
             begin
                return i * i;
             end square;
             ...
             int j = 5;
    
             put(j);
    
             put(square(j));
    
             put(square(j) + 1);
             


Order of Declarations: Define Before Use



Scope



Local and Global Variables



Global Variables



Java Locals and Globals



Functions and Strong Typing



Function Calls as Arguments



Expression Functions



Terminology Revisited



Parameter Modes





Question: How Does Get Work? Answer: Modes



Parameter Modes - Syntax



Parameters Modes Specify Information Flow



Example with In Mode and Out Mode



Example with In Out Mode Parameters



Modes: Copy In, Copy Out

Conceptually, we can think of the modes as follows: Actual implementation may or may not involve copying.

In Mode Parameters are Constants



Parameter Modes Improve Error Detection



Why Parameter Modes



Parameter Modes and Java



What Modes are Not: Implementation Mechanism



Procedure vs Function





Functions are Values; Procedures are Statements



Java and C Can Ignore Expression Results



Procedures or Functions: Which to use



Recursive Routines





Recursive Routines



Mutually Recursive Routines



Mutually Recursive Routines - Problem solved



Mutually Recursive Routines - Realistic Example



More on Globals





Global Variables - Why Not?

Problems with globals (Yannick Moy, Adacore) David Parnas - Module design: Maximize cohesion, minimize coupling Phillip Koopman, CMU: "Global Variables Are Evil!"

Global Variables - Why Not?



Modes Help Debugging, if no Global Variables



Uninitialized Variables





WARNING: Unitialized Variables