cpts121-7-2 - CptS 121 Fall 09 Lecture 7-2 HK Chapter 6...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CptS 121 L7-2 – 10/7/09 Prof. Chris Hundhausen CptS 121 Fall ‘09 Lecture 7-2 HK Chapter 6: Modular Programming Lecture Outline I. Functions with Output Parameters II. Scope
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 CptS 121 L7-2 – 10/7/09 Prof. Chris Hundhausen Functions with Output Parameters In many situations, we would like to define functions that compute more than one value Example: Previously, we "sold out" when we defined function get_width , which prompts the user for the width of a room, and returns that width. The function had to make the unrealistic assumption that rooms are always square, because we had no way to return multiple parameters! Wouldn't it be nice if we could define a function get_room_dims that prompts the user for the width and length of a room (both doubles), and returns those dimensions? Ideally, function get_room_dims would be able to return two values: length and width Is that possible?
Background image of page 2
3 CptS 121 L7-2 – 10/7/09 Prof. Chris Hundhausen Functions with Output Params (cont.) Yes, it is! Function parameters would appear a promising place to start. But we first need to understand precisely what happens when we call a function with parameters, e.g., void foo (int a, char b, double c); int main (void) { int myint; char mychar; double mydouble; myint = 12; mychar = 'a'; mydouble = 23.45; foo (myint, mychar, mydouble); } void foo (int a, char b, double c) { /* This does random, meaningless stuff */ ++a; b = 'd'; c *= 2; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 CptS 121 L7-2 – 10/7/09 Prof. Chris Hundhausen Functions with Output Params (cont.) Remember that each function has a separate area of memory for storing its local variables and parameters. Each data area exists only when the function is active. Before the call to foo , memory might look something like this: Function main data area 12 myint 'a' mychar 23.45 mydouble
Background image of page 4
CptS 121 L7-2 – 10/7/09 Prof. Chris Hundhausen Functions with Output Params (cont.) Then, when function foo is called from main , the foo
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

cpts121-7-2 - CptS 121 Fall 09 Lecture 7-2 HK Chapter 6...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online