Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Structure of a C program
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Using Abstraction to Manage Complexity Abstraction: procedural abstraction & data abstraction. Procedural abstraction=> function development should separate.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…)
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Guide To UNIX Using Linux Third Edition
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
chap13 Chapter 13 Programming in the Large.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 13: Programming in the Large Problem Solving & Program Design.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
2Object-Oriented Program Development Using C++ 3 Function Declarations and Definitions Analogize functions to methods –Share same basic features –Unlike.
A First Book of ANSI C Fourth Edition
Chapter 12 Text and Binary File Processing Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
CISC105 – General Computer Science
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
Top-Down Design with Functions
Presentation transcript:

Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-2 Outline 13.1 USING ABSRACTION TO MANAGE COMPLEXITY 13.2 PERSONAL LIBRARIES: HEADER FILES 13.3 PERSONAL LIBRARIES: IMPLEMENTATION FILES 13.4 STORAGE CLASSES 13.5 MODIFYING FUNCTIONS FOR INCLUSION IN A LIBRARY 13.6 CONDITIONAL COMPILATION 13.7 ARGUMENTS TO FUNCTION MAIN 13.8 DEFINING MACROS WITH PARAMETERS 13.9 COMMON PROGRAMMING ERRORS

Copyright ©2004 Pearson Addison-Wesley. All rights reserved USING ABSRACTION TO MANAGE COMPLEXITY procedural abstraction –separation of what a function does from the details of how the function accomplishes its purpose data abstraction –separation of the logical view of a data object (what is stored) from the physical view (how the information is stored) information hiding –protecting the implementation details of a lower-level module from direct access by a higher-level module encapsulate –packaging as a unit a data object and its operators

Copyright ©2004 Pearson Addison-Wesley. All rights reserved PERSONAL LIBRARIES: HEADER FILES header file –text file containing the interface information about a library needed by a compiler to translate a program system that uses the library or by a person to understand and use the library

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-5 Figure 13.1 Preparing a Program for Execution

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-6 HEADER FILES Typical contents of a header file –a block comment summarizing the library’s purpose –#define directives naming constant macros –type definitions –block comments stating the purpose of each library function and declarations of the form extern prototype

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-7 Figure 13.2 Header File planet.h for Personal Library with Data Type and Associated Functions

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-8 Figure 13.2 Header File planet.h for Personal Library with Data Type and Associated Functions

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-9 Figure 13.3 Portion of Program That Uses Functions from a Personal Library

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Cautionary Notes for Header File Design Notice that the constant macro defined has a long name that begins with the library name –This naming strategy reduces the likelihood that the name associated with a constant in the header file will conflict with other constant macro names in the program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved PERSONAL LIBRARIES: IMPLEMENTATION FILES implementation file –file containing the C source code of a library’s functions and any other information needed for compilation of these functions The elements of an implementation file –a block comment summarizing the library’s purpose –#include directives for this library’s header file and for other libraries used by the functions in this library –#define directives naming constant macros used only inside this library –type definitions used only inside this library –function definitions including the usual comments

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 13.4 Implementation File planet.c Containing Library with Planet Data Type and Operators

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 13.4 Implementation File planet.c Containing Library with Planet Data Type and Operators (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Using a Personal Library Creation –C1 Create a header file containing the interface information for a program needing the library –C2 Create an implementation file containing the code of the library functions and other details of the implementation that are hidden from the user program –C3 Compile the implementation file. This step must be repeated any time either the header file or the implementation file is revised Use –U1 Include the library’s header file in the user program through an #include directive –U2 After compiling the user program, include both its object file and the object file created in C3 in the command that activates the linker

Copyright ©2004 Pearson Addison-Wesley. All rights reserved STORAGE CLASSES auto –default storage class of function parameters and local variables; storage is automatically allocated on the stack at the time of a function call and deallocated when the function returns extern –storage class of names known to the linker

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 13.5 Storage Classes auto and extern as Previously Seen

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 13.5 Storage Classes auto and extern as Previously Seen (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Global Variables global variable –a variable that may be accessed by many functions in a program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 13.7 Use of Variables of Storage Class extern

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Storage Classes static and register static –storage class of variables allocated only once, prior to program execution register –storage class of automatic variables that the programmer would like to have stored in registers

Copyright ©2004 Pearson Addison-Wesley. All rights reserved MODIFYING FUNCTIONS FOR INCLUSION IN A LIBRARY In previous work, functions have dealt with an error either by returning an error code or by displaying an error message and returning a value that should permit continued execution C’s exit function from the standard library stdlib can be used in these types of situations to terminate execution prematurely

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 13.8 Function factorial with Premature Exit on Negative Data

Copyright ©2004 Pearson Addison-Wesley. All rights reserved exit Function (1/2) SYNTAX exit (return_value); EXAMPLE /* * Gets next positive number from input stream. * Returns EOF if end of file is encountered. * Exits program with error message if erroneous * input is encountered. */

Copyright ©2004 Pearson Addison-Wesley. All rights reserved exit Function (2/2) int get_positive(void) { int n, status; char ch; for (status=scanf(“%d”,&n); status ==1 && n<=0; status=scanf(“%d”,&n)) { } if (status==0){ scanf(“%c”, &ch); printf(“\n***Function get_positive”); printf(“reports ERROR in data at”); printf(“>>%c<<***\n”, ch); exit(1); } else if (status==EOF) return (status); else return (n); }

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CONDITIONAL COMPILATION C’s preprocessor recognizes commands that allow the user to select parts of a program to be compiled and parts to be omitted. Conditional compilation allows one to compile only the code appropriate for the current computer Figure 13.9 shows a recursive function containing printf calls to create a trace of its execution –Complication of these statements depends on the value of the condition defined (TRACE)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 13.9 Conditional Compilation of Tracing printf Calls

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Conditional Compilation of Tracing printf Calls

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Header File That Protects Itself from Effects of Duplicate Inclusion

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Header File That Protects Itself from Effects of Duplicate Inclusion (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Header File That Protects Itself from Effects of Duplicate Inclusion (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved ARGUMENTS TO FUNCTION MAIN command line arguments –options specified in the statement that activates a program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure File Backup Using Arguments to Function main

Copyright ©2004 Pearson Addison-Wesley. All rights reserved DEFINING MACROS WITH PARAMETERS macro –facility for naming a commonly used statement or operation –#define macro_name(parameter list) macro body macro expansion –process of replacing a macro call by its meaning

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Program Using a Macro with Formal Parameters

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Macro Expansion of Second Macro Call of Program in Fig

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Use of Parentheses in Macro Body The use of adequate parentheses in a macro’s body is essential for correct evaluation. In Fig , a program fragment uses a macro to compute n 2. –Two versions of the macro definition and the different program outputs that result.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Macro Calls Showing Importance of Parentheses in Macro Body

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Macro Calls Showing Importance of Parentheses in Macro Body (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Macro Expansions of Macro Calls from Fig

Copyright ©2004 Pearson Addison-Wesley. All rights reserved COMMON PROGRAMMING ERRORS The most common problem in the development of large systems by terms of programmers is a lack of agreement regarding the details of a system’s design. When developing personal libraries, it is easy to forget the long-range goal of having reusable functions in the rush of completing a current project. It is easy to slip and type a blank after the macro name in the definition of a macro with parameters, causing the preprocessor to misinterpret the definition.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Chapter Review C’s facility for creating a personal library provides a means of encapsulating an abstract data type. Dividing a library definition into a header file and an implementation file provides a natural separation of the description of what the library functions do from how they do it. The exit function allows premature termination of program execution Conditional compilation provides a means of customizing code for different implementations and of creating library header file that protect themselves from duplicate inclusion.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Question?