[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] /[CS320 Course Materials] /10.html [10.txt(Text)] [Search ]
Mon Apr 29 14:54:57 PDT 2013
[Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Grading] [Contact]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]

Contents


    CS320/10 Subprograms and Chapter 9


      Table
      Prev 09Control structuresChapter 8 not section 5lab09 C/C++ Control
      10SubprogramsChapter 9 not sect'ns 10..12 lab10 C/C++ Functions
      Next** Project Phase 1 ResubmitOld UML and proposed changes (10 pts)
      11LISPChapter 2 section 4 + LISP handoutlab11 LISP101

      (Close Table)

      Preparation

      Think about the notes (below), diagrams, and chapter 9.

      Answer review questions on this chapter.

      Hand in answers to 3 or more review questions;

      Notes and FAQ on Chapter 9 Subprograms

      What is parametric polymorphism?

      Polymorphism is when an operation or subprogram automatically adjusts its behavior to parameter's data types. In C++ a template creates a function that can work with any kind of data. The compiler generates special purpose functions, as needed, for each function call. For example the following function will work out the largest of two ints, two chars, two floats, two longs, or two of any type of data with a defined ">=" operator:
          template <typename T> T max( T x, T y ) { return x>=y ? x : y ; }

      How does pass-by-name really work?

      Call by name is a common result of using a macro. The compiler makes a copy of the subprogram body and writes in the actual parameters in place of the formal parameters. The resulting code is then executed in place of the call. For example, in C++ define:

                     #define zark(x)    x=3*(x)+1;
      and then if you write the call
                                   zark(example)
      the compiler creates the code
                                   example=3*(example)+1;
      and that is compiled an run instead. And if you write the following call
                                   zark( i++)
      then you get this effect
                                   i++ = 3*(i++) +1;
      In Algol 60 compilers pass by name was handled by creating a special temporary subprogram for each such parameter. It was called a "thunk"! Each reference to the formal parameter was compiled as a call to the thunk. The thunk could evaluate the actual expression or return a left-hand value.

      Parameter Passing in C and C++

      All C and C++ macros ( #define ) pass parameters by name -- more in next session and lab work!

      In K&R C all function arguments were passed by value. This is an in mode. However an address (a reference to an object ) is a valid value (shown like this: &variable_name) and so could be passed to a function. A function that was passed an address of an object of type T had an argument declared like this T*argument_name. In the body of the function this could be de-referenced (by using this syntax *argument_name ) to get to a T. So by explicit referencing (&) and de-referencing(*) we get pass by reference (an in-out mode). For example:

            void add1to(int *i) { (*i)++;}
      
      
            ....
      
      
            int i=0, j=0;
      
      
            add1to(&i);
      
      
            add1to(&j);
      
      This mechanism proved to be unsafe when normal programmers used them. If a
      program missed out the '&' in a call then the program would compile and
      run. The program would then mysteriously work, fail, or crash until the
      missing & was diagnosed. Similarly a missing or extra '*' in a function
      body would cause mysterious bugs and crashes. In C++ we have call by
      value(in), call by reference(in-out), call by constant reference(in). The
      old C tricky &* is still there but hidden! The C++ is more reliable than
      human hand coded pass by reference.
      

      Semantic modes and Implementations

      Be careful to relate the semantic modes and the parameter passing methods correctly. The methods implement the modes. The modes say what the effect is (= what we want) and the methods say how the computer gives the effect.

      Mode and Implementation

      Object-oriented languages

      Object-oriented langauges tend to use a form of Pass-by-Reference. Thus a sub-program can change the state of the actual parameter by using a member-function. This also makes the parameter polymorphic. The book calls this pass-by-assignment. But we will stick to pass by reference. Example -- Python [ 4b8b0e06a2d5cfcc?hl=en ]

      Call by Value+Coercion is not Object-Oriented!

      In C++ arguments are normally passed by value, and the type of the argument is coerced to the type of the formal argument. So, if a function is declared like this: func(....., B b, ....) but called like this func(...., d, ....) and where d is an object of type D that is a class derived from B then it compiles and runs BUT all the extra info in d is stripped out to give a B. It is therefore wise, in C++ to use PassByConstReference for objects rather than PassByValue.

    Class Work

    [ 10q.html ]

    Lab Work

    [ lab/10.html ]

    Next -- LISP

    Special Preparation -- Handout -- Review Questions -- Bring your own Question in your head. [ 11.html ]

( End of document ) <<Contents | Top