Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Functions A bit of review (things we’ve covered so far)

Similar presentations


Presentation on theme: "C++ Functions A bit of review (things we’ve covered so far)"— Presentation transcript:

1 C++ Functions A bit of review (things we’ve covered so far)
We’ll distinguish functions vs. member functions main is a member of a class in Java, but not in C++ Declarations (prototypes without a body) belong in header files Definitions belong in source files (compilation units) Today we’ll cover Function declarations, definitions, and calls Parameter and variable declarations, const and & Function arguments passed by reference vs. by value Inlining functions, and default function arguments Libraries often offer lots of helpful functions E.g., strlen () from the <cstring> library (text, pp. 116) E.g., strcmp () from the <cstring> library (text, pp. 201) E.g., isalpha () from the <cctype> library (text, pp. 249)

2 Declaring, Defining, and Calling C++ Functions
// function declarations in myfile.h void foo (); void baz (int j); // function definitions in myfile.cc void foo (){ int i = 7; // foo calls baz, passing variable i to it baz (i); } void baz (int j){ cout << j << endl; // prints passed value

3 Parameter/Variable Declarations
Read function parameter & variable declarations right to left int 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)” “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” void usage (char * program_name); “function getstring takes a reference to a (C++) string, and returns void” void getstring (string & s);

4 How Const Works Making something const says that a value cannot be changed through it It’s ok for a const reference or pointer to access a non-const variable/object Not ok for a non-const pointer or reference to access a const variable/object const int i = 7; // value of i cannot be changed int j = 7; // value of j can be changed const int & r = i; // r cannot be used to change i const int & s = j; // s cannot be used to change j // cannot say int & t = i; int * p = 0; “p is a pointer to an integer” const int * q = 0; “q is a pointer to an integer that’s const” int const * r = 0; “r is a pointer to a const integer (same as q, less common)” int * const s = 0; “s is a const pointer to an integer (similar to an array)” const int * const t = 0; “t is a const pointer to an integer that’s const” const int & u = i; “u is a reference to an integer that’s const” const int * & v = q; “v is a reference to a pointer to an integer that’s const” int * const & w = s; “w is a reference to a const pointer to an integer” const int * const & x = t; “x is a reference to a const pointer to an integer that’s const”

5 What & Means Remember how = can mean 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 assignment int j = 7; // j is initialized with value 7 j = 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) If it’s used outside a type declaration, it means “address of” int & s = j; // reference s initialized to refer to j int * p = & j; // pointer p initialized w/ j’s address

6 Pass By Value void foo () { int i = 7; baz (i); } void baz (int j)
local variable i Think of this as declaration with initialization, along the lines of: int j = what baz was passed; argument variable j (initialized with the value passed to baz, then assigned value 3) 7 → 3

7 Pass By Reference void foo () { int i = 7; baz (i); }
again declaration with initialization int & j = what baz was passed; void foo () { int i = 7; baz (i); } void baz (int & j) j = 3; 7 → 3 local variable i 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

8 More About Functions in C++
Inlining Makes use of simple functions faster (at a cost) Default Arguments Fill in information the caller does not specify Can live without these features in many cases Due to compiler versions Due to other design considerations However, they can be useful in some cases Workarounds may add code/design complexity Or may result in some performance degradation

9 How Function Calls Work
A function call uses the “program call stack” Stack frame “pushed” when the call is made Execution jumps to the function’s code block Function’s code block is executed Execution returns to just after where call was made Stack frame is “popped” 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 frame

10 Motivation for Inlining
If the function body is small Computer might do more work manipulating the stack frame than in the actual function Classic computer science tradeoff If we spend some more space, can we save time? We can ask the compiler to use function inlining Intention: a directive, sometimes just a request

11 How Inlining Works Basic idea
Instead of pushing a stack frame, … … do all of the work in place Compiler makes an “in line” copy of the code at each place where it’s called Code is inserted once for every function call made Rather than the code being in a single place, as for standard function calls

12 Benefits and Costs of Inlining
Especially good for small repetitive function calls Class/struct member accessors and mutators Empty or very simple implementations Be aware of how much inlining will cost How often are the calls invoked? Use code analysis tools What is the relative cost of the function call Compared to code being inlined Shorter functions have higher relative call costs Space cost can be non-trivial Especially if space is limited (embedded systems)

13 Limitations for Inlining
There are limitations on when inlining can be used Depend on the compiler and target The inline keyword is just a suggestion to the compiler Increase portability: make inlining requests optional Same code can be used with inlining on or off Clever use of #define pre-compiler directive, and an additional kind of file: inline (.inl) file See example, next

14 Example of Portable Inlining
// foo.h #ifdef USE_INLINE #define INLINE inline #include “foo.inl” #else #define INLINE double square(const double x); #endif // foo.cpp #include “foo.h” #ifndef USE_INLINE // foo.inl INLINE double square(const double x) {return x * x;} Header file (foo.h) Macro to control inlining Includes implementation if inlining is on Source file (foo.cpp) Includes implementation if inlining is off Inline file (foo.inl) Actual function definition

15 Building with Portable Inlining
Control inlining on the compile line g++ -DUSE_INLINE –o foo foo.cpp Or in the Makefile CXXFLAGS = -DUSE_INLINE This adds a bit of complexity to your code base, … … but can make it more portable across platform architectures and C++ compilers

16 Default 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 in

17 Required 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);

18 Defaults 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) void foo(int a = 1, int b = 3);

19 Default 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 defaults

20 For Next Time Topic: C++ Debugging in Eclipse Assigned readings: none
Please make sure to catch up if needed


Download ppt "C++ Functions A bit of review (things we’ve covered so far)"

Similar presentations


Ads by Google