cs31s11dis4sol - CS31 Introduction to Computer Science I...

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

View Full Document Right Arrow Icon
Discussion 1H Notes (Week 4, April 22) TA: Brian Choi ([email protected]) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Passing Arguments By Value and By Reference So far, we have been passing in “values” into functions. We say we pass arguments by value . This method does not allow you to access variables outside. We will make a way to access an outside variable now. Output: What is that ampersand in the function definition? As usual, when the function rush is called, it will start by creating the variables for the arguments – currentTotal , yards , and count . Then it sees the ampersand before count . This means it is a reference to a variable, not a real variable. The variable it references to is what is passed from main() – in this case, rushCount . count is like a window that leads to the box labeled rushCount in the main world! (In effect, we are returning two values here.) In the function rush , whenever we access count , we are actually accessing rushCount in main. When the function terminates, we’ll destroy the memory space for rush , but the changes made to rushCount will remain. We say that count is passed by reference . When in doubt, draw the “box” diagram. void functions There are functions that do not have to return any return value. For example, it might be some function that displays a few lines of text such as “About this program: Copyright 2008 blah blah. ..” message. You can create such a function by making the return type void . You do not have to have a return statement, but if you need a return statement to terminate the function, you can just say “ return; ” without any value. CS31: Introduction to Computer Science I Spring 2011 Copyright Brian Choi 2011. All Rights Reserved. Week 4, Page 1/9 double rush(double currentTotal, double yards, int &count) // ampersand! { count++; return currentTotal + yards; } int main() { double rushTotal = 0; int rushCount = 0; rushTotal = rush(rushTotal, 10.5, rushCount); rushTotal = rush(rushTotal, 5.3, rushCount); rushTotal = rush(rushTotal, 7.2, rushCount); cout << “Our running back ran “ << rushCount << “ times and the total of “ << rushTotal << “ yards.” << endl; return 0; }
Background image of page 1

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

View Full DocumentRight Arrow Icon
More on Strings Here are things you can do with strings. operation what it does example string s = “hello”; string s2 = “!!!”; declares strings s and s2 s.length() or s.size() returns the length of s cout << s.size(); // prints 5 s[i] or s.at(i) returns i -th character i must be an integer between 0 and size-1 (inclusive). cout << s[1]; // prints ‘e’ cout << s.at(0); // prints ‘h’ s.empty() returns true if s is empty if (!s.empty()) cout << “not empty”; s + s2 s + “?” concatenates two strings cout << s + s2; // prints “hello!!!” cout << s + “?”; // prints “hello?” s.substr(i, n) s.substr(i) takes a substring of length n, starting from the i -th character cout << s.substr(2,2); // prints “ll” cout << s.substr(2); // prints “llo” s.replace(i, n, s2)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 9

cs31s11dis4sol - CS31 Introduction to Computer Science I...

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

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