cs31s11dis4

cs31s11dis4 - CS31 Introduction to Computer Science I...

Info icon This 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; }
Image of page 1

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

View Full Document Right 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) replaces a substring of length n starting at i with another string s2, and sets s with a new string s.replace(2, 2, s2); // sets s to // “he!!!o” There are a few more functions, but this set of functions should be enough for our purposes.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern