11-subprograms - 1 CSE 452 Programming Languages...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSE 452: Programming Languages Subprograms 2 Organization of Programming Languages-Cheng Outline ? Subprograms ? Parameter passing ? Type checking ? Using multidimensional arrays as parameters ? Using subprograms as parameters ? Overloaded subprograms ? Generic subprograms ? Implementation 2 3 Organization of Programming Languages-Cheng Parameter Passing ? Pass-by-value ? Pass-by-result ? Pass-by-value-result ? Pass-by-reference ? Pass-by-name 4 Organization of Programming Languages-Cheng Parameter Passing in PL ? Fortran ? Always use inout-mode model of parameter passing ? Before Fortran 77, mostly used pass-by-reference ? Later implementations mostly use pass-by-value-result ? C ? mostly pass by value ? Pass-by-reference is achieved using pointers as parameters int *p = { 1, 2, 3 }; void change( int *q) { q[0] = 4; } main() { change(p); /* p[0] = 4 after calling the change function */ } 3 5 Organization of Programming Languages-Cheng Parameter Passing in PL ? C ? Pass-by reference: value of pointer is copied to the called function and nothing is copied back #include <stdio.h> void swap (int *p, int *q) { int *temp; temp = p; p = q; q = temp; } main() { int p = {1, 2, 3}; int q = {4, 5, 6}; int i; swap (p, q); } 6 Organization of Programming Languages-Cheng Parameter Passing in PL ? C++ ? includes a special pointer type called a reference type void GetData(double &Num1, const int &Num2) { int temp; for (int i=0; i<Num2; i++) { cout << “Enter a number: “; cin >> temp; if (temp > Num1) { Num1 = temp; return; } } ? Num1 and Num2 are passed by reference ? const modifier prevents a function from changing the values of reference parameters ? Referenced parameters are implicitly dereferenced ? Why do we need a constant reference parameter? 4 7 Organization of Programming Languages-Cheng Parameter Passing in PL ? Ada ? Reserved words: in, out, in out (in is the default mode) procedure temp(A : in out Integer; B : in Integer; C : in Integer ) ? out mode can be assigned but not referenced ? in mode can be referenced but not assigned ? in out can be both referenced and assigned ? Fortran ? Semantic modes are declared using Intent attribute Subroutine temp(A, B, C) Integer, Intent(Inout) :: A Integer, Intent(In) :: B Integer, Intent(Out) :: C 8 Organization of Programming Languages-Cheng Parameter Passing in PL ? Perl ? Actual parameters are implicitly placed in a predefined array named @_ sub foo { local $i, $a=0, $b = 1; for ($i=0; $i<scalar( @_ ); $i++) { $a = $a + $_[$i]; $b = $b * $_[$i]; } return ($a, $b); } … ($a, $b) = foo(1, 2, 3); 5 10 Organization of Programming Languages-Cheng Implementing Parameter Passing Code Data Heap Stack Memory contents program code global and static data Dynamically allocated variables local data 11 Organization of Programming Languages-Cheng Implementing Parameter Passing ? Pass by Value ? Values copied into stack locations ? Stack locations serve as storage for corresponding formal parameters ? Pass by Result...
View Full Document

This note was uploaded on 07/25/2008 for the course CSE 452 taught by Professor Cheng during the Fall '05 term at Michigan State University.

Page1 / 26

11-subprograms - 1 CSE 452 Programming Languages...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online