1 / 55

CIS162AB - C++

CIS162AB - C++. Call-By-Reference Juan Marquez 05_call_by_ref.ppt. Overview Of Topics. Review of call-by-value Functions Introduce Void Functions Introduce Pointers Introduce call-by-reference Functions Preconditions and Postconditions. Review of Call-By-Value (P05).

nat
Download Presentation

CIS162AB - C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS162AB - C++ Call-By-Reference Juan Marquez 05_call_by_ref.ppt

  2. Overview Of Topics • Review of call-by-value Functions • Introduce Void Functions • Introduce Pointers • Introduce call-by-reference Functions • Preconditions and Postconditions

  3. Review of Call-By-Value (P05) • calcTax is a generic function that will be used to calculate fica, federal, and state tax. • Main needs to keep track of the individual rates and tax amounts, so it needs individual variables for each rate and tax amount. • calcTax only needs gross and some taxRate to calculate a tax amount, so generic variable names are used.

  4. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >> hours >> rate; gross = calcGross(hours, rate); fica = calcTax(gross, FICA_RATE); federal = calcTax(gross, FEDERAL_RATE); state = calcTax(gross, STATE_RATE); }

  5. double calcTax(double gross, double taxRate) { double tax; tax = gross * taxRate; return tax; }

  6. main() calcTax()

  7. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >> hours>> rate; gross = calcGross(hours, rate); fica = calcTax(gross, FICA_RATE); federal = calcTax(gross, FEDERAL_RATE); state = calcTax(gross, STATE_RATE); }

  8. main() Values after cin and calcGross() calcTax()

  9. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >> hours>> rate; gross = calcGross(hours, rate); fica = calcTax(gross, FICA_RATE); federal = calcTax(gross, FEDERAL_RATE); state = calcTax(gross, STATE_RATE); } double calcTax(double gross, double taxRate)

  10. main() call with FICA_RATE calcTax()

  11. double calcTax(double gross, double taxRate) { double tax; tax = gross * taxRate; // 24 = 400 * .06 return tax; }

  12. main() Values after fica calculation calcTax()

  13. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >>hours>> rate; gross = calcGross(hours, rate); fica = calcTax(gross, FICA_RATE); federal = calcTax(gross, FEDERAL_RATE); state = calcTax(gross, STATE_RATE); } double calcTax(double gross, double taxRate)

  14. main() Call with FED_RATE calcTax()

  15. double calcTax(double gross, double taxRate) { double tax; tax = gross * taxRate; // 60 = 400 * .15 return tax; }

  16. main() Values after federal calculation calcTax()

  17. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >> hours>> rate; gross = calcGross(hours, rate); fica = calcTax(gross, FICA_RATE); federal = calcTax(gross, FEDERAL_RATE); state = calcTax(gross, STATE_RATE); } double calcTax(double gross, double taxRate)

  18. main() Call with ST_RATE calcTax()

  19. double calcTax(double gross, double taxRate) { double tax; tax = gross * taxRate; // 20 = 400 * .05 return tax; }

  20. main() Values after state calculation calcTax()

  21. Void Functions • No value returned through return statement. • Return statement is optional. • Defined the same way as call-by-value, but return type is void. • If a function doesn’t require a parameter, the parameter list may be left empty, or an entry of void may be made.

  22. Void as Return Value and Parameter void displayHeadings( ) { cout << “Rate \t Hours \t Gross \n\n”; return; } - OR - void displayHeadings(void) { cout << “Rate \t Hours \t Gross \n\n”; return; }

  23. Void as Return Value void displayDetails(double rate, int hours, double gross) { cout << rate << hours << gross << endl; return; }

  24. Void Function Calls • Functions that return a value need variable and equal sign on the function call.gross = calcGross (rate, hours); • Void functions do NOT return value, so there isn’t a variable or equal sign in front of the function call.displayHeadings( );displayDetails(rate, hours, gross);

  25. Void Arguments • Functions that do not have any parameters may be defined with an empty list or keyword void.void displayHeadings( );void displayHeadings(void); • However, in the function call, do not enter the key word void, or a syntax error will be generated. The following would be the function call for either prototype listed above.displayHeadings( );

  26. Void Function Uses • Facilitates top-down design. • Can move displaying of headings and detail lines to a function without having to return a value. • Can be used with call-by-reference to “return” more than one value. • First an introduction to pointers.

  27. Pointer Defined • A pointer is the memory address of a variable. • We as programmers declare variables and use the variable name to point to a location in memory. • The computer uses the memory address allocated to a variable that was declared.

  28. Pointer Variables • A pointer variable is a special type of variable that can store a memory address instead of a payRate or hoursWorked. • Pointers are declared differently than regular variables.

  29. Declaring Pointer Variables • Declared using an asterisk (*).int regularInt; //variable for integer valueint *pointerInt; //pointer for an integer • To get the address assigned to regularInt use ampersand (&).pointerInt = &regularInt;

  30. The * and & Operators • Use asterisk (*) in front of pointer to reference the value stored in the location it points to. • Use ampersand (&) in front of a regular variable to get its memory address.int regularInt, *pointerInt; //declarepointerInt = &regularInt; //get address of regularInt*pointerInt = 100; //assign value to regularInt • regularInt now equals 100 because both variables point to the same place in memory.

  31. Uses of Pointers • Pointers are heavily used in C and many of the functions defined in the standard library. • Can be used to create dynamic variables. • This may help us to better understand • arrays • functions used in Windows programming (P12) • call-by-reference • At this point, just understand that they store a memory address instead of a regular value.

  32. call-by-value Functions • Values of the arguments are passed through the function’s parameters. • Many values can be passed to the functions, but only one value can be returned through the return statement. • Enter the call-by-reference.

  33. call-by-reference Functions • Addresses of the arguments are passed through the function’s parameters. • Many values can be passed to the functions, and many values can be “returned”. • Use the & to make the parameter a call-by-reference parameter.

  34. //prototype – make return type void void calcTax(double gross, double taxRate, double& tax); //definition void calcTax(double gross, double taxRate, double& tax) { tax = gross * taxRate; return; } //In the call-by-value example tax was declared in the body, now it is declared in header.

  35. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >> hours>> rate; gross = calcGross(hours, rate); calcTax(gross, FICA_RATE, fica); calcTax(gross, FEDERAL_RATE, federal); calcTax(gross, STATE_RATE, state); }

  36. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >> hours>> rate; gross = calcGross(hours, rate); calcTax(gross, FICA_RATE, fica); calcTax(gross, FEDERAL_RATE, federal); calcTax(gross, STATE_RATE, state); }

  37. main() Addresses and Values after initializa-tion and calcGross() calcTax()

  38. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >> hours>> rate; gross = calcGross(hours, rate); calcTax(gross, FICA_RATE, fica); calcTax(gross, FEDERAL_RATE, federal); calcTax(gross, STATE_RATE, state); }

  39. void calcTax(double gross, double taxRate, double& tax) { tax = gross * taxRate; // 24 = 400 * .06 return; } //value “returned” through parameter instead of through the return statement.

  40. main() Addresses and Values after fica calculation calcTax()

  41. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >> hours>> rate; gross = calcGross(hours, rate); calcTax(gross, FICA_RATE, fica); calcTax(gross, FEDERAL_RATE, federal); calcTax(gross, STATE_RATE, state); }

  42. void calcTax(double gross, double taxRate, double& tax) { tax = gross * taxRate; // 60 = 400 * .15 return; }

  43. main() Addresses and Values after federal calculation calcTax()

  44. void main() { hours, rate, gross, fica, federal, state; FICA_RATE = .06, FEDERAL_RATE = .15, STATE_RATE = .05; cin >> hours>> rate; gross = calcGross(hours, rate); calcTax(gross, FICA_RATE, fica); calcTax(gross, FEDERAL_RATE, federal); calcTax(gross, STATE_RATE, state); }

  45. void calcTax(double gross, double taxRate, double& tax) { tax = gross * taxRate; // 20 = 400 * .05 return; }

  46. main() Addresses and Values after state calculation calcTax()

  47. call-by-reference Functions • Addresses of the arguments are passed through the function’s parameters instead of the values stored in the arguments. • The function can now modify memory locations assigned to main(). When it does, it can be considered to have “returned” a value.

  48. Return Multiple Values • Use void and call-by-reference to “return” more than one value. • Code many call-by-reference parameters to “return” more than one value.

  49. main() { getHours(w1Hours, w2Hours, w3Hours, w4Hours); } void getHours(int& w1Hours, int& w2Hours, int& w3Hours, int& w4Hours) { cin >> w1Hours >> w2Hours >> w3Hours >> w4Hours; return; }

  50. Mixing Parameters Types • The parameters can be a mixture of call-by-value and call-by-reference. • If a function should NOT change the value, send it as a call-by-value.

More Related