






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







th
rd
Two major types:– Procedures: No return values– Functions: Have return values– Observation: No procedures in C – everything is a function
-^
Design issues:– Already addressed issues related to local variable
allocation, scope, etc.
Giving a parameter a default value:– int foo(int x = 0);– foo() means that x is initialized to 0.– Parameters are resolved positionally in this case.
-^
What about:– int foo(int x, int y = 0, char c = ‘ ‘);– foo(1,2,’c’); foo(1,2); foo(1) all ok.– foo(1,’c’) is a problem.
-^
What about the following:– int foo(int x, int y = 0, char c)– This won’t compile – makes no sense if resolved positionally
-^
L to R matching means that there must be 3 parameters regardless tomatch with the right one in this case
-^
Keyword parameters would be better in the context of defaultparameters (but not available in C++)
value
reflected in the caller
reflected in the caller
Copy:– Can be expensive for large structures (e.g., arrays)– Best fit with IN semantics
-^
Reference:– Cheap in most cases– Best fit with IN OUT semantics
-^
What if you want IN semantics but without theexpense?– Implement compiler checking scheme to prevent changes
to parameters
Copy vs. Reference Gives Different Results on IN OUTParameters int x;void foo(IN-OUT int y){
x++;y++; } void main(void){
x = 10;foo(x);cout << x << endl; } •^
If IN-OUT implemented by copy, then output is 11.
-^
If IN-OUT implemented by reference, then output is 12.
their parameters
other parameters
Reusing the same function name for multiplefunctions
-^
Functions must have different signatures (i.e.,parameter lists)
-^
Example:– foo(int a, int b, int c)– foo(float x, float y)
-^
Functions disambiguated based on parameter lists