SlideServe
Menu
Préférences
© SlideServe · v2026

Parameter passing

emanuelj avatar
19 diapositives · 586 Views · Jan 02, 2020

Explore different parameter passing modes such as call by value, reference, result, value-result, and name in languages like Pascal, C, C++, Java, and Ada. Learn how these modes affect program behavior and memory management.

Parameter  passing - slide 1
01 / 19Parameter passing
1 / 17

Télécharger la présentation

Parameter passing
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Parameter passing Module 12.3COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

  2. Parameter passing by Value Reference Result Value-result Name Parameter passing in Pascal, C, C++, Java, Ada Topics

  3. Parameter Passing • Formal parameter (a.k.a parameter): parameter that appears in the declaration of a subroutine. • Actual parameter (a.k.a. argument): expression actually used in procedure call. • Some languages allow only ONE parameter passing mode: • C, Fortran, ML, Lisp, Java. • Other languages allow multiple parameter passing modes: • Pascal, Modula, Ada.

  4. Parameter Passing Modes 1) Call by value: Pass the value of the argument. • C, C++, Pascal, Java, RPAL. 2) Call by reference: Pass the address of the argument. • Smalltalk, Lisp, ML, Clu, C++, Pascal. 3) Call by result: Pass the value, on return only. • Ada. 4) Call by value-result: Pass the value (a.k.a. copy-in, copy-out) • Ada. 5) Call by name: Pass the text of the argument. • C pre-processor (#define), Algol 60.

  5. Choosing Parameter Passing Mode • Many languages provide a choice: • Pass by value: • Intent is to not modify the argument. • Copying large objects can get expensive. • Pass by reference: • Intent is to modify the argument. • Often used to avoid copying a large object, even if there’s no intent to modify it. • In some languages (C, C++) modifying the argument can be explicitly forbidden (const)

  6. Parameter Passing Modes: Example Pass by value: Output is (5,6,5) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; begin y := 5; A(y); write(y); end;

  7. Parameter Passing Modes: Example Pass by reference: Output is (5,2,1) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; begin y := 5; A(y); write(y); end;

  8. Parameter Passing Modes: Example Pass by result: Output is (??,6,1) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; begin y := 5; A(y); write(y); end;

  9. Parameter Passing Modes: Example Pass by value/result: Output is (5,6,1) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; begin y := 5; A(y); write(y); end;

  10. Parameter Passing Modes: Example Pass by name: Output is (5,2,1) (same as pass by reference) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; begin y := 5; A(y); write(y); end;

  11. Pass by name procedure swap(a,b:integer); var t:integer: begin t := a: a := b: b := t; end; ... var n,m:integer; n := 3; m := 4; swap(n,m); Pass by name literally substitutes n for a, and m for b. Result: t := n; n := m; m := t; Swap is achieved.

  12. Pass by name procedure swap(a,b:integer); var t:integer: begin t := a: a := b: b := t; end; ... var i:integer; var A: array[1..10] of integer; i := 3; A[3] := 17; swap(i,A[i]); However, attempting to swap i and A[i] results in t := i; i := A[i]; A[i] := t; 17 assigned to i, but 3 assigned to A[17] (out of range) Swap not achieved.

  13. Parameter Passing in pascal • Use keyword var to effect pass by reference. Without var, it’s pass by value. • With var , output is (5,2,1). • Without var, output is (5,6,5). var y:integer; procedure A(var x:integer); begin write(x); x := 1; write(y+x); end; begin y := 5; A(y); write(y); end;

  14. Parameter Passing in C • All parameters passed by value. • However, can use pointers (which are passed by value): void swap (int *a, int *b) { int t; t = *a; *a = *b; *b = t; } ... swap(&p,&q);

  15. Parameter Passing in C++ • C++ has pass by value and by reference. • Use the & prefix for pass by reference. void swap (int &a, int &b) { int t; t = a; a = b; b = t; } swap (p,q); • Parameters can be declared const.

  16. Parameter Passing in java • “primitives passed by value, non-primitives passed by reference”. NOT true. All parameters passed by value in Java. • A primitive (int, float, etc.) has no reference, so the value is passed. public void swap (int a, int b) { intt; t = a; a = b; b = t; } int p=3; q=4; swap(p,q); // no swap achieved.

  17. Non-primitives are handled by reference, so any reference is passed by value. Not the same as pass by reference. public void tricky(Point a, Point b){ a.x= 100; a.y= 100; Point temp; temp = a; a = b; b = temp; } public static void main(String [] args) { Point x = new Point(0,0); Point y = new Point(0,0); tricky(x,y); } tricky changes a, but swap is not achieved. Parameter Passing in java

  18. Parameter Passing in Ada ADA provides 3 modes: in (value), out (result), in out (value-result). Default mode is in. procedure Add (V:Integer; To:inout Integer; Limited_To:Integer) is begin -- Check that the result won’t be too large. if V + To > Limited_To then To := Limited_To; else To := V + To; end if; end Add;

  19. Parameter passing by Value Reference Result Value-result Name Parameter passing in Pascal, C, C++, Java, Ada summary