(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.

Slides:



Advertisements
Similar presentations
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Advertisements

Introduction to C Programming
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Pointers Applications
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
Review 1 List Data Structure List operations List Implementation Array Linked List.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(3-2) File Processing with Functions Instructor - Andrew S. O’Fallon CptS 121 (September 11, 2015) Washington State University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
Variables and memory addresses
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 7: User-Defined Functions II
UNIT 5 C Pointers.
Pointers.
Pointers.
CNG 140 C Programming (Lecture set 10)
INC 161 , CPE 100 Computer Programming
POINTERS.
User-Defined Functions
Object Oriented Programming COP3330 / CGS5409
IFS410 Advanced Analysis and Design
Chapter 9: Pointers and String
(4 – 2) Introduction to Classes in C++
Presentation transcript:

(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University

C. Hundhausen, A. O’Fallon 2 Functions with Output Parameters (1) In many situations, we would like to define functions that compute more than one value – A function that computes the min and max of numbers – A function that computes the variance and deviation – Many others… Is that possible?

C. Hundhausen, A. O’Fallon 3 Functions with Output Params (2) 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; }

C. Hundhausen, A. O’Fallon 4 Functions with Output Params (3) 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 mydouble

C. Hundhausen, A. O’Fallon 5 Functions with Output Params (4) Then, when function foo is called from main, the foo function's data area becomes active, and the actual parameter values passed to foo are copied to spaces in its memory area: Function main data area 12 myint 'a' mychar mydouble Function foo data area 12 a 'a' b c copied

C. Hundhausen, A. O’Fallon 6 Functions with Output Params (5) Finally, when function foo finishes executing, its memory area disappears, and control returns to the main function. The state of memory is thus as it was prior to the call to foo : Function main data area 12 myint 'a' mychar mydouble

C. Hundhausen, A. O’Fallon 7 Functions with Output Params (6) Since foo 's data area goes away after execution, there's no way for foo to communicate with main through its parameter list. What we'd like is a two-way flow, something like this: Function main data area 12 myint 'a' mychar mydouble Function foo data area 12 a 'a' b c copied

C. Hundhausen, A. O’Fallon 8 Functions with Output Params (7) Let’s look at the definition of a pointer – A variable that stores as its contents the address of another variable We should be able to use these address values to access a variable indirectly Indirect access to these memory locations also will allow for modification to the contents

C. Hundhausen, A. O’Fallon 9 Functions with Output Params (8) We like to visualize pointers with the following: 1000 pointer 42 integer

C. Hundhausen, A. O’Fallon 10 Functions with Output Params (9) In C, we can achieve the effect of output parameters by passing memory addresses (pointers) instead of values. Here's how we could modify the previous code to accomplish this: void foo(int* a, char* b, double* c); int main(void) { int myint; char mychar; double mydoubl; myint = 12; mychar = 'a'; mydouble = 23.45; /* pass memory locations of variables, not vars themselves */ foo(&myint, &mychar, &mydouble); … } void foo(int *a, char *b, double *c) { ++(*a); /* autoincrement the value at memory pointed to by a */ *b = 'd'; /* assign to memory pointed to by b */ *c *= 2; /* assign to memory pointed to by b */ }

C. Hundhausen, A. O’Fallon 11 Functions with Output Params (10) In order to visualize what the previous code is doing, we'll need to number (arbitrarily) the memory locations at which the variable values are stored: Since foo 's parameters contain memory locations and not values, foo can use those memory locations to access, and ultimately to change, the original values in the main function. Such changes are called "side effects". Function main data area 12 myint 'a' mychar mydouble Function foo data area 100 a 200 b 300 c &myint &mychar &mydouble

C. Hundhausen, A. O’Fallon 12 Aside: The Many Faces of * By now, you might find the multiple meanings of the '*' operator confusing. Let's review them. – Meaning one: "multiplication" e.g., 5 * 3 – Meaning two: "pointer to" e.g., char *c; – Meaning three: "follow the pointer" (unary indirection) e.g., *i = 4; – Each of these meanings is markedly different!

More About Pointers (1) We can apply arithmetic operations to pointers – We can increment pointers: ptr++, ++ptr, ptr = ptr + 1, ptr = ptr + n, where n is an integer – We can decrement pointers: ptr--, --ptr, ptr = ptr – 1, ptr = ptr - n C. Hundhausen, A. O’Fallon 13

More About Pointers (2) Let’s declare two pointers as follows: – char *char_ptr = NULL; – int *int_ptr = NULL; Let’s now declare two “regular” variables – char character = ‘A’; – int number = 42; We can now assign the addresses of the “regular” variables to the pointers as follows: – char_ptr = &character; – int_ptr = &number; C. Hundhausen, A. O’Fallon 14

More About Pointers (3) We can indirectly modify the value to which each pointers “points” by dereferencing it: – *char_ptr = ‘B’; // Overwrites the ‘A’ in character – *int_ptr = 25; // Overwrites the 42 in number In the future, we’ll be able to access contigous blocks of memory by using pointer arithmetic!!! C. Hundhausen, A. O’Fallon 15

C. Hundhausen, A. O’Fallon 16 You Try It With a partner, design a function divide that accepts four arguments: – number : an integer input parameter – divisor : an integer input parameter – result : an integer output parameter – remainder : an integer output parameter The function divides number by divisor. The result is placed into the output parameter result, and the remainder is placed into the output parameter remainder. Also show how a main function would call divide.

C. Hundhausen, A. O’Fallon 17 Scope (1) As has been discussed in previous lectures, the scope of an identifier is the region of a program within which the identifier is defined. In general: – #define macro definitions have global scope, meaning that they are defined within the entire source file – A function is visible to all functions defined below it. However, its local variables are visible only to itself.

C. Hundhausen, A. O’Fallon 18 Scope (2) Identify the scope of the identifiers in the following example:

C. Hundhausen, A. O’Fallon 19 Next Lecture… An extended example that includes several functions with input and output parameters – Prepare yourself by studying the extended example in Section 6.6 Debugging techniques Common programming errors

C. Hundhausen, A. O’Fallon 20 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8 th Ed.), Addison- Wesley, 2016 P.J. Deitel & H.M. Deitel, C How to Program (7 th Ed.), Pearson Education, Inc., 2013.

C. Hundhausen, A. O’Fallon 21 Collaborators Chris Hundhausen