18 - Click to edit Master subtitle style Chapter Eighteen...

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

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: Click to edit Master subtitle style Chapter Eighteen Modern Programming Languages, 2nd ed. Parameters Chapter Eighteen Modern Programming Languages, 2nd ed. 11 Chapter Eighteen Modern Programming Languages, 2nd ed. Parameter Passing How are parameters passed? Looks simple enough We will see seven techniques Chapter Eighteen Modern Programming Languages, 2nd ed. 22 int plus(int a, int b) { return a+b; } int x = plus(1,2); formal parameters method body method call actual parameters Chapter Eighteen Modern Programming Languages, 2nd ed. Outline 18.2 Parameter correspondence Implementation techniques 18.3 By value 18.4 By result 18.5 By value-result 18.6 By reference 18.7 By macro expansion 18.8 By name 18.9 By need 18.10 Specification issues Chapter Eighteen Modern Programming Languages, 2nd ed. 33 Chapter Eighteen Modern Programming Languages, 2nd ed. Parameter Correspondence A preliminary question: how does the language match up parameters? That is, which formal parameters go with which actual parameters? Most common case: positional parameters Correspondence determined by positions n th formal parameter matched with n th actual Chapter Eighteen Modern Programming Languages, 2nd ed. 44 Chapter Eighteen Modern Programming Languages, 2nd ed. Keyword Parameters Correspondence can be determined by matching parameter names Ada: DIVIDE(DIVIDEND => X, DIVISOR => Y); Matches actual parameter X to formal parameter DIVIDEND , and Y to DIVISOR Parameter order is irrelevant here Chapter Eighteen Modern Programming Languages, 2nd ed. 55 Chapter Eighteen Modern Programming Languages, 2nd ed. Mixed Keyword And Positional Most languages that support keyword parameters allow both: Ada, Fortran, Dylan, Python The first parameters in a list can be positional, and the remainder can be keyword parameters Chapter Eighteen Modern Programming Languages, 2nd ed. 66 Chapter Eighteen Modern Programming Languages, 2nd ed. Optional Parameters Optional, with default values: formal parameter list includes default values to be used if the corresponding actual is missing This gives a very short way of writing certain kinds of overloaded function definitions Chapter Eighteen Modern Programming Languages, 2nd ed. 77 Chapter Eighteen Modern Programming Languages, 2nd ed. Example: C++ Chapter Eighteen Modern Programming Languages, 2nd ed. 88 int f(int a=1, int b=2, int c=3) { body } int f() {f(1,2,3);} int f(int a) {f(a,2,3);} int f(int a, int b) {f(a,b,3);} int f(int a, int b, int c) { body } Chapter Eighteen Modern Programming Languages, 2nd ed. Unlimited Parameter Lists Some languages allow actual parameter lists of unbounded length: C, C++, and scripting languages like JavaScript, Python, and Perl Library routines must be used to access the excess actual parameters A hole in static type systems, since the types of the excess parameters cannot be checked at compile time Chapter Eighteen Modern Programming Languages, 2nd ed....
View Full Document

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 50

18 - Click to edit Master subtitle style Chapter Eighteen...

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

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