Multi-Paradigm Programming in C++: Functions and Operators
This content delves into functions and operators in C++, addressing topics such as transforming state, overloading parameter lists, pass-by-value vs. pass-by-reference semantics, and pointers. It discusses how functions and operators establish scopes, handle automatic and static variables, and initialize parameters. Examples illustrate the differences between pass-by-value and pass-by-reference, showcasing the impact on local and parameter variables.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
E81 CSE 428S: Multi-Paradigm Programming in C++ Functions and Operators Department of Computer Science & Engineering Washington University, St. Louis MO Chris Gill cdgill@wustl.edu 1
Functions and Operators Transform State Map (possibly empty) parameter list into outputs Different parameter lists overload the same name or symbol Arguments initialize the parameters at the point where called Each function or operator establishes a scope For local automatic variables (lifetimes within one call) For local static variables (lifetimes extend from call to call) Semantics of pass-by-value vs. pass-by-reference Pass by value creates a separate copy of the argument Pass by reference aliases the original argument Can pass pointer variables by value or by reference CSE 428S Multi-Paradigm Programming in C++ 2
Review: Pass by Value Semantics void foo () { int i = 7; baz (i); } local variable i (stays 7) 7 Think of this as declaration with initialization, along the lines of: int j = what baz was passed; void baz (int j) { j = 3; } parameter variable j (initialized with the value passed to baz, and then is assigned the value 3) 7 3 CSE 428S Multi-Paradigm Programming in C++ 3
Review: Pass by Reference Semantics void foo () { int i = 7; baz (i); } again declaration with initialization int & j = what baz was passed; 7 3 local variable i void baz (int & j) { j = 3; } j is initialized to refer to the variable that was passed to baz: when j is assigned 3, the passed variable is assigned 3. 7 3 argument variable j CSE 428S Multi-Paradigm Programming in C++ 4
Review: Pointers Passed by Value j is initialized with the address (value) that was passed to baz int foo () { int i = 7; return baz (&i); } address-of operator local variable i 7 3 int baz (int * j) { *j = 3; return *j; } dereference operator dereferencing j gives the location to which it points, so the variable whose address was passed is assigned 3. 0x74bead00 argument variable j CSE 428S Multi-Paradigm Programming in C++ 5
Review: Pointers Passed by Reference local variable i local variable j int foo () { int i = 7; int j = 4; int *p = &i; return baz (p, j); } 7 4 0x74bead04 0x74bead00 &i &k parameter q parameter k int baz (int * & q, int & k) { q = &k; return *q; } q 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 CSE 428S Multi-Paradigm Programming in C++ 6
Operator Precedence & Associativity The list of operators available in C++ is fixed Each has a symbol that corresponds to that operator Can t add operators that are not already on the list Each operator has a specified associativity Left associative: cout << j << endl; // left shift Right associative: i = j = k = 0; // assignment Relative precedence of operators is also pre-defined Can override precedence/associativity using parentheses LLM chapter 4.12 (Table 4.4) has all the operators with their precedence and associativity (and pages in LLM to read) Operators already defined for built-in types (e.g., int) Implicit type conversions increase their applicability (e.g., <) CSE 428S Multi-Paradigm Programming in C++ 7
Studio 4 Examine the precedence of operators from Studio 2 Show that some additional operators are already available due to implicit type conversions Construct sets (associative containers) of enumerated types from Studio 2 Use set intersection and set union algorithms Studios 0 through 11 are due 11:59pm Monday October 16th (the night before Exam 0) Submit as soon as each is done so you get feedback and can resubmit any that may be marked incomplete CSE 428S Multi-Paradigm Programming in C++ 8