Unformatted text preview:

1 Function overloading Sometimes you will create two or more functions that perform the same operations but use a different set of parameters or parameters of different data types For example you have a function that s used to square an int parameter and return an int However you need to square and return a double So you could actually have two function prototypes with different return values and parameters but the same name This is pretty handy Here is an example C Test 2 Study Guide Other Function Topics int divideby2 int double divideby2 double int main return 0 int divided divideby2 4 double divider divideby2 3 25 int divideby2 int number return number 2 double divideby2 double number return number 2 This is a very simple concept but there can be confusing when it comes to the question of certain overload functions being legal or illegal Example The following 3 functions are considered different and distinguishable by the compiler as they have different parameter lists int Process double num int Process char letter function 2 int Process double num int position function 3 function 1 Sample calls based on the above declarations int x float y 12 34 x Process 3 45 12 x Process f x Process y invokes function 3 invokes function 2 invokes function 1 automatic type conversion applies Default parameters new C feature not available in C 1 In C functions can be made more versatile by allowing default values on parameters This allows some parameters to be optional for the caller Therefore you can show which parameters are optional inside the declaration For example to create a function that takes one int and one double but if the user doesn t enter the second parameter it defaults to 10 0 It would look like this double exampleFunc int x double y 10 0 user can still explicitly enter a second parameter but if they do not then it will calculate using 10 0 as y 2 Understand how default parameters inside functions affect overloading capabilities A function that uses default parameters can count as a function with different numbers of parameters Recall the three functions in the overloading example int Process double num int Process char letter function 2 int Process double num int position function 3 function 1 Now suppose we declare the following function int Process double x int y 5 function 4 This function conflicts with function 3 obviously It ALSO conflicts with function 1 Consider these calls cout Process 12 3 10 cout Process 13 5 matches functions 3 and 4 matches functions 1 and 4 So function 4 cannot exist along with function 1 or function 3 BE CAREFUL to take default parameters into account when using function overloading 3 Random number generation using library functions It s pretty easy to make a random number generator Remember how we did it with our rock paper scissors assignment First you need to include the standard C library cstdlib contains functions for getting pseudo random numbers Then you need to include ctime a A pseudo random number starts with a seed value If you use the same seed then you will get a predictable pattern of random numbers srand is used to seed the random number generator and only needs to be called once at the start of main srand time 0 this seeds the random number generator b Once the random number has been seeded use the rand function to retrieve a random number The function takes no parameters and returns the random number int r r rand c The random number could be from a very large range so if we want to narrow the number down to a smaller range we can use the modulus operator r rand 10 r will be a random number from 0 9 this is because of the use of the modulus operator which will return the remainder if the random number when div by 10 d If we wanted to have the range of random numbers start somewhere other than 0 just add an offset or subtract to shift the starting point of the range r rand 10 1 a random number from 1 10 r rand 10 5 a random number from 5 14 Pass by Reference 1 To delare a reference variable we use the unary operator int n 5 this declares a variable n int r n this declares r as a reference to n a In this example r is now a reference to n They are both referring to the SAME storage location in memory To declare a reference variable add the operator after the type 2 Reference variables when used as a function parameter this allows access to the original argument A reference variable is an alias for another variable Any changes made to the reference variable are actually performed on the variable for which it is an alias By using a reference variable a function may change a variable that is defined in another function a If you plan on using a reference variable in your function your prototype and function declaration must have an address operator i double function1 int ii double function1 int variable b This tool is useful when variables are in different scopes i e functions You can pass the same variable through different functions and have it change inside each one if you use the address operator For Example include iostream using namespace std void doubler int void doubled int int main int x 2 int y 3 doubler x this would actually change the value of x to 4 in the main function and the doubler function doubled y this would just change the value of y to 6 in the doubled function but the value of y would not change in the main function return 0 void doubler int number number 2 void doubled int number number 2 c Know that reference variables are used in parameter passing and return values 3 Pass by Value local copies of parameters are made and copies of returns are sent back Recall that the variables in the formal parameter list are always local variables of a function You pretty much need to know that whenever you are sending a non reference variable into a function you are sending a copy and only changing in a local scope Outside this scope the value of the variable you passed remains the same Do not get confused with passing arrays through functions Array names are actually memory addresses so the values can be altered 4 Pass by Reference Copies of parameters and returns are not made Local parameters are references to the originals When you pass by reference no copy is made you are sending the actual memory address through the function The function will be able to alter the value of this variable 5 Use of const with reference parameters to prevent a function from changing the original but avoid overhead of making a copy faster


View Full Document

FSU COP 3014 - C++ Test 2 Study Guide

Download C++ Test 2 Study Guide
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 C++ Test 2 Study Guide 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 C++ Test 2 Study Guide 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?