Docsity

Class 16: Function Parameters and Polymorphism, Slides of Programming Languages

Partial preview of the text

Download Class 16: Function Parameters and Polymorphism and more Slides Programming Languages in PDF only on Docsity!

Class 16: Function Parameters and Polymorphism

SI 413 - Programming Languages and Implementation

Dr. Daniel S. Roche

United States Naval Academy

Fall 2011

Parameter Passing Modes

Our programs are littered with function calls like f (x, 5).

This is a way of passing information from the call site (where the code f(x,5) appears) to the function itself.

The parameter passing mode tells us how the information about the arguments (e.g. x and 5) is communicated from the call site to the function.

Pass by Value

C/C++ use pass by value by default. Java uses it for primitive types (int, boolean, etc.). v o i d f ( i n t a ) { a = 2* a ; print ( a ); }

i n t main () { i n t x = 5; f ( x ); print ( x ); r e t u r n 0; } What does this C++ program print? 10 , then 5

Pass by Value

In this scheme, the function recieves copies of the actual parameters.

The function cannot modify the originals, only its copies, which are destroyed when the function returns.

Function arguments represent one-way communication from call site to function. (How can the function communicate back?)

Pass by Reference

C++ supports reference parameters. Perl and VB use this mode by default. sub foo { $_ [0] = โ€ h a h a c h a n g e d by f o o โ€ ; }

my $y = โ€ t h i s i s m i n e! โ€ ; foo ( $y ); p r i n t $y , โ€ \ n โ€ ;

You can guess what this Perl program prints...

Similar behavior happens if we wrote void f(int &a) {...} in the previous C++ example.

Pass by Reference

The formal parameters of the function become aliases for the actual parameters! Normally the actual parameters must be variable names (or more generally, l-values... ).

Function arguments now represent two-way communication.

References provide a way for functions to return multiple things.

They also avoid the (potentially expensive) overhead of copying without having to deal with pointers. This is especially useful with (large) data structures stored in objects.

Pass by Value/Result

This is the default in Fortran, and for โ€œin outโ€ parameters in Ada: --in file f.adb p r o c e d u r e f ( x : i n out Integer ) i s b e g i n x := 10; end f ;

--in file test.adb p r o c e d u r e test i s y : Integer := 5; b e g i n f ( y ); Ada. Integer_Text_IO. Put ( y ); end test ;

Calling test prints 10, not 5!

Pass by Sharing

This is the default in languages like Java, for non-primitive types: c l a s s Share { s t a t i c c l a s s Small { p u b l i c i n t x ; p u b l i c Small ( i n t thex ) { x = thex ; } }

p u b l i c s t a t i c v o i d test ( Small s ) { s. x = 10; s = new Small (20); }

p u b l i c s t a t i c v o i d main ( String [] args ) { Small mainsmall = new Small (5); test ( mainsmall ); System. out. println ( mainsmall. x ); } }

Does this program print 5, 10, or 20?

Parameter Passing in Functional Languages

Why havenโ€™t we talked about parameter passing in Haskell, Scheme, etc.?

Parameter Passing in Functional Languages

Why havenโ€™t we talked about parameter passing in Haskell, Scheme, etc.?

In pure functional programming, it makes no difference! Scheme actually uses pass by sharing.

Overloading

Function overloading is when the same name refers to many functions. Which function to call is determined by the types of the arguments.

Overloading

Function overloading is when the same name refers to many functions. Which function to call is determined by the types of the arguments. c l a s s A { v o i d print () { cout << โ€ i n Aโ€ << endl ; } }; c l a s s B { v o i d print () { cout << โ€ i n Bโ€ << endl ; } };

v o i d foo ( i n t a ) { cout << a << โ€ i s an i n t \ n โ€ ; } v o i d foo ( d o u b l e a ) { cout << a << โ€ i s a d o u b l e \ n โ€ ; }

i n t main () { cout << (5 << 3) << endl ; A x ; B y ; x. print (); y. print (); foo (5); foo (5.0); }

How does overloading occur in this C++ example?

Polymorphism example in C++

c l a s s Base { v i r t u a l v o i d aha () = 0; };

c l a s s A : p u b l i c Base { v o i d aha () { cout << โ€ I โ€™m an A! โ€ << endl ; } };

c l a s s B : p u b l i c Base { v o i d aha () { cout << โ€ I โ€™m a B! โ€ << endl ; } }

i n t main ( i n t argc ) { Base * x ; i f ( argc == 1 ) // canโ€™t know this at compile-time! x = new A ; e l s e x = new B ; x. aha (); // Which one will it call? }

Class outcomes

You should know: The differences between the four parameter passing modes we have discussed How class method calls are typically interpreted by compilers How overloading works with operators, classes, and functions What subtype polymorphism is and its benefits and drawbacks

You should be able to: Follow and create programming examples demonstrating pass by value vs. pass by reference