Chapter 10 More on Modular Programming and Functions.

Slides:



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

Introduction to C Programming
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Pointers Applications
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
CSCI 171 Presentation 6 Functions and Variable Scope.
MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
1 ICS103 Programming in C Lecture 8: Functions I.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
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.
Windows Programming Lecture 03. Pointers and Arrays.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 8 Arrays, Strings and Pointers
Introduction to Programming Using C
Deitel- C:How to Program (5ed)
C Basics.
6 Chapter Functions.
Comments, Prototypes, Headers & Multiple Source Files
Local Variables, Global Variables and Variable Scope
A First Book of ANSI C Fourth Edition
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
C Programming Lecture-8 Pointers and Memory Management
Functions Reasons Concepts Passing arguments to a function
Introduction to Pointers
Presentation transcript:

Chapter 10 More on Modular Programming and Functions

10.1 INTRODUCTION For a function that must return multiple values. We use parameter passing by pointers.

10.2 POINTER VARIABLES A method in C that allows a function to return multiple values through its parameter list is parameter passing by pointers. A variable name is the symbolic address of a memory location, which contains a value. Pointer variables are variables whose values are memory addresses.

Declaring Pointer Variables Associate it with data type such as int, char or double and use the symbol * as its prefix. int margaret; int *point_to_margaret; int *another_pointer;

Initializing Pointer Variables 1. The value NULL 2. An address 3. The value of another pointer variable point_to_margaret =NULL;

With the exception of NULL and 0, no other constant value can be assigned to pointer variables. margaret = 20; The symbol & is the address operator. int *pointer_to_margaret; pointer_to_margaret=&margaret; int *pointer_to_margaret =&margaret

printf(“%d”, *pointer_to_margaret); The symbol * in this context is an operator, known as the indirection operator. another_pointer= pointer_ to_ margaret; both pointer_ to_ margaret and another_pointer point to margaret.

10.3 PARAMETER PASSING BY POINTERS 1.Declare void called_ function ( int *result); 2.In calling called_ function (&value_ returned); 3.In a function prototype void called_ function( int *x);

Example 10.2 Validates the day part of a date

10.4 ADDITIONAL STYLE CONSIDERATIONS FOR MODULAR PROGRAMS 1.While implementing a module as a function, clearly identify data that it must receive and data that it must return. 2.To send data to functions, use parameter passing by value. 3.If a function has one value to return,you may return it. For functions with two or more return values, it is better to use parameter passing by pointers.

10.5 EXAMPLE PROGRAM 1: A C Program that Generates Calendars

10.6 MODULAR PROGRAMMING WITH PROGRAMMER- DEFINED LIBRARIES A library in C is a collection of reasonably general- purpose and related functions.

Program modularization through Programmer-Defined Libraries We can collect such functions in libraries and implement them, then we may use them in other projects, just as we use standard library functions.

Step 1. Identify the reusable modules For example, the functions compute_number_of_days, determine_week_day, get_ year, get_month, get_ day, get_ week_ day, include these six functions in a programmer -defined library named calendar.

Step 2. A programmer - defined library, have only declarations in header files, and only the definitions of the functions in the implementation file. A header file, named calendar.h, and store it in our default directory.

Step 3. The implementation file, contains the following items: 1.The preprocessor directive #include “calendar.h”,enclosed by double quotations. 2.include preprocessor directives for all standard libraries needed #include 3.The definitions for the functions.

Step 4. A function main will consist of these items: 1.The preprocessor directive #include “calendar.h”. 2.The include preprocessor directives for the standard libraries. 3.The function definitions.

10.1 INTRODUCTION