Presentation is loading. Please wait.

Presentation is loading. Please wait.

Returning Structures Lesson xx

Similar presentations


Presentation on theme: "Returning Structures Lesson xx"— Presentation transcript:

1 Returning Structures Lesson xx
In this module, we’ll talk about returning structures from functions.

2 Objectives Review return value from a function Returning structures
Program returning a structure Our goal is to review returning a value from a functions. Then, we’ll talk about returning a structure from a function. Lastly we’ll write a program to demonstrate another use of structures.

3 Passing Arguments int ans = compute ( z ); int compute ( float zz) {
. . . return a; } Let’s review functions. The statement in red is how you call a function that returns a value. On the left hand side of the = sign, you put a variable with the same data type as the return type of the function. Since function compute returns an int, the variable ans must be an int. On the right hand side of the = sign, you put the name of the function followed by the argument list in ( )s. In this case, we are calling function compute() and sending in z. Notice that z should be a float. The header of the function is: int compute (float zz). int means that the function returns a number that is an integer. compute is the name of the function. float zz means that we have 1 floating point argument that is sent in.

4 Returning Structures struct student { long id; int age; char sex; };
int main ( ) student s; . . . s = fun (3); } student fun (int n) student found; return found; Here is an example of how you return a structure from a function. We have defined a structure called student at the beginning of our code. In order to declare a structure variable, we write student s; in main(). We can call function fun and pass in the #3 as an argument. When we get into the function, 3 is stored in the local variable n. At the end of function fun(), we have the statement return found; Notice that the data type of found is a structure of the type student.

5 Why Return a Structure ? struct student { long id; int age; char sex;
}; int main ( ) student s; . . . s = fun (3); } student fun (int n) student found; return found; Let’s look at the same example again. Why would you want to return a structure? Suppose the purpose of function fun() is to lookup student record n. In other words if we send in the # 3 for n, function fun() will return the id, age and sex of student #3. One way to get this information back is to return 1 structure that contains 3 parts, the id, age, and sex.

6 Program Specifications
Write a program that will: Read in a start time and end time in the form hh mm. 2. Calculate and print the total time worked. Let’s now write a program that demonstrates returning a structure. We’ll read in a starting and ending time in the form hh mm. Then, we’ll calculate and print the total time worked.

7 Program Code Part 1 #include <iostream> using std::cin; using std::cout; using std::endl; struct time { int hr; int min; }; int main() time start, end, hoursworked; int i; time calc_hrs (time s, time e); The first part of the program contain the preprocessor directives, the structure definition and other declarations.

8 Program Code Part 2 cout << "enter starting time in the form hh mm "; cin >>start.hr >>start.min; cout << "enter ending time "; cin >>end.hr >>end.min; hoursworked = calc_hrs (start, end); cout << "hours worked = " << hoursworked.hr << " minutes = “ << hoursworked.min; return 0; } Part 2 of the code does the input, calculations and the output.

9 Program Code – Part 3 /**************function to calculate number of hrs worked******/ struct time calc_hrs (struct time s, struct time e) { struct time hw; if (s.hr > e.hr) hw.hr = 24-s.hr +e.hr; /*s = e = 0115*/ else hw.hr = e.hr - s.hr; /*s = e = 0315*/ if (e.min < s.min ) /*s = e = 1509*/ hw.min = 60 - s.min + e.min; hw.hr--; } hw.min = e.min - s.min; /*s = e = 1413*/ return (hw); Part 3 is a listing of the calc_hr ( ) function which calculates the time worked. Now, let’s go through the program in detail.

10 Structure Definition struct time { int hr; int min; };
Our goal in this program is to read in a starting time, an ending time and then calculate the time worked. For example, the person might start work at 9:32 and work until 11:35. In this case the person worked 2 hr and 3 mins. Each of those times contains an hour and a minute. Our structure called time allows us to group these items together and think of a time as 1 object rather than 2 separate items.

11 Declarations int main() { time start, end, hoursworked; int i;
time calc_hrs (time s, time e); end.hr end end.min start.hr start.min start hoursworked.hr hoursworked hoursworked.min In main(), we have declared 3 date variables called start, end and hoursworked. We have a prototype for function calc_hrs() which calculates the time a person worked. Shown at the bottom of the slide is a picture of the structure variables which are sometimes called objects. We can say that we have 3 objects of the type time.

12 Inputs cout << "enter starting time in the form hh mm ";
cin >>start.hr >>start.min; cout << "enter ending time "; cin >>end.hr >>end.min; 9 32 start.hr start.min start 11 35 end.hr end end.min hoursworked.hr hoursworked hoursworked.min These lines of code prompt the user for the starting and ending time. If he user enters 9:32 and 11:35 for the starting and ending time respectively, the memory looks as shown. Start.hr has 9 and start.min has 32. end.hr has 11 and end.min has 35. hoursworked is uninitialized.

13 Function Call hoursworked = calc_hrs (start, end);
This statement is the call to the calc_hrs () function which calculates the time worked. We pass in the starting and ending time which were read into start and end. Notice that on the left side of the = sign, we have a structure variable of the form time. calc_ hrs() returns a structure, hoursworked.hr will have the # of hours the person worked and hoursworked.min will have the # of minutes.

14 Print Time Worked << " minutes = “ << hoursworked.min;
cout << "hours worked = " << hoursworked.hr << " minutes = “ << hoursworked.min; When we return from the function, all we have to do is to print hoursworked.hr and hoursworked.min to obtain our answer.

15 calc_hrs ( ) Function time calc_hrs (time s, time e) { struct time hw;
hoursworked = calc_hrs (start, end); time calc_hrs (time s, time e) { struct time hw; We get to the calc_hrs() function from main() with the call statement in red. Into the calc_hrs() function, we send 2 objects which contain the start time and end time. After we execute the 1st line of code in the function, the memory of the computer looks as shown: we have the objects s, e and hw. s and e contain the information passed in from start and end respectively. hw is a local variable which will hold the time worked. 11 35 e.hr e e.min 9 32 s.hr s.min s hw.hr hw hw.min

16 Calculate Hours Worked
if (s.hr > e.hr) hw.hr = 24-s.hr +e.hr; /*s = e = 0115*/ else hw.hr = e.hr - s.hr; /*s = e = 0315*/ This is the code in the function that calculates the # hours worked. In the if statement, we check to see if the starting hour is > the ending hour. If it is, this implies that the person started work one day and finished on the next day. In either case, hw.hr contains the # of hours the person worked

17 Calculate Minutes Worked
if (e.min < s.min ) /*s = e = 1509*/ { hw.min = 60 - s.min + e.min; hw.hr--; } else hw.min = e.min - s.min; /*s = e = 1413*/ This part of the code calculates the # of minutes the person worked. Normally, you would take the ending minutes and subtract the starting minutes but, we have to account for the situation where the ending minutes are < the starting minutes. When we finish this code, hw.min contains the # of minutes the person worked.

18 Returning the Time Worked
return (hw); 11 35 e.hr e e.min 9 32 s.hr s.min s 2 3 hw.hr hw hw.min The object hw looks like the picture drawn if the person started work at 9:32 and ended at 11:35. hw.hr has the # 2 and hw.min has the # 3 The last statement of the calc_hrs() function is return hw; This will send a copy of the of hw back to main(). In the next slide, we’ll show you the passing and returning mechanism of a function.

19 Passing & Returning Mechanism
hoursworked = calc_hrs (start, end); time calc_hrs (time s, time e) { struct time hw; . . . return hw; } The statement in red is how we call the function. The variables start and end are passed by value in to the local variable s and e. In the function, we have a local variable called hw where we store the hours and minutes the person worked. Remember, local objects and variables are destroyed upon completion of a function, therefore, we have the statement: return hw; which returns a copy of hw in to the variable hoursworked in main() 11 35 e.hr e e.min 9 32 s.hr s.min s 2 3 hw.hr hw hw.min

20 Why Return a Structure time calc_hrs (time s, time e) {
struct time hw; . . . return hw; } Let’s talk about why you might want to return a structure. In a function, you can only return 1 item. Well, what if you need to return several items like in our case, you need to return the hours and minutes worked. One way to solve the problem is to return a structure that contains several parts as we have shown you in this example.

21 Summary Review return value from a function Returning structures
Program returning a structure In this module, we reviewed return value from a function. We discussed returning structures from functions and finally we wrote a program where we returned a structure from a function.


Download ppt "Returning Structures Lesson xx"

Similar presentations


Ads by Google