Download presentation
Presentation is loading. Please wait.
Published byGwen Fleming Modified over 8 years ago
1
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008
2
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 2 3.6 Variables of Functions
3
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 3 In C++, there are 3 types of variables that a function may make use of: Passed parameters and return parameter(s) The links between the called function and the calling function Local variable Visible only within a function For temporary local storage Global variable Visible to all functions in the program An old and dangerous way to communicate between functions Where do variables locate in your program?
4
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 4 Local Variables #include using namespace std; float Convert(float); //function prototype int main() {float TempFer; float TempCel = 10; TempFer = 100; TempCel = Convert(TempFer); cout << TempCel << endl; return 0; } float Convert(float Fer) {float Cel; Cel = ((Fer - 32) * 5) / 9; return Cel; } A function can define local variables for temporary usage Local variables are only visible within the function defining them
5
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 5 #include using namespace std; float Convert(float); int main() {float TempFer; float Cel = 10; TempFer = 100; Cel = Convert(TempFer); cout << Cel << endl; return 0; } float Convert(float Fer) {float Cel; Cel = ((Fer - 32) * 5) / 9; return Cel; } Cel in main() is different from the Cel in Convert() although their name is the same. Actually for each function, a separate piece of memory is allocated to the local variables of each function regardless of their names.
6
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 6 #include using namespace std; float Convert(float); int main() {float TempFer; float Cel = 10; TempFer = 100; Cel=Convert(TempFer); cout << Cel << endl; return 0; } Memory Variables float Convert(float Fer) { float Cel; Cel = ((Fer - 32) * 5) / 9; return Cel; } 1001037.710037.7 main() TempFerCel Convert() FerCel For return 37.7
7
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 7 Parameter Passing – Passed by Value It is seen in the previous example that parameters are passed by value Only a copy of the parameter value is passed to the called function. What the called function does to the passed parameters have nothing to do with the original one, since they are just two variables (and occupying different memory, even when their names are the same). However, such behavior sometimes is not preferred. In that case, we need the passed by reference parameter, which will be covered in the section of Pointers.
8
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 8 Global Variables #include using namespace std; int Convert(float); //function prototype changed float Cel; // Global variable int main() {float TempFer; cout << "Please enter the temperature in Fahrenheit: "; cin >> TempFer; Convert(TempFer); //No need to collect the return value cout << "\nHere's the temperature in Celsius: "; cout << Cel << endl; return 0; } int Convert(float Fer) { Cel = ((Fer - 32) * 5) / 9; return 0; } Global variable are visible to all functions. Must be carefully used. Make your program difficult to debug. Global variable are visible to all functions. Must be carefully used. Make your program difficult to debug.
9
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 9 Scope of Variables It is a rule of thumb that variables defined within a pair of braces are visible only to the statements in that braces after the variable is defined. #include using namespace std; void myFunc() { int x = 8; cout << "\nIn myFunc, local x: " << x << endl; { cout << "\nIn block in myFunc, x is: " << x; int x = 9; //This x is not the same as the previous x cout << "\nVery local x: " << x; } cout << "\nOut of block, in myFunc, x: " << x << endl; } int main() { myFunc(); return 0; } x = 8 x = 9 x = 8 Be careful!
10
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 10 Default Parameters Calling function should pass parameters of exactly the same types as those defined in the prototype of the called function long myFunction(int); // function prototype It means that any function that calls myFunction() should pass an integer to it. The only exception is if the function prototype has a default value, e.g., long myFunction(int x = 50) //default value If the calling function does not provide a parameter, 50 will be automatically used.
11
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 11 #include using namespace std; int volumeCube(int, int width = 25, int height = 1); int main() {int length = 100, width = 50, height = 2, volume; volume = volumeCube(length, width, height); cout << "First volume equals: " << volume << "\n"; volume = volumeCube(length, width); cout << "Second volume equals: " << volume << "\n"; volume = volumeCube(length); cout << "Third volume equals: " << volume << "\n"; return 0; } int volumeCube(int length, int width, int height) {return (length*width*height); } Default Parameters volume = 100x50x2 = 10000 volume = 100x50x2 = 10000 volume = 100x50x1 = 5000 volume = 100x25x1 = 2500 You may use other names
12
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 12 Overloading Functions C++ allows overloading of function, i.e., create more than one function with the same name. e.g. int myFunction (int, int); int myFunction (long, long); int myFunction (long); When a function calls myFunction(), the compiler checks the number and type of the passed parameters to determine which function should be called. Function overloading is also called polymorphism. Poly means many, morph means form A polymorphic function is many-formed. 3 different functions
13
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 13 #include using namespace std; int intDouble(int); float floatDouble(float); int main() {intmyInt = 6500, doubledInt; floatmyFloat = 0.65, doubledFloat; doubledInt = intDouble(myInt); doubledFloat = floatDouble(myFloat); cout << "doubledInt: " << doubledInt << "\n"; cout << " doubledFloat : " << doubledFloat << "\n"; return 0; } int intDouble(int original) {return 2*original; } float floatDouble(float original) {return 2*original; } The objective of both functions is to double the passed parameter. It looks much better to have a single function name but different parameter types. Overloading allows us to do so. The objective of both functions is to double the passed parameter. It looks much better to have a single function name but different parameter types. Overloading allows us to do so.
14
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 14 #include using namespace std; int Double(int); float Double(float); int main() {intmyInt = 6500, doubledInt; floatmyFloat = 0.65, doubledFloat; doubledInt = Double(myInt); doubledFloat = Double(myFloat); cout << "doubledInt: " << doubledInt << "\n"; cout << " doubledFloat : " << doubledFloat << "\n"; return 0; } int Double(int original) {return 2*original; } float Double(float original) {return 2*original; } Overloading
15
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 15 Exercise 3.6a void myFunc() { int x = 8; cout << "\nIn myFunc, local x: " << x << endl; cout << "\nIn block in myFunc, x is: " << x; int x = 9; cout << "\nVery local x: " << x; cout << "\nOut of block, in myFunc, x: " << x << endl; } The main() on the right is used to call myFunc() above. Build the program. What is the error message when compiling? Why? Correct the error accordingly. #include using namespace std; void myFunc(); int main() { myFunc(); return 0; }
16
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 16 Exercise 3.6b A for loop is used instead as follows: void myFunc() { int x = 8; cout << "\nIn myFunc, local x: " << x << endl; for (int x = 10; x>0; x--) cout << "\nInside for loop x: " << x; cout << "\nIn block in myFunc, x is: " << x << endl; } Do you think there is error message when compiling? What is the scope of the variables defined in the initialization part of the for loop? Verify it by building the program.
17
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 17 Exercise 3.6c Based on the program in p. 14, write a program that 1. Ask the user to input one integer, one floating point number, and one double number (what are the differences between an integer, floating point number and double number?) 2. Design a series of overloaded function square() that returns the square of the number the user inputs.
18
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 18 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.