LectureFifteen - C Programming Lecture 15 Functions Part V...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 15 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Hashemite University ٢ Outline ± Reference parameters and variables. ± Functions call by reference and call by value. ± Functions with default arguments. ± Unary scope resolution operator. ± Functions overloading. ± Functions templates.
Background image of page 2
The Hashemite University ٣ Reference Variables I ± Reference variable is an alias to some variable. ± is used to signify a reference ± E.g: int x = 0; int &y = x; //y is a reference to an integer which is x ± Here y is an alias to the variable x since the address of y is equal to the address of x. ± So, modifying either x or y both variables will have the same modified value since both of them refer to the same memory location or address.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Hashemite University ٤ Reference Variables II ± Reference variables must be initialized within the same statement that define them, if not it will a syntax error . ± Reference variables must be initialized with a variable only, constants and expressions are not allowed Æ ± You cannot reassign the reference variable to another variable since you simply copy the value of the new variable in the old one and you still working on the old one and this is considered as a logical error.
Background image of page 4
The Hashemite University ٥ Call By Reference I ± Two types of function call: ± Call by value ± Copy of data passed to function. ± Changes to copy do not change the original found in the caller. ± Used to prevent unwanted side effects. ± Call by reference ± Function can directly access data. ± Changes affect the original found in the caller. ± No copy exist (reduce overhead), however, it is dangerous since the original value is overwritten.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Hashemite University ٦ Call By Reference II ± Function arguments can be passed by reference. ± In both the function header and prototype you must proceed the reference variable by ± In the function call just type the name of the variable that you want to pass. ± Inside the function body use the reference variable by its name without &.
Background image of page 6
The Hashemite University ٧ Call By Reference III ± Again the reference argument must be a variable (constants or expressions are not allowed Æ syntax error ) ± E.g.: { variable += 3; } ± Adds 3 to the input variable which also affects the original variable (passed as argument to function change in the caller).
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 23

LectureFifteen - C Programming Lecture 15 Functions Part V...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online