Procedures and Functions





Procedures and Functions: Overview





Procedures and Scope





Terminology: Procedures, functions, methods, routines



Declaring and Calling a Procedure



Procedure with Parameters



Procedure with a Local Variable



Order of Declarations, Scope, Global Variables



Scope



Global Declarations and Variables



WARNING: Unitialized Variables



Deep Nesting?



Functions





Example Functions



Functions and Strong Typing

      function half_it (n : Natural) return Float is
      begin
         return n / 2.0;
      end half_it;

      half: Integer;
   begin
      half := half_it(10);  -- Compile error: Expected Int found Float 


Function Calls as Arguments



Expression Functions



An Example with Procedures and Functions



Parameter Modes





Parameters and Information Flow



Parameter Modes - In and Out Modes



Example with In Mode and Out Mode



Example with In Out Mode Parameters



Why Parameter Modes



Modes: Initialization, Constants, Modification



Parameter Modes Improve Error Detection



Parameter Mode vs Parameter Passing Mechanism



Modes and Swap in Ada and Java





Using Modes: Swap Two Ints in Java



Procedure Swap in Ada



Swap Two Arrays in Java



Swapping Arrays in Ada



Procedure vs Function





Functions are Values; Procedures are Statements



Procedures or Functions: Which to use



Keyword, Positional, and Default Parameters





Motivation



Keyword Parameters



Default Parameters



Recursive Routines





Recursive Routines



Mutually Recursive Routines



Mutually Recursive Routines - Problem Solved



Java and C Can Ignore Expression Results





Java and C Can Ignore Expression Results



Side Effects



What is a Statement?



Order of Declaration and Global Variables





Order of Declarations (repeated)



Scope (repeated)



Global Variables



Global Variables and Java



Why Avoid Global Variables



Side Effects



Functions and Parameter Modes





Functions and Out Mode



Function Parameter Modes, and Parameter Modes



Procedures and Design Goals





Parameter Modes - Context



Structure Charts





Structure Charts



Looking Ahead





Looking Ahead: Arrays as Parameters