Chapter 4 Parameters and Overloading

Used to provide access to caller’s actual argument Caller’s data can be modified by called function! Typically used for input function To retrieve data for caller Data is then "given" to caller. Specified by ampersand, &, after type in formal parameter list | Chapter 4 Parameters and Overloading Learning Objectives Parameters Call-by-value Call-by-reference Mixed parameter-lists Overloading and Default Arguments Examples, Rules Testing and Debugging Functions assert Macro Stubs, Drivers Parameters Two methods of passing arguments as parameters Call-by-value "copy" of value is passed Call-by-reference "address of" actual argument is passed Call-by-Value Parameters Copy of actual argument passed Considered "local variable" inside function If modified, only "local copy" changes Function has no access to "actual argument" from caller This is the default method Used in all examples thus far Call-by-Value Example: Display Formal Parameter Used as a Local Variable (1 of 3) Call-by-Value Example: Display Formal Parameter Used as a Local Variable (2 of 3) Call-by-Value Example: Display Formal Parameter Used as a Local Variable (3 of 3) Call-by-Value Pitfall Common Mistake: Declaring parameter "again" inside function: double . | Chapter 4 Parameters and Overloading Learning Objectives Parameters Call-by-value Call-by-reference Mixed parameter-lists Overloading and Default Arguments Examples, Rules Testing and Debugging Functions assert Macro Stubs, Drivers Parameters Two methods of passing arguments as parameters Call-by-value "copy" of value is passed Call-by-reference "address of" actual argument is passed Call-by-Value Parameters Copy of actual argument passed Considered "local variable" inside function If modified, only "local copy" changes Function has no access to "actual argument" from caller This is the default method Used in all examples thus far Call-by-Value Example: Display Formal Parameter Used as a Local Variable (1 of 3) Call-by-Value Example: Display Formal Parameter Used as a Local Variable (2 of 3) Call-by-Value Example: Display Formal Parameter Used as a Local Variable (3 of 3) Call-by-Value Pitfall Common Mistake: Declaring parameter "again" inside function: double fee(int hoursWorked, int minutesWorked) { int quarterHours; // local variable int minutesWorked // NO! } Compiler error results "Redefinition error " Value arguments ARE like "local variables" But function gets them "automatically" Call-By-Reference Parameters Used to provide access to caller’s actual argument Caller’s data can be modified by called function! Typically used for input function To retrieve data for caller Data is then "given" to caller Specified by ampersand, &, after type in formal parameter list Call-By-Reference Example: Display Call-by-Reference Parameters (1 of 3) Call-By-Reference Example: Display Call-by-Reference Parameters (2 of 3) Call-By-Reference Example: Display Call-by-Reference Parameters (3 of 3) Call-By-Reference Details What’s really passed in? A "reference" back to caller’s actual argument! Refers to memory location of actual argument Called "address", which is a unique number referring to distinct place in memory Constant Reference .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.