Download presentation
Presentation is loading. Please wait.
Published byClara Hoover Modified over 9 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.5 Functions
3
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 3 Functions - Revisit A function is, in effect, a subprogram that can act on data and return a value When the name of the function is encountered in the program, the function is called. When the function returns, execution resumes on the next line of the calling function. Callingfunc() { Statements ; funcA(); Statements ; funcB(); Statements ; } funcA() { Statements ; return; } funcB() { Statements ; return; } Return value is void input output
4
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 4 Function Body unsigned short int FindArea (int length, int width); return typefunction name type of input parameters name of input parameters {// Opening brace Statements ; return (return_value); }// Closing brace return value
5
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 5 Why do we need functions? Functions help us shorten our program by re- using the program codes #include using namespace std; void floatDiv(int x, int y) {float a = (float)x; float b = static_cast (y); float c = a/b; cout << "c: " << c << endl; } int main() {int w = 7, x = 5, y = 3, z = 2; floatDiv(w,x); floatDiv(x,y); floatDiv(y,z); return 0; } 13 lines Reason 1 Ex 3.5
6
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 6 The same program will be much longer without function Can be even longer if the same operation is done in other part of the program int main() {int w = 7, x = 5, y = 3, z = 2; float a, b, c; a = (float)w; b = static_cast (x); float c = a/b; cout << "c: " << c << endl; a = (float)x; b = static_cast (y); float c = a/b; cout << "c: " << c << endl; a = (float)y; b = static_cast (z); float c = a/b; cout << "c: " << c << endl; return 0; } 16 lines
7
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 7 Functions make our program much easier to read. void floatDiv(int x, int y) {float a = (float)x; float b = static_cast (y); float c = a/b; cout << "c: " << c << endl; } int main() {int w = 7, x = 5, y = 3, z = 2; floatDiv(w,x); floatDiv(x,y); floatDiv(y,z); return 0; } In this program, it is easily seen that 3 floating-point divisions are to be done. Not the case of the previous program without the function. Reason 2
8
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 8 Declaring Functions In C++, anything that is not a part of the C++ language needs to be declared. However, a function need NOT be separately declared if it is placed before the functions that will call it. #include using namespace std; void DemoFunction() { cout << "In Demo Function\n"; } int main() { cout << "In main\n"; DemoFunction(); cout << "Back in main\n"; return 0; } Since DemoFunction() is placed before main(), it need not be separately declared. DemoFunction() can be used directly Since DemoFunction() is placed before main(), it need not be separately declared. DemoFunction() can be used directly Function prototype
9
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 9 However, it is a bad programming practice to require functions to appear in a particular order because It is difficult for code maintenance (too restrictive). Two functions may call each other (typically inside some loop). void FuncA() { : FuncB(); : } void FuncB() { : FuncA(); : } Which one should be placed first?
10
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 10 Functions are usually declared by either one of the two ways: Write the prototype of the function at the beginning of the file in which the function is used. Put the prototype of the function into a header file and include it in the file in which the function is used. Function Prototype unsigned short int FindArea(int, int); return typefunction name type of input parameters
11
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 11 Declaring Functions #include using namespace std; int Area(int length, int width);// function prototype int main() {int lengthOfYard; int widthOfYard; int areaOfYard; cout << "\nHow wide is your yard? "; cin >> widthOfYard; cout << "\nHow long is your yard? "; cin >> lengthOfYard; areaOfYard = Area(lengthOfYard,widthOfYard); cout << "\nYour yard is "; cout << areaOfYard; cout << " square feet \n\n"; return 0; } int Area(int yardLength, int yardWidth) {return yardLength * yardWidth; } Although it is not necessary, adding the name of the parameters makes the prototype easier to read Area() is placed after main(). Note the name of the parameters are not the same as the prototype
12
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 12 #include #include "area.h"// function prototype using namespace std; int main() {: areaOfYard = Area(lengthOfYard,widthOfYard); : return 0; } int Area(int yardLength, int yardWidth) {return yardLength * yardWidth; } int Area(int length, int width);// function prototype Assume the file area.h has the following statement and is at the same folder as main() It is equivalent to place the content of area.h to here Ex 3.5
13
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 13 Putting Function Prototypes to Header file Advantage: if a particular set of function prototypes is often used in different programs, we need not declare them every time they are needed. E.g. iostream Contains prototypes of many functions that are related to the manipulation of I/O stream. Is needed in most programs that need I/O, such as cout, cin, etc. Should be included at the beginning of most programs; otherwise, we need to type all prototypes in every program. In different.cpp files One line Vs many lines
14
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 14 Exercise 3.5 1)For the program on p. 5, add the function prototype such that we can place the function floatDiv() after main(). 2)Modify the program you have developed in 1) as follows: Remove the function prototype. Use Notepad to prepare a file named floatDiv.h that contains just one statement: the function prototype of floatDiv(). Store the file floatDiv.h in the same folder as your C++ file. Include this header file at the beginning of the program as the example on p. 12. Achieve the same result as 1). P.5 P.12
15
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 15 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.