Objectives Pass data to methods by value Pass data by reference 1 Function prototype void ShowSum int int int int main int Value1 Value2 Value3 cout Enter 3 integers cin Value1 Value2 Value3 ShowSum Value1 Value2 Value3 return 0 void ShowSum int Num1 int Num2 int Num3 cout Num1 Num2 Num3 endl 2 Changing the Value of a Parameter void ShowSum int int int int main int Value1 Value2 Value3 Value1 3 Value2 5 Value3 9 ShowSum Value1 Value2 Value3 cout Value1 Value2 Value3 return 0 void ShowSum int Num1 int Num2 int Num3 Num1 cout Num1 Num2 Num3 3 Function prototypes void function1 int void function2 char int void function3 double int char int main char ch Q int a 15 double b 5 6 function1 a function2 ch a function3 b a ch cout ch a b 4 int main char ch Q int a 15 double b 5 6 function1 a 15 function2 ch a function3 b a ch a cout ch a b return 0 void function1 int x x x 5 cout x displays 20 5 int main char ch Q int a 15 double b 5 6 function1 a 15 function2 ch a function3 b a ch a cout ch a b return 0 void function1 int x 15 x x 5 x cout x displays 20 6 int main char ch Q int a 15 double b 5 6 function1 a 15 function2 ch a function3 b a ch a cout ch a b return 0 void function1 int x 20 x x 5 x cout x displays 20 7 int main char ch Q int a 15 double b 5 6 function1 a Q 15 function2 ch a function3 b a ch a ch cout ch a b return 0 void function2 char c int b c b cout c b Q 15 c b 8 int main char ch Q int a 15 double b 5 6 function1 a Q 15 function2 ch a function3 b a ch a ch cout ch a b return 0 void function2 char c int b c b cout c b R 16 c b 9 PASSING BY VALUE NEVER CHANGES THE DATA BELONGING TO THE CODE THAT CALLED THE FUNCTION int main char ch Q int a 15 double b 5 6 function1 a function2 ch a function3 b a ch cout ch a b DISPLAYS Q 15 5 6 10 Reference Variables as Parameters void DoubleNum int Value 4 int main int Value 4 cout In main Value is Value DoubleNum Value cout Back in main Value is Value return 0 void DoubleNum int RefVar RefVar 2 11 Reference Variables as Parameters void DoubleNum int Value 4 int main int Value 4 cout In main Value is Value DoubleNum Value cout Back in main Value is Value return 0 void DoubleNum int RefVar RefVar 2 12 Reference Variables as Parameters void DoubleNum int Value 4 int main int Value 4 cout In main Value is Value DoubleNum Value cout Back in main Value is Value return 0 void DoubleNum int RefVar RefVar 2 alias for 13 Reference Variables as Parameters void DoubleNum int Value 8 void main void int Value 4 cout In main Value is Value DoubleNum Value cout Back in main Value is Value return 0 void DoubleNum int RefVar RefVar 2 alias for 14
View Full Document
Unlocking...