Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Modularity In “C”

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

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

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

5 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). InputsOutput Function XXX

6 6 Built in functions:  Include the proper header file in your C program.  Ex:  scanf(): Is a built in function, #include Requires 2 or more input  printf()

7 7 math.h Appendix-B  To use the math library functions: #include Ex:  pow (12, 2)  144  sqrt (100)  10  abs (-23)  23

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

9 9 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound statement

10 10 Parameter list:  A function gets the input data through its parameters.  Each parameter is a pair of: data type name Input output parameters Function … ////////

11 11 Reture_type Return_type function_Name (parameter list) {…{… return expression; // return this exp. }  Is the data type of the function’s result (output).

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

13 13  Cont… return expression:  expression can be a variable name,  expression can be an expression that evaluates to the proper data type. Inputoutput return … ////////

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

15 15 void  Means nothing!  Use keyword void to indicate: no input no output

16 16 Write the code: void display_myinfo () { printf (“Programmer: Good student \n”); printf (“ID: 123-23-2323 \n”); printf (“Course: CIT105 \n”); }

17 17 Invoke the function display_myinfo from main(): main() { display_myinfo(); } See computer demo

18 18 Example-2:  Write a function to compute the volume of a square box: Function name? Input data? Generated output ? Input? Output?

19 19 Develop an algorithm: 1) Declare variables 2) Compute the volumne 3) Return the result. // See computer demo

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

21 21 Ex: Write a C program and use your function to compute the volume of a cube: #include float computeVolume (float side) { float volume; volume = side * side * side; return volume; } Dummy name

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


Download ppt "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."

Similar presentations


Ads by Google