Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++ EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
Data Structures: Lecture 4 Lecture outline Announcements/reminders Program 1 due Friday, 2/8 All programs to be submitted via Blackboard Submit a single .zip file containing all files for this assignment Today’s lecture Review: structures Nested structures Functions in C++ Declarations Argument passing: by value, by address, and by reference 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Review: Structures User-defined types; example: struct StudentInfo { char first[50]; char middle; char last[50]; unsigned int ID; double GPA; }; Can define variables of that type Scalar: StudentInfo student1; Array: StudentInfo classList[10]; Pointer: StudentInfo *sPtr; Access members using Dot operator: student1.middle = ‘J’; Arrow (if pointers): sPtr->GPA = 3.5; Typically passed to functions by address 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Nested structures Structures can contain other structures: struct Name { char first[50]; // First name char middle; // Middle initial char last[50]; // Last name }; struct SINew { Name sname; // Student name unsigned int ID; // ID # double GPA; // Grade point Will need multiple dot operators to access field within nested structure Given SINew s1; s1.sname Name structure within s1 s1.sname.middle middle initial of name within s1 5/9/2019 Data Structures: Lecture 4
Typical file usage with structures Each structure has its own .h/.cpp files .h file (i.e., Name.h, SINew.h) contains structure definition and prototypes of related functions .cpp file (i.e., Name.cpp, SINew.cpp) contains function definitions To use struct type and functions, include .h file SINew.h would include Name.h Allows Name variable inside SINew structures Main program could include both … … although that’s redundant—including SINew.h implicitly includes Name.h Will organize classes similarly 5/9/2019 Data Structures: Lecture 4
Avoiding multiple inclusion of .h files Say we have two files that start as shown: file1.h: #include "file2.h" ... myprogram.cpp: #include "file1.h" What’s the problem? What does an #include directive really do? 5/9/2019 Data Structures: Lecture 4
Avoiding multiple inclusion of .h files Anything in file2.h is included twice #include directive copies/pastes contents of file Compiler will think program redefines structures, function prototypes, etc. Solution: conditional compilation Header files include header guard code to ensure they’re only compiled once Example code for fictional file2.h: #ifndef file2_h // Start of file #define file2_h ... #endif // End of file Code between #ifndef & #endif only compiled once 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Functions Used to break programs into smaller pieces Useful when code sequences repeated Functions have: An optional return value A name Optional arguments Must be prototyped or written completely prior to use C++ supports three forms of argument passing Pass by value (also supported in C) Pass by address (also supported in C) Pass by reference 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Function examples All examples below are function prototypes Contain information about how to call function Return type, name, and argument list Only arg types required, but good practice to list names No details on operation of function (definition) int f1(); double f2(int x, int y); void f3(int *p1, int *p2); void f4(int &r1, int &r2); What’s the * in f3()? Argument is passed by address What’s the & in f4()? Argument is passed by reference 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Review: pointers Pointer: address of a variable Can get address of existing object using & Can get value of existing pointer using * Can assign pointers of same type to each other Pointer declaration: <base type>* <pointer name> Base type determines how reference is interpreted Use pointers as function arguments pass by address 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Pass by address Pointer(s) as function argument(s) Ex.: p1 and p2 in: void f3(int *p1, int *p2); Function can modify contents of data at address Example: say f3() is swap routine: void f3(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } Function call requires passing pointers: int v1 = 32; int v2 = 20; f3(&v1, &v2); // v1 = 20, v2 = 32 // after f3 is done 5/9/2019 Data Structures: Lecture 4
Pass by reference (C++ only) Similar to pass by address—gives function ability to modify contents of arguments References are aliases of arguments Can refer to argument by name—no need for extra operators Not as obvious function can modify arguments Example: void f4(int &r1, int &r2) { int temp = r1; r1 = r2; r2 = temp; } Function call requires passing pointers: int v1 = 32; int v2 = 20; f4(v1, v2); // v1 = 20, v2 = 32 // after f4 is done 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Function example #include <iostream> using namespace std; double f1(int v1, int v2); void f2(int *ptr1, int *ptr2); void f3(int &ref1, int &ref2); int main() { int foo = 10; int bar = 57; double baz; baz = f1(foo, bar); cout << "After f1(), foo = " << foo << ", bar = " << bar << ", baz = " << baz << "\n"; f2(&foo, &bar); cout << "After f2(), foo = " << foo << ", bar = " << bar << "\n"; f3(foo, bar); cout << "After f3(), foo = " << foo return 0; } double f1(int v1, int v2) { return (v1 + v2) / 2.0; } void f2(int *ptr1, int *ptr2) { while (*ptr1 > 5) { *ptr2 -= 3; (*ptr1)--; void f3(int &ref1, int &ref2) { if (ref1 == 5 && ref2 >= 45) { ref1++; ref2--; else if (ref1 == 5) { ref1--; ref2++; else { ref1 = ref2 - 10; ref2 = ref1 + 10; 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Example output After f1(), foo = 10, bar = 57, baz = 33.5 After f2(), foo = 5, bar = 42 After f3(), foo = 4, bar = 43 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Program 1 basics Refresher on structures Gain familiarity with basic C++ I/O Will use two structures Point: simple point in 2D plane Polygon: collection of Point structures + Number of points Bounding box (min/max X & Y values) (optional) Will write functions to Add Points to a Polygon Display Point and Polygon structures Test if given Point is inside given Polygon 5/9/2019 Data Structures: Lecture 4
Data Structures: Lecture 4 Final notes Next time Alternate input functions I/O manipulators for output formatting C++ strings (time permitting) Reminders: Program 1 due Friday, 2/8 All programs to be submitted via Blackboard Submit a single .zip file containing all files for this assignment 5/9/2019 Data Structures: Lecture 4