CS161_Topic#11

CS161_Topic#11 - CS161 CS161 Introduction to Computer...

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

View Full Document Right Arrow Icon
1 CS161 CS161 Introduction to Introduction to Computer Science Computer Science Topic #11
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS161 Topic #11 2 Today in CS161 Review Functions Call by value versus Call by reference Look at more examples Introduction to Strings What is a string in C++? How can I define strings? How can I read and write strings?
Background image of page 2
CS161 Topic #11 3 Remember: Call by value brings values into a function (as the initial value of formal arguments) that the function can access but not permanently change the original actual args Call by reference can bring information into the function or pass information to the rest of the program the function can access the values and can permanently change the actual arguments!
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS161 Topic #11 4 Functions: Value vs. Reference Call by value is useful for: - passing information to a function - allows us to use expressions instead of variables in a function call - value arguments are restrained to be modified only within the called function; they do not affect the calling function. - can't be used to pass information back, except through a returned value
Background image of page 4
CS161 Topic #11 5 Functions: Value vs. Reference Call by reference is useful for: - allowing functions to modify the value of an argument, permanently - requires that you use variables as your actual arguments since their value may be altered by the called function; - you can't use constants or literals in the function call!
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS161 Topic #11 6 Example of call by reference: void convert (float inches, float & mils); int main() { float in; //local variable to hold # inches float mm; //local variable for the result cout << “Enter the number of inches: “; cin >> in; convert (in, mm); //function call cout << in << “ inches converts to “ << mm << “mm”; return 0; } mils = 25.4 * inches; }
Background image of page 6
CS161 Topic #11 7 Example of call by reference: void swap (int & a, int & b); int main() { int i = 7, j = -3; cout << "i and j start off being equal to :" << i << " & " swap(i,j); cout << "i and j end up being equal to :" << i << " & " return 0; } int temp = d; d = c; c = temp; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
CS161 Topic #11 8 What kind of args to use? Use a call by reference if:
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/17/2010 for the course CS 161 taught by Professor Fant during the Winter '08 term at Portland State.

Page1 / 26

CS161_Topic#11 - CS161 CS161 Introduction to Computer...

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

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