Docsity

C++ Functions: Parameters, Return Types, and Examples, Summaries of Programming Abstractions

Partial preview of the text

Download C++ Functions: Parameters, Return Types, and Examples and more Summaries Programming Abstractions in PDF only on Docsity!

Wednesday, September 28, 2016

Programming Abstractions

Fall 2016

Stanford University

Computer Science Department

Lecturer: Chris Gregg

reading:

Programming Abstractions in C++, Chapters 2-

CS 106B Lecture 2: C++ Functions parameters function result

Today's Topics

  • (^) Logistics:
    • (^) Signing up for section
    • (^) CS 106L
    • (^) Qt Creator installation help on Thursday
    • (^) Stanford Local Programming Contest: Saturday
  • (^) Homework 1: Fauxtoshop!
    • (^) Due Friday, October 7, at Noon
    • (^) A note on the honor code and cheating
    • (^) YEAH Hours tonight! (will be recorded!)
  • (^) Functions
    • (^) Some review — functions are very similar to Java functions!
    • (^) Value semantics
    • (^) Reference semantics
  • (^) Reading Assignment: Chapters 2 and 3

Assignment 1: Fauxtoshop!

Scatter!

Click for Intro Video!

Assignment 1: Fauxtoshop!

Edge Detection!

Assignment 1: Fauxtoshop!

Compare Images!

Fauxtoshop

  • The program you write will utilize:
    • Functions
    • Constants
    • Loops
    • I/O (cout, getLine(), getInteger())
    • Reference semantics, Value semantics
    • Strings
    • Logic
    • Nicholas Cage
  • We will discuss all of the above before the project is due
  • Get started early! (Idea: finish Scatter by Friday!)
  • Go to YEAH hours! Go to the LaIR!
  • This is a non- pair programming program — you must work independently
  • Due: 12pm (Noon) on Friday, October 7

Defining Functions (2.3) A C++ function is like a Java method. Similar declaration syntax but without the public or private keyword in front. type functionName(type name, type name, ..., type name) { statement; statement; ... statement; return expression; // if return type is not void } return type parameters Calling a function: functionName(value, value, ..., value); arguments (called in the same order as the parameters)

Function Return Types A C++ function must have a return type, which can be any time (including user-defined types, which we will cover later). double square(double x); // returns a double Vector matrixMath(int x, int y); // returns a Vector // probably not a good // idea! (covered later) string lowercase(string s); // returns a string (maybe // not a good idea... void printResult(Vector &v); // returns nothing! A C++ function can only return a single type, and if you want to return multiple "things," you have to do it differently (unlike in languages such as Python). We will cover this later, as well.

Function Example: Output 99 bottles of Coke on the wall. 99 bottles of Coke. Take one down, pass it around, 98 bottles of Coke on the wall. 98 bottles of Coke on the wall. 98 bottles of Coke. Take one down, pass it around, 97 bottles of Coke on the wall. 97 bottles of Coke on the wall. 97 bottles of Coke. Take one down, pass it around, 96 bottles of Coke on the wall. ... 3 bottles of Coke on the wall. 3 bottles of Coke. Take one down, pass it around, 2 bottles of Coke on the wall. 2 bottles of Coke on the wall. 2 bottles of Coke. Take one down, pass it around, 1 bottles of Coke on the wall. 1 bottles of Coke on the wall. 1 bottles of Coke. Take one down, pass it around, 0 bottles of Coke on the wall.

Function Example, brought to you by #include #include "console.h" using namespace std; const string DRINK_TYPE = "Coke"; // Function Definition and Code void bottles(int count) { cout << count << " bottles of " << DRINK_TYPE << " on the wall." << endl; cout << count << " bottles of " << DRINK_TYPE << "." << endl; cout << "Take one down, pass it around, " << (count-1) << " bottles of " << DRINK_TYPE << " on the wall." << endl << endl; } int main() { for (int i=99; i > 0; i--) { bottles(i); } return 0; } (bus drivers hate them!) How many functions does this program have? What does the bottles() function return? Answer: 2. bottles() and main() Answer: nothing (void function)

Function Example 2 // Function example #2: returning values #include #include "console.h" using namespace std; int larger(int a, int b) { if (a > b) { return a; } else { return b; } } // Returns the larger of the two values. int main() { int bigger1 = larger(17, 42); // call the function int bigger2 = larger(29, -3); // call the function again int biggest = larger(bigger1, bigger2); cout << "The biggest is " << biggest << "!!" << endl; return 0; }

Function Example 2 // Function example #2: returning values #include #include "console.h" using namespace std; int larger(int a, int b) { if (a > b) { return a; } else { return b; } } // Returns the larger of the two values. int main() { int bigger1 = larger(17, 42); // call the function int bigger2 = larger(29, -3); // call the function again int biggest = larger(bigger1, bigger2); cout << "The biggest is " << biggest << "!!" << endl; return 0; } Output:

Function Example 2: Debugging

  • Notes from live debugging:
    • You can see variable values as the program executes
    • You use the following buttons to continue the program: continue to next breakpoint stop running Go to next line but not into functions Go to next line and into functions Finish the function and leave it
  • Debugging effectively takes a little time to learn, but is super effective if you have hard to find bugs.

Declaration Order #include #include "console.h" using namespace std; const string DRINK_TYPE = "Coke"; int main() { for (int i=99; i > 0; i--) { bottles(i); } return 0; } // Function Definition and Code void bottles(int count) { cout << count << " bottles of " << DRINK_TYPE << " on the wall." << endl; cout << count << " bottles of " << DRINK_TYPE << "." << endl; cout << "Take one down, pass it around, " << (count-1) << " bottles of " << DRINK_TYPE << " on the wall." << endl << endl; }

  • Believe it or not, this program does not compile!
  • In C++, functions must be declared somewhere before they are used.
  • But, we like to put our main() function first, because it is better style.