chapter 10 more on modular programming and functions
Post on 20-Dec-2015
224 views
TRANSCRIPT
![Page 1: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/1.jpg)
Chapter 10
• More on Modular Programming and Functions
![Page 2: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/4.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/6.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/7.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/9.jpg)
• Example 10.2
• Validates the day part of a date
![Page 10: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/11.jpg)
10.5 EXAMPLE PROGRAM 1: A C Program that Generates Ca
lendars
![Page 12: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/14.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/15.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/16.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/17.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/18.jpg)
![Page 19: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/19.jpg)
![Page 20: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/20.jpg)
![Page 21: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/21.jpg)
![Page 22: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/22.jpg)
![Page 23: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/23.jpg)
![Page 24: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/24.jpg)
![Page 25: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/25.jpg)
![Page 26: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/26.jpg)
![Page 27: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/27.jpg)
![Page 28: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/28.jpg)
![Page 29: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/29.jpg)
![Page 30: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/30.jpg)
![Page 31: Chapter 10 More on Modular Programming and Functions](https://reader036.vdocument.in/reader036/viewer/2022062313/56649d4d5503460f94a2c83e/html5/thumbnails/31.jpg)
10.1 INTRODUCTION