DOC PREVIEW
WUSTL CSE 332S - Lecture Notes

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Review: What = and & MeanReview: Parameter/Variable DeclarationsHow Function Calls WorkReview: Pass By ValueReview: Pass By ReferenceWhat About Pointers as By-Value Arguments?What About Passing Pointers By-Reference?Pass By const ReferenceDefault ArgumentsRequired vs. Default ArgumentsDefaults with Multiple ArgumentsDefault Argument LimitationsVarying ParametersFunction OverloadCSE 332: C++ functionsReview: What = and & Mean•In C++ the = symbol means either initialization or assignment–If it’s used with a type declaration, it means initialization–If it’s used without a type declaration, it means assignmentint j(7); // j is initialized with value 7int k = 4; // k is initialized with value 4j = 3; // j is assigned value 3•In C++ the & symbol also has a similar “dual nature”–If it’s used inside a type declaration, it means a reference (an alias)•Arguments to function are always declared along with their types–If it’s used outside a type declaration, it means “address of”int swap (int & i, int & j); // references to intint & s = j; // reference s initialized to refer to jint * p = & j; // pointer p initialized w/ j’s addressCSE 332: C++ functionsReview: Parameter/Variable Declarations•Hint: read parameter and variable declarations right to leftint i; “i is an integer”int & r = i; “r is a reference to an integer (initialized with i)”int * p; “p is a pointer to an integer”int * & q = p; “q is a reference to a pointer to an integer (initialized with p)”•Read function declarations inside out“function main takes an integer and an array of pointers to char, and returns an integer”int main (int argc, char * argv[]);“function usage takes a pointer to char, and returns void (nothing)”void usage (char * program_name);“function setstring takes a reference to a (C++) string, and returns void”void setstring (string & s);CSE 332: C++ functionsHow Function Calls Work•A function call uses the “program call stack”1. Stack frame is “pushed” when the call is made2. Execution jumps to the function’s code block3. Function’s code block is executed4. Execution returns to just after where call was made5. Stack frame is “popped” (variables in it destroyed)•This incurs a (small) performance cost–Copying arguments, other info into the stack frame–Stack frame management–Copying function result back out of the stack frameCSE 332: C++ functionsReview: Pass By Valuevoid foo (){ int i = 7; baz (i);}void baz (int j){ j = 3;}77 → 3local variable i (stays 7)parameter variable j (initialized with the value passed to baz, and then is assigned the value 3)Think of this as declaration with initialization, along the lines of:int j = what baz was passed;CSE 332: C++ functions Review: Pass By Referencevoid foo (){ int i = 7; baz (i);}void baz (int & j){ j = 3;}7 → 3local variable ij is initialized to refer to the variable thatwas passed to baz:when j is assigned 3,the passed variableis assigned 3.7 → 3again declarationwith initializationint & j = what baz was passed;argument variable jCSE 332: C++ functions What About Pointers as By-Value Arguments?void foo (){ int i = 7; baz (&i);}void baz (int * j){ *j = 3;}7 → 3local variable idereferencing jgives the location to which it points, sothe variable whoseaddress was passedis assigned 3.argument variable j0x74bead00dereference operatoraddress-of operatorj is initialized with the address (value) thatwas passed to bazCSE 332: C++ functions What About Passing Pointers By-Reference?void foo (){ int i = 7; int j = 4; int *p = &i; baz (p, j);}void baz (int * & q, int & k){ q = &k;}7local variable iq references p and k references j: when q is assigned the address of k, the effect is to make p point to j instead of i argument variable q4local variable j0x74bead04argument variable k0x74bead00&i → &kCSE 332: C++ functions Pass By const Referencevoid foo (){ int i = 7; baz (i);}void baz (const int & j){ cout << j << endl;}7local variable ij is initialized to refer to the variable thatwas passed to baz:j can not be changed. 7again declarationwith initializationConst int & j = what baz was passed; and it is read only. argument variable jCSE 332: C++ functionsDefault Arguments•Some functions can take several arguments–Can increase function flexibility–Can reduce proliferation of near-identical functions•But, callers must supply all of these arguments–Even for ones that aren’t “important”•We can provide defaults for some arguments–Caller doesn’t have to fill these inCSE 332: C++ functionsRequired vs. Default Arguments•Function with required argument// call as foo(2); (prints 2)void foo(int a); void foo(int a) {cout << a << endl;}•Function with default argument–Notice only the declaration gives the default value// can call as foo(2); (prints 2)// or can call as foo(); (prints 3)void foo(int a = 3);void foo(int a) {cout << a << endl;}CSE 332: C++ functionsDefaults with Multiple Arguments•Function with one of two arguments defaulted// can call as foo(2); (prints 2 3)// or can call as foo(2, 4); (prints 2 4)void foo(int a, int b = 3); void foo(int a, int b) {cout << a << “ ” << b << endl;}•Same function, with both arguments defaulted// can call as foo(); (prints 1 3)// or can call as foo(2); (prints 2 3)// or can call as foo(2, 4); (prints 2 4)void foo(int a = 1, int b = 3); void foo(int a, int b) {cout << a << “ ” << b << endl;}CSE 332: C++ functionsDefault Argument Limitations•Watch out for ambiguous signatures–foo(); and foo(int a = 2); for example•Can only default the rightmost arguments–Can’t declare void foo(int a = 1, int b);•Caller must supply leftmost arguments–Even if they’re the same as the defaultsCSE 332: C++ functionsVarying Parametersinitializer_list<T> parameters;void foo(initializer_list<string> coffee){ for (auto beg = coffee.begin(); beg != coffee.end(); ++beg) { cout << *beg << “ ”;}cout << endl;}//print out all 4 coffeesfoo(“latte”, “mocha”, “eggnog latte”, “peppermint mocha”);//print out only 2 coffeesfoo(“latte”, “mocha”);CSE 332: C++ functionsFunction Overload// can call errMsg with 1 or 2 integers or a stringvoid errMsg(int & errCode) {cout << “Error code is: ” << errCode<< endl;}void errMsg(const int & errCode) {cout << “Error code is: ” << errCode<< endl;}void errMsg(int & errCode, string msg) {cout <<


View Full Document

WUSTL CSE 332S - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?