. 16 Advanced Functions . Previous -- Simple functions etc .See ./15.html .Open Prepare . 6.11 Function Call Stack and Activation Records . 6.12 Functions with Empty Parameter Lists . 6.13 Inline Functions Simply: Short functions should be declared as "inline" to make the program run faster. . 6.14 References and Reference Parameters . 6.15 Default Arguments . 6.16 Unary Scope Resolution Operator . 6.17 Function Overloading . 6.18 Function Templates Neat, powerful, and really part of CSCI202! .Close Prepare . Deliver -- a question and P8 .Open Questions and Answers . Explain the differences between auto register extern mutable and static The only one of these that you need in CS201 is "static". Nearly all variables you will use are "auto" -- which means stored on the runtime stack. They are created by a declaration and deleted when you exit the surrounding block. Static variables are created by the linker when the program starts and are deleted when the program terminates. If they are local variables declared "static", however, you can only access them in that block, even though they exist outside the block. Global variable are in the static storage, and if you declare them as "static" then they are hidden from other files. However, if they are not declared "static" then another file can use them by declaring an "extern" variable. Register variables are not really needed -- a good compiler does this automatically for you anyway. Even if you specify "register" the compiler can ignore it! As a rule.... don't use anything but local variables in CS201. Note: the meanings of these keywords when applied to data members in a class is related but not identical to their use inside a function. The life time of normal data members is equal to the lifetime of their object, for example. If you declare a variable as a "static" data member" then it is attached to the class not the insances of a class. There is a single variable, any function in the calss can use it, and its life is equal to the whole program. This is a complex set of distinction that we will review in CS202 and CS320. . What happens if you don't put () after a function name It don't get a function call, you get the address of the compiled code that starts the function! So .As_is printit(); calls the function... but .As_is printit; compiles and does nothing. . Can a function inside of another function return values You can not just write a function declaration inside another one. But you can call a function, and return the result .As_is return sqrt(myfunction(x)); . Clarify the difference between pass-by-value and pass-by-reference Download and run this .See ./calls.cpp it shows how pass-by-valaue transfers a value from an expression in a function call into a parameter.... that stays inside the function, but pass_by_reference acts directly on the variable that is given to the function. . How and when to use call by reference (1) Use reference parameters when you need a function that changes the value of the variables it is given. (2) Use reference parameters to avoid copying large objects (and vectors). In this case add "const" to signal you won't change them. . When returning a reference to a local non-static variable You return a piece of garbage. Seriously-- we call a piece of storage that has been given up for reuse as garbage. On exit the local varaible become garbage and you return the address of the garbage. This is not a good idea. .What is a function prototype A function prototype is just like that function header except it has a semicolon after it: .As_is type name(parameters); It's purpose is to tell the compiler that such a function exists, and how to compile calls to it. The linker connects up the calls with the actual function declaration. In essence a function prototype is a promise that you will supply, sometime, a complete function that can be called. . How are default parameters used They let you omit some values in a call. They are replaced by the defaults. . If f has default values for its parameters can I write a call like this f(,,34) No. The omitted parameters are always at the end of the parameter list in the current version of C++. Perhaps later standards will allow this freedom, or something like it, since it is part of Ada. . What is the difference between f(void) and f() in a declaration In C++ there is no difference. In C the second forms allows any parameters at all. In C++ you write this f(...), and have to use special libraries to handle an unknown number of undefined parameters. Using a reference parameter in a call is just like any other call.... but you can only give the function a variable. Constants and expressions are rejected by the compiler. The function needs a variable to work on... . How is an everloaded function distinguished by its parameters The name and parameters .As_is type name(parameters); are combined (this is called .Key Name Maangling ) to give a longer name that contains both the name of the function and all the types of the parameters. It is complex and works without us worrying to much about it. When the function is called.... the values are matched to the right function. For example, .See ./overloaded.cpp . What happens if overloaded functions have the same signatures The compiler rejects them (I think). . How to use function overloading Don't worry about it! It just means that you don't have to think about keeping all the function names different. Make the name describe what it does and the parameters the data it uses. . Can we use overloading and default arguments to recreate C printf By using the (...) you can get exactly the functionallity of printf! You will find it in the library. Try .See http://www.cplusplus.com/reference/clibrary/cstdio/ for details. . What is the unary scope operator useful for Not much. Except when you have a local and a global variable with the same name. . How big can you build arrays (1) how much RAM or primary memory can you afford? (2) The subscripts must be stoable as int's -- so less than 2^32 on a 32 bit CPU. . Which is best Mac, PC, or Linux I don't like to give an answer to this question. (1) I use a Mac and a PC laptop at home, everyday. I use a PC Laptop and Linux desktop everyday at work. They all leave me feeling disatisfied. (2) A computer professional should be able to use any computer and any operating system better than an amateur. You use what is available. (3) It is more a matter of picking a system that is best for the job in hand.... you can set up a modern laptop or desktop to run two or more operating systems and switch as needed. . When to use recursion In CSCI202.... and when the data has a recursive structure -- for example hogh level language input. .Close Questions and Answers . Exercises . Quiz 7 . Next -- Vectors and more software engineering .See ./17.html