Subroutines

Subroutines - COP4020 Programming Languages Subroutines and...

Info iconThis preview shows pages 1–8. 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

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: COP4020 Programming Languages Subroutines and Parameter Passing Prof. Robert van Engelen COP4020 Spring 2011 2 4/7/11 Overview ! Parameter passing modes ! Subroutine closures as parameters ! Special-purpose parameters ! Function returns COP4020 Spring 2011 3 4/7/11 Parameters ! First some definitions ! Formal parameters " Lisp: (lambda ( a b ) (/ (+ a b ))) " C function: float ave(float a , float b ) { return ( a + b )/2.0; } ! Versus actual parameters " Lisp function arguments: (ave x 10.0 ) " C function arguments: ave( x , 10.0 ) ! Versus operands (of special forms) " Lisp special forms: (if flag yes no ) " C operators: x > 0 && flag " Operand handling often depends on the type of built-in operator, e.g. special forms and operators with short-circuit evaluation COP4020 Spring 2011 4 4/7/11 Parameter Passing ! Parameter passing modes " In " In/out " Out ! Parameter passing mechanisms " Call by value (in) " Call by reference (in+out) " Call by result (out) " Call by value/result (in+out) " Call by name (in+out) ! Different mechanisms used by C, Fortran, Pascal, C++, COP4020 Spring 2011 4 4/7/11 Parameter Passing ! Parameter passing modes " In " In/out " Out ! Parameter passing mechanisms " Call by value (in) " Call by reference (in+out) " Call by result (out) " Call by value/result (in+out) " Call by name (in+out) ! Different mechanisms used by C, Fortran, Pascal, C++, Java, Ada (and Algol 60) COP4020 Spring 2011 5 4/7/11 Parameter Passing in C ! Call by value parameter passing only ! Actual parameter is evaluated and its value assigned to the formal parameter ! A formal parameter in the function body behaves as a local variable " For example: int fac(int n) { if (n < 0) n = 0; return n ? n*fac(n-1) : 1; } ! Passing pointers allows the values of actuals to be modified " For example: swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } " A function call should explicitly pass pointers, e.g. swap(&x, &y); ! Arrays and pointers are exchangeable in C " An array is automatically passed as a pointer to the array COP4020 Spring 2011 6 4/7/11 Parameter Passing in Fortran ! Call by reference parameter passing only ! If the actual parameter is an l-value (e.g. a variable) its reference is passed to the subroutine ! If the actual parameter is an r-value (e.g. the value of an expression) it is assigned to a hidden temporary variable whose reference is passed to the subroutine " For example SUBROUTINE SHIFT(A, B, C) INTEGER A, B, C A = B B = C END " For example, SHIFT(X, Y, 0) assigns Y to X , and Y is set to 0 " For example, SHIFT(X, 2, 3) assigns 2 to X and the assignment to B in the subroutine has no effect, but in Fortran IV this was not handled correctly! COP4020 Spring 2011 7 4/7/11 Parameter Passing in Pascal ! Call by value and call by reference parameter passing ! Call by value is similar to C ! Call by reference: indicated with var qualifier in argument list ! For example procedure swap(var a:integer, var b:integer) var t; begin t := a;...
View Full Document

Page1 / 25

Subroutines - COP4020 Programming Languages Subroutines and...

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

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