Date post: | 20-Dec-2015 |
Category: |
Documents |
View: | 217 times |
Download: | 1 times |
1
ModularityIn “C”
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
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
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
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
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()
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
8
Writing your own functions: Do not do it if there is a built in function.
9
General Syntaxdata_type function_Name (parameter list)
{…return expression; // return output
}
Body of the function is a compound statement
10
Parameter list:
A function gets the input data through its parameters.
Each parameter is a pair of: data type name
Input output
parametersFunction …
////
11
Reture_typeReturn_type function_Name (parameter list)
{ …return expression; // return this exp.
}
Is the data type of the function’s result (output).
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
Cont… return expression:
expression can be a variable name, expression can be an expression that
evaluates to the proper data type.
Input outputreturn …
////
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
void Means nothing! Use keyword void to indicate:
no input no output
16
Write the code:void display_myinfo ()
{ printf (“Programmer: Good student \n”);
printf (“ID: 123-23-2323 \n”);
printf (“Course: CIT105 \n”);
}
17
Invoke the function display_myinfo from main(): main() {
display_myinfo();}
See computer demo
18
Example-2:
Write a function to compute the volume of a square box: Function name? Input data? Generated output?
Input? Output?
19
Develop an algorithm:1) Declare variables2) Compute the volumne3) Return the result.
// See computer demo
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
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
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);}