chapter 10 more on modular programming and functions

31
Chapter 10 • More on Modular Programming and Functions

Post on 20-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 10 More on Modular Programming and Functions

Chapter 10

• More on Modular Programming and Functions

Page 2: 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.

Page 3: Chapter 10 More on Modular Programming and Functions

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.

Page 4: Chapter 10 More on Modular Programming and Functions

• 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;

Page 5: Chapter 10 More on Modular Programming and Functions

Initializing Pointer Variables

• 1. The value NULL

• 2. An address

• 3. The value of another pointer variable

• point_to_margaret =NULL;

Page 6: Chapter 10 More on Modular Programming and Functions

• 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

Page 7: Chapter 10 More on Modular Programming and Functions

• 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.

Page 8: Chapter 10 More on Modular Programming and Functions

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);

Page 9: Chapter 10 More on Modular Programming and Functions

• Example 10.2

• Validates the day part of a date

Page 10: Chapter 10 More on Modular Programming and Functions

10.4 ADDITIONAL STYLE CONSIDERATIONS FOR MODULAR PROG

RAMS

• 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.

Page 11: Chapter 10 More on Modular Programming and Functions

10.5 EXAMPLE PROGRAM 1: A C Program that Generates Ca

lendars

Page 12: Chapter 10 More on Modular Programming and Functions

10.6 MODULAR PROGRAMMING WITH PROGRAMMER- DEFINED LI

BRARIES

• A library in C is a collection of reasonably

• general- purpose and related functions.

Page 13: Chapter 10 More on Modular Programming and 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.

Page 14: Chapter 10 More on Modular Programming and 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.

Page 15: Chapter 10 More on Modular Programming and Functions

• 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.

Page 16: Chapter 10 More on Modular Programming and Functions

• 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 <stdio.h>

• 3.The definitions for the functions.

Page 17: Chapter 10 More on Modular Programming and 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 .

Page 18: Chapter 10 More on Modular Programming and Functions
Page 19: Chapter 10 More on Modular Programming and Functions
Page 20: Chapter 10 More on Modular Programming and Functions
Page 21: Chapter 10 More on Modular Programming and Functions
Page 22: Chapter 10 More on Modular Programming and Functions
Page 23: Chapter 10 More on Modular Programming and Functions
Page 24: Chapter 10 More on Modular Programming and Functions
Page 25: Chapter 10 More on Modular Programming and Functions
Page 26: Chapter 10 More on Modular Programming and Functions
Page 27: Chapter 10 More on Modular Programming and Functions
Page 28: Chapter 10 More on Modular Programming and Functions
Page 29: Chapter 10 More on Modular Programming and Functions
Page 30: Chapter 10 More on Modular Programming and Functions
Page 31: Chapter 10 More on Modular Programming and Functions

10.1 INTRODUCTION