Select this to skip to main content [CSUSB] >> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CSci620] >> 10 [Source]
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Grading]
Thu Apr 29 14:28:32 PDT 2004

Contents


    CS620 Session 10 Subprograms

      Previous

      [ 09.html ]

      Topics

        Run-time Errors

        As a rule it better to use the compiler to clear out the errors. This is very hard and in some languages impossible.

        Many run time errors are about running out of memory or accessing parts of memory that we shouldn't touch. Most of the rest of them are when we pass parameter to a function with a bad value: dividing by zero, square root of a negative number, etc.

        Functions and Procedures

        In Computer science: functions have a value and Procedure do things.

        Activation Records

        In FORTRAN each function and subprogram had one and only one activation record. It was allocated as static storage when the program was loaded. Recursion can not work.

        Modern language maintain a Run-time stack that has all active activation records on it.

        Dynamic pointers indicate where a function or procedure was called.

        IMHO (In my humble opinion) displays are not common. Instead all activation records are put on a stack, BUT each one has an extra static pointer that indicates the activation record of the subprogram where this subporgram was declared.

        Parameter Passing

        Modern terminology is to use 'pass by' not 'call by'.

      . . . . . . . . . ( end of section Topics) <<Contents | Index>>

      Questions

        Call-by-value vs call-by-reference

        In call by value the actual parameter is first evaluated, and then it's value passed is used to initialize a local varaible: the formal parameter. In call be reference the actual parameter must be a variable and its address is passed in. Inside the function/procedure the address must be dereferenced each time the parameter is used.

        C++ notation: call by reference is shown in the header by 'T & x`.

        In Java: primitive types like int and 'double' are passed by value but Objects are passed by reference.

        On page 130..131 what does t:=1*1 etc mean

        This is an attempt to show the way a value is returned to the calling code.

        In reality, programs have a run time stack that is used to handle expressions: values are pushed on to it, operators pop them off. The same stack is also used for activation records, and so when a function returns a value the activation record is popped of the stack and the reurned value left in its place ready to be used in an expression.

        In figure 5.16 page 132, why is AR2 in front of AR1 for p2?

        Because the second AR is pushed on to the stack and so is on top (in front of) the previous one.

        What about memory boundaries and recursion

        An uncontrolled recursion will often fill the memory with ARs. When the stack of ARs fills the available memory the program is forced to halt with an error.

        What is the difference between heap and stack overflow.

        When either overflows we either have to get more space or halt. So either can trigger garbage collection.

        Stack overflow happens because we call to many functions without returning from them. This is typically when we write a bad recursive function.

        Heap overflow occurs when we allocate two much data with deallocating it. This tends to be when we forget to collect garbage.

        Explain Call by value-result

        This means that the value of the formal parameter is the initial value of the formal parameter, and the final value of the formal parameter replaces the old actual parameter's value.

        It has little to reccomend it compare with call-by-reference.

        Do you have a complete list of parameter passing methods


        1. value
        2. constant reference
        3. name
        4. reference
        5. value-result
        6. result
        7. need (lazy evaluation)

        How do you trace a recursive call

        Carefully!

        It is just like any call: create a new AR, fill in the data.....

        How can you change the value of a varible passed by value

        You can;t. All changes are on a local copy.

        What is a functions's signature

        A function's signature is a description of the types of it's parameters + sometimes, type of returned data.

        When should you use call by reference vs call by value.

        Call be value is best used for primitive data types.

        Pass arrays, records, all objects and ponters to objects by reference.

        How do detect a run away pointer?

        You find out about it when a program crashes.

        In a few language each pointer is given a piece of storage over which it is allowed to point. This makes it easy to to test each use of the pointer to see if it is out of range.

        Many modern machines isolate access to memory to special segments. This can also detect a pointer that is out of range with a 'Segment overflow' or 'general protection fault' error.

        It is best to be able to prove that all your pointers are going somewhere sensible.

        It is 4am in the morning: do you know where your poiner is?

        How does a procedural language from an imperative one?

        There is no real difference. Procedural means 'step by step' and imperative means 'using commands'.

        What happens with FORTRAN and COBOL not checking boundaries.

        (1) the ran faster.

        (2) clever programmers invent clever tricks that work and exploited the lack of boundaries.

        (3) stupid programs made life very interesting for themselves and others by creating fun bugs.

        (4) normal programmers exercised care and had no problems.

      . . . . . . . . . ( end of section Questions) <<Contents | Index>>

      Laboratory

      Parameter passing and recursive activation records: [ lab10.html ]

      Next

      [ 11.html ]

    . . . . . . . . . ( end of section CS620 Session 10 Subprograms) <<Contents | Index>>

    Glossary

  1. BNF::="Backus-Naur Form", for syntax and grammar, developed by Backus and Naur.
  2. EBNF::="Extended " BNF.
  3. HTML::= "HyperText Markup Language", used on the WWW.
  4. HTML_page::syntax= "<HTML>" head body.
  5. Java::="An " OO " Language from Sun".
  6. LISP::= "LISt Processing Language".
  7. LRM::="Language Reference Manual".
  8. OO::="Object-Oriented".
  9. Prolog::="Programming in Logic".
  10. TBA::="To Be Announced".
  11. UML::="Unified Modeling Language".
  12. URL::=Universal_Resource_Locator,
  13. Universal_Resource_Locator::syntax= protocol ":" location, where
    Net
    1. protocol::= "http" | "ftp" | "mailto" | ... ,
    2. location::= O( "//" host) O(pathname).

    (End of Net)
  14. WWW::= See http://www.csci.csusb.edu/dick/cs620/, index to web site for this class.
  15. XBNF::="eXtreme" BNF, developed by the teacher from EBNF, designed to ASCII input of syntax, semantics, and other formal specifications.


Formulae and Definitions in Alphabetical Order