+ All Categories
Home > Documents > 1 Modularity In “C”. 2 What is Modularity? Modularity, is the heart of the high level,...

1 Modularity In “C”. 2 What is Modularity? Modularity, is the heart of the high level,...

Date post: 20-Dec-2015
Category:
View: 217 times
Download: 1 times
Share this document with a friend
22
1 Modularity In “C”
Transcript
Page 1: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

1

ModularityIn “C”

Page 2: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

2

What is Modularity? Modularity, is the heart of the high level, structured

languages. Means breaking down a big problem into smaller

parts, and writing code for each part. In C, modularity is implemented by using Functions.

Page 3: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

3

Advantages: Reduces complexity, Adds to clarity of the program, Provides reusability, Reduces redundant code, Makes it easier to maintain and modify the code in

future.

Page 4: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

4

Modularity in C: Modularity is accomplished by functions in “C”. A “C” program is a group of one or more functions. A “C” program MUST have at least one function

named main(). In C, execution begins with the main() function. There is a rich collection of built in functions that are

grouped in the “C” standard libraries.

Page 5: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

5

What is a function? Is a segment of code that accomplishes some task. It has a name, ex: pow. It has a body, which is “C” statements enclosed in

{ }. It may require 0 or more information (input(s)), It normally generates one result (output).

Inputs OutputFunction XXX

Page 6: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

6

Built in functions:

Include the proper header file in your C program.

Ex: scanf():

Is a built in function, #include <stdio.h> Requires 2 or more input

printf()

Page 7: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

7

math.h Appendix-B

To use the math library functions: #include <math.h>Ex: pow (12, 2) 144 sqrt (100) 10 abs (-23) 23

Page 8: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

8

Writing your own functions: Do not do it if there is a built in function.

Page 9: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

9

General Syntaxdata_type function_Name (parameter list)

{…return expression; // return output

}

Body of the function is a compound statement

Page 10: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

10

Parameter list:

A function gets the input data through its parameters.

Each parameter is a pair of: data type name

Input output

parametersFunction …

////

Page 11: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

11

Reture_typeReturn_type function_Name (parameter list)

{ …return expression; // return this exp.

}

Is the data type of the function’s result (output).

Page 12: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

12

return statement:Return_type function_Name (parameter list)

{…return exp; // return output

} A function sends out its result (output) via return

statement. Function’s output must be of the same data type as

Return_type .

Page 13: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

13

Cont… return expression:

expression can be a variable name, expression can be an expression that

evaluates to the proper data type.

Input outputreturn …

////

Page 14: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

14

Example-1: Write a function to display your information

to the screen: Think about:

Function’s name: display_myinfo Required input: none Produced output: none Task: To display: name and ssno to standard

output.

Page 15: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

15

void Means nothing! Use keyword void to indicate:

no input no output

Page 16: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

16

Write the code:void display_myinfo ()

{ printf (“Programmer: Good student \n”);

printf (“ID: 123-23-2323 \n”);

printf (“Course: CIT105 \n”);

}

Page 17: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

17

Invoke the function display_myinfo from main(): main() {

display_myinfo();}

See computer demo

Page 18: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

18

Example-2:

Write a function to compute the volume of a square box: Function name? Input data? Generated output?

Input? Output?

Page 19: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

19

Develop an algorithm:1) Declare variables2) Compute the volumne3) Return the result.

// See computer demo

Page 20: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

20

How to use a function? You must invoke it.

Specify its name, Send required data in, Store the produced output in a variable.

Function invocation appears either: On the right hand side of an assignment statement, Or can be part of an expression.

Page 21: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

21

Ex: Write a C program and use your function to compute the volume of a cube:#include <stdio.h>

float computeVolume (float side){ float volume; volume = side * side * side; return volume; }

Dummy name

Page 22: 1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.

22

// main() function: main(){ float length, vol; printf(“ Enter side of the cube:”); scanf (“%f”, &length);

// Invoke the function, store the result in vol: vol = computeVolume( length);

// display the output: print f(“volume is = %f “, vol);}


Recommended