. CS320 Notes on Subprograms http://www.csci.csusb.edu/dick/cs320/sebesta/09.html http://www.csci.csusb.edu/dick/cs320/sebesta/09 . Contents 9.1 Introduction 9.2 Fundamentals 9.3 Design Issues 9.4 Local Referencing Environments 9.5 Parameter Parsing: theory and practice 9.6 Subprograms as Parameters 9.7 Overloading 9.8 Generic Subprograms 9.9 Separate vs Independent 9.10 Functions 9.11 Nonlocal Environments 9.12 User defined overloaded operators 9.13 Coroutines Exercises Problems . 9.1 Introduction. Humans can design complicated things that work because we use a process of abstraction This means abstracting the important features, working with these, and then take each feature and work out the details. Abstraction means replacing a lot of detail by a more or less imaginary single item. Pretending the Many is actually One. One kind of abstraction is procedural abstraction One command (call) mapped into many commands (body). Two ways of getting desired effect: (1) Open subroutine or Macro (2) Closed subroutine, procedure,... Procedure: no value returned/void Function: Returns a value. Open subroutine or Macro During or before compilation replaces subprogram call by its expansion. Closed subroutine, procedure, function,... One copy is compiled. Each subprogram call transfers control to start of code and this code returns to the instruction after call. . 9.2 Fundamentals - Learn the definitions. Distinguish:- CALL: A call of the subprogram NAME: The name of the subprogram/function DEFINITION: The Definition of the subprogram Says precisely how it does it, in detail as well as how to use it. DESCRIPTION: A description of how to call it. Says how to use it. Parameter types Procedure or function If function: what is returned (C/C++/Java Prototype) parameters = arguments data that is given by the caller to the body. Procedures are NOT functions! A procedure in C is a function that returns no useful value. The book uses the word 'protocol' for a description of how a subprogram is to be used: Parameter types Procedure or function If function: what is returned In C/C++ this is called a 'prototype' (Easily confused with a prototype version of a program.) Theorists call it a 'signature'. . 9.3 Issues - learn This sets the agenda for the rest of the chapter. . 9.4 Local Environment The C code on page 332 has the effect of accumulating all the successive arrays that it adds up. What do you change so that it doesn't do this but gives the sum of each array alone. (easy in C and C++) How do you modify it so that: adder(array, length) accumulates more and clear() resets the running sum to zero.(tricky in C, easy in C++) How do you further make it impossible for a user to accidentally destroy the current value of sum by assigning a value other than zero to it? (Tough in C, Easy in C++) . 9.5 Parameter passing Why parameters? BASIC and COBOL do without. Semantics: Learn! in out inout Implementation models: Learn! value result value-result reference Name -> now reborn as late binding. Examples FORTRAN ALGOL 60, 68 Pascal Ada C Note a dirty secret about C macros. They pass parameters by name! Try the following macro in a program based on pages 315..316. #define SIGMA(s,a,i,n) for(i=1,s=0.0;i<=n;s+=a,i++) Here is an example of the code: http://www.csci.csusb.edu/dick/cs320/sebesta/09.name.cc main() { int i; float sum; SIGMA(sum, i, i, 10); cout << sum <