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 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.
10.5 EXAMPLE PROGRAM 1: A C Program that Generates Ca
lendars
10.6 MODULAR PROGRAMMING WITH PROGRAMMER- DEFINED LI
BRARIES
• 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 <stdio.h>
• 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