ch9Mar12 - 9.2.3 Parameters Positional binding of actual to...

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

View Full Document Right Arrow Icon
1 9.2.3 Parameters Positional binding of actual to formal parameter is by position e.g. here “a” binds to “i”, “b” to “j”, etc. // call fun(a, b, c, d); // prototype void fun(int i, int j, int k, int l);
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 9.2.3 Parameters Keyword binding of actual to formal parameter is by using the formal parameter name e.g. here “a” binds to “l”, “b” to “k”, etc. // call fun(l=a, k=b, j=c, i=d); // prototype void fun(int i, int j, int k, int l);
Background image of page 2
3 Keyword Parameters Advantages The order of the parameters can be arbitrary. Useful if you have a long list of parameters. Disadvantages You have to know the formal parameter names. You have to know special rules for their use. e.g. In Python, after a keyword parameter appears, the rest must also be keyworded.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Default parameters In Python, Ruby, C++, Fortran 95, Ada, and PHP, formal parameters can have default values. A default value is used when no actual parameter is passed to the formal parameter. e.g. In Python, defaults are specified with = def compute_pay(income, exemptions = 0, tax_rate = 0.20): Default parameters must appear last.
Background image of page 4
5 def compute_pay(income, exemptions = 0, tax_rate = 0.20): if exemptions == 1: tax_rate = tax_rate / 2 tax = income * tax_rate print income - tax compute_pay(10000, tax_rate = 0.40) compute_pay(10000, 0.20) compute_pay(10000, 1, 0.1) compute_pay(10000, 1) Default and keyword parameters Example in Python: What is the output?
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 def compute_pay(income, exemptions = 0, tax_rate = 0.20): if exemptions == 1: tax_rate = tax_rate / 2 tax = income * tax_rate print income - tax compute_pay(10000, tax_rate = 0.40) compute_pay(10000, 0.20) compute_pay(10000, 1, 0.1) compute_pay(10000, 1) Default and keyword parameters Example in Python: =8000 =9000 =6000 =9500
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/29/2010 for the course COMPUTER S cps615 taught by Professor Pro during the Spring '10 term at Randolph College.

Page1 / 22

ch9Mar12 - 9.2.3 Parameters Positional binding of actual to...

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

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