560 likes | 777 Views
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).
E N D
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) • 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.
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) { double tax; tax = gross * taxRate; return tax; }
main() calcTax()
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); }
main() Values after cin and calcGross() calcTax()
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)
main() call with FICA_RATE calcTax()
double calcTax(double gross, double taxRate) { double tax; tax = gross * taxRate; // 24 = 400 * .06 return tax; }
main() Values after fica calculation calcTax()
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)
main() Call with FED_RATE calcTax()
double calcTax(double gross, double taxRate) { double tax; tax = gross * taxRate; // 60 = 400 * .15 return tax; }
main() Values after federal calculation calcTax()
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)
main() Call with ST_RATE calcTax()
double calcTax(double gross, double taxRate) { double tax; tax = gross * taxRate; // 20 = 400 * .05 return tax; }
main() Values after state calculation calcTax()
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.
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; }
Void as Return Value void displayDetails(double rate, int hours, double gross) { cout << rate << hours << gross << endl; return; }
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);
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( );
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.
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.
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.
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 = ®ularInt;
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 = ®ularInt; //get address of regularInt*pointerInt = 100; //assign value to regularInt • regularInt now equals 100 because both variables point to the same place in memory.
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.
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.
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.
//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.
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); }
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); }
main() Addresses and Values after initializa-tion and calcGross() calcTax()
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); }
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.
main() Addresses and Values after fica calculation calcTax()
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); }
void calcTax(double gross, double taxRate, double& tax) { tax = gross * taxRate; // 60 = 400 * .15 return; }
main() Addresses and Values after federal calculation calcTax()
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); }
void calcTax(double gross, double taxRate, double& tax) { tax = gross * taxRate; // 20 = 400 * .05 return; }
main() Addresses and Values after state calculation calcTax()
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.
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.
main() { getHours(w1Hours, w2Hours, w3Hours, w4Hours); } void getHours(int& w1Hours, int& w2Hours, int& w3Hours, int& w4Hours) { cin >> w1Hours >> w2Hours >> w3Hours >> w4Hours; return; }
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.