18

Click here to load reader

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

Embed Size (px)

DESCRIPTION

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?

Citation preview

Page 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

1

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

Learning the C++ language 3. The Nuts and Bolts of C++

16 September 2008

Page 2: 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 Programming3. The Nuts and Bolts of C++

3.6 Variables of Functions

Page 3: 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

3

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

• 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?

Page 4: 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

4

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

Local Variables#include <iostream>using namespace std;

float Convert(float); //function prototypeint 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

Page 5: 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

5

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

#include <iostream>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.

Page 6: 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

6

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

#include <iostream>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;}

100 10 37.710037.7

main()

TempFer Cel

Convert()

Fer CelFor return

37.7

37.7

Page 7: 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

7

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

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.

Page 8: 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

8

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

Global Variables#include <iostream>using namespace std;int Convert(float); //function prototype changedfloat Cel; // Global variableint main(){ float TempFer;

cout << "Please enter the temperature in Fahrenheit: ";cin >> TempFer;Convert(TempFer); //No need to collect the return valuecout << "\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.

Page 9: 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

9

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

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 <iostream>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 = 8

x = 9

x = 8

Be careful!

Page 10: 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

10

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

Default Parameters• Calling function should pass parameters of exactly the same

types as those defined in the prototype of the called functionlong 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 valueIf the calling function does not provide a parameter, 50 will be automatically used.

Page 11: 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

11

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

#include <iostream>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 = 100x50x1 =

5000

volume = 100x25x1

= 2500

You may use other names

Page 12: 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

12

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

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

Page 13: 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

13

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

#include <iostream>using namespace std;int intDouble(int);float floatDouble(float);int main(){ int myInt = 6500, doubledInt;

float myFloat = 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.

Page 14: 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

14

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

#include <iostream>using namespace std;int Double(int);float Double(float);int main(){ int myInt = 6500, doubledInt;

float myFloat = 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

Page 15: 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

15

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

Exercise 3.6avoid 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 <iostream>using namespace std;void myFunc();int main(){ myFunc(); return 0;}

Page 16: 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

16

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

Exercise 3.6bA 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.

Page 17: 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

17

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

Exercise 3.6cBased 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.

Page 18: 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

18

3. The Nuts and Bolts of C++

Computer Programming3. The Nuts and Bolts of C++

Acknowledgment

The slides are based on the set developed by Dr. Frank Leung (EIE).