L08 - function parameters and scope

L08 - function parameters and scope - Last time - Passing...

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

View Full Document Right Arrow Icon
Last time -- Passing Arguments to Functions • Two mechanisms for passing arguments to functions: pass-by-value and pass-by-reference – pass-by-value • a copy is made of the value of the argument and that copy is given to the called function. • all changes to the called function’s copy are purely local to the called function – they do not get reflected outside the original variable’s value • disadvantages: if the argument is something like an array, this can get really big and making the copy can take time • advantages: keeping the changes local avoids unintended side effects of the function’s execution – pass-by-reference • calling program gives the called function the ability to directly change the data values in the calling program • but the overhead of copying is avoided • can weaken security (the data is not protected) • can lead to code that is harder to debug
Background image of page 1

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

View Full DocumentRight Arrow Icon
You can also use pointers to do pass-by-reference • To tell the compiler that a parameter is passed by reference using pointers, you have to declare the parameter in the function prototype and also in the function header • Then in the function call, use the & operator to send the address of the variable that is the argument • This example illustrates it all --
Background image of page 2
int SquareByValue (int); void SquareByReference (int &); void SquareByPointer (int *); void fcnparms() { int x = 2, y = 3, z = 4; Console::WriteLine(L"Original value of x is {0}", x.ToString()); Console::WriteLine(L"Original value of y is {0}", y.ToString()); Console::WriteLine(L"Original value of z is {0}", z.ToString()); Console::WriteLine(L"Value of x before SquareByValue is {0}", x.ToString()); SquareByValue (x); Console::WriteLine(L"Value of x after SquareByValue is {0}", x.ToString()); Console::WriteLine(L"Value of y before SquareByReference is {0}", y.ToString()); SquareByReference (y); Console::WriteLine(L"Value of y after SquareByReference is {0}",y.ToString()); Console::WriteLine(L"Value of z before SquareByPointer is {0}", z.ToString()); SquareByPointer (&z); Console::WriteLine(L"Value of z after SquareByPointer is {0}", z.ToString()); } int SquareByValue (int a) {return a * a;} void SquareByReference(int &cRef) {cRef *= cRef;} void SquareByPointer (int *cPtr) {*cPtr *= *cPtr;} function prototypes function definitions
Background image of page 3

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

View Full DocumentRight Arrow Icon
Be careful … – In the called function, reference parameters are only mentioned by name. You might (by accident) treat reference parameters just like pass-by-value parameters – this can lead to unintended side effects!! Default arguments: – Often, a program calls a function repeatedly with the same arguments – In these cases, you can specify a default argument and you can specify a value for that default argument Functions can be defined to have “optional” arguments – In this case, if the call gives all of the parameters explicitly, these are used – If one or more parameter values are not specified, then default values are used
Background image of page 4
Constraints for default parameters: Default arguments have to be the rightmost (last) parameters in a function’s parameter list
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

L08 - function parameters and scope - Last time - Passing...

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

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