returning structures lesson xx

21
Returning Structures Lesson xx

Upload: maida

Post on 07-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Returning Structures Lesson xx. Objectives. Review return value from a function Returning structures Program returning a structure. Passing Arguments. int ans = compute ( z ); int compute ( float zz ) { int a; . . . return a; }. Returning Structures. struct student - PowerPoint PPT Presentation

TRANSCRIPT

Slide 1

Returning StructuresLesson xx

In this module, well talk about returning structures from functions.1Objectives Review return value from a function Returning structuresProgram returning a structure

Our goal is to review returning a value from a functions. Then, well talk about returning a structure from a function. Lastly well write a program to demonstrate another use of structures.2Passing Argumentsint ans = compute ( z );

int compute ( float zz) { int a; . . . return a; }

Lets 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. 3Returning Structuresstruct 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. 4Why 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; }

Lets 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.5Program SpecificationsWrite 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.

Lets now write a program that demonstrates returning a structure. Well read in a starting and ending time in the form hh mm. Then, well calculate and print the total time worked.6Program Code Part 1#include 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. 7Program Code Part 2 cout >start.hr >>start.min; cout >end.hr >>end.min; hoursworked = calc_hrs (start, end); cout 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 worked16Calculate Minutes Worked if (e.min < s.min ) /*s = 1215 e = 1509*/ { hw.min = 60 - s.min + e.min; hw.hr--; } else hw.min = e.min - s.min; /*s = 1212 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.17Returning the Time Worked1135e.hree.min932s.hrs.mins23hw.hrhwhw.min return (hw);

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, well show you the passing and returning mechanism of a function.18Passing & Returning Mechanism time calc_hrs (time s, time e) { struct time hw; . . . return hw; } hoursworked = calc_hrs (start, end); 1135e.hree.min932s.hrs.mins23hw.hrhwhw.min

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()19Why Return a Structuretime calc_hrs (time s, time e) { struct time hw; . . . return hw; }

Lets 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.20Summary 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.21