Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.

Similar presentations


Presentation on theme: "Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions."— Presentation transcript:

1 Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions

2 #include using namespace std; // function prototypes void swap(int, int); int main() { // Declare and initialize variables int num1 = 1; int num2 = 2; // Invoke the swap function to attempt // to swap two variables swap(num1, num2); cout << "After invoking swap” << “ num1 is " << num1 << " and num2 is " << num2 << endl; return 0; } void swap(int n1, int n2) { // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; }

3 Primitive variables are pass-by-value. This means that only the value in the symbol table for that variable is passed to the function. In order to actually change the value of a primitive in a function you must use pass –by-reference. This means that the reference to the variable is passed to the function. The reference is the variable’s actual address in the symbol table. This is done by putting an ampersand before the name of the variable name in the function header.

4 #include using namespace std; // function prototypes void swap(int&, int&); int main() { // Declare and initialize variables int num1 = 1; int num2 = 2; // Invoke the swap function to attempt to swap two variables swap(num1, num2); cout << "After invoking swap” << “ num1 is " << num1 << " and num2 is " << num2 << endl; return 0; } void swap(int &n1, int &n2) { // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; }

5 Functions that allow changes to happen to their parameters are said to have side effects. We need to make sure that the side effects are always intentional and not by mistake. With primitive data types side effects are pretty much always intentional, we have to use pass by reference on purpose. Objects (as we will find out later) are pass by reference by default and we must use the keyword const to prevent unintended side effects.

6 Functional Abstraction and Stepwise Refinement Functional Abstraction is separating the use of a function from its implementation. A client can use a function without know the details of the implementation. Those details are hidden in the implementation … encapsulated within the function.

7 Top-Down Design Top down design is a process used to design a large program by breaking it down into functional bits and writing the details after you have the overall design done.

8 Function overloading We can define multiple functions with the same name but different signatures. The correct function is executed based on the parameters passed to it. int sum (int, int); double sum (double, double); void sum(int[], int[], int[]); This is very useful for building libraries of functions for special purposes.

9 Review Questions 6.1, 6.5, 6.7


Download ppt "Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions."

Similar presentations


Ads by Google