.Open CS320/10 Subprograms and Chapter 9 .Table .Row Prev 09 Control structures Chapter 8 not section 5 lab09 C/C++ Control .Row 10 Subprograms Chapter 9 not sect'ns 10..12 lab10 C/C++ Functions .Row Next ** Project Phase 1 Resubmit Old UML and proposed changes (10 pts) .Row 11 LISP Chapter 2 section 4 + LISP handout lab11 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: .As_is template 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: .As_is #define zark(x) x=3*(x)+1; and then if you write the call .As_is zark(example) the compiler creates the code .As_is example=3*(example)+1; and that is compiled an run instead. And if you write the following call .As_is zark( i++) then you get this effect .As_is 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: .As_is void add1to(int *i) { (*i)++;} .As_is .As_is .... .As_is .As_is int i=0, j=0; .As_is .As_is add1to(&i); .As_is .As_is add1to(&j); .As_is 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. .Image 10ole.gif Mode and Implementation . 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. .Close . Class Work .See ./10q.html . Lab Work .See ./lab/10.html . Next -- LISP Special Preparation -- Handout -- Review Questions -- Bring your own Question in your head. .See ./11.html