{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

1_Review_4-1

# 1_Review_4-1 - CIS 15AG 1 Review Chapter 4 Functions...

This preview shows pages 1–3. Sign up to view the full content.

CIS 15AG Review Chapter 4: Functions Functions in C are more general than mathematical functions; they can be viewed as small programs on their own. A function receives zero or more pieces of data, operates on them, and returns at most one piece of data. It can also have side effects. There are two categories of functions: 1. Defined by programmers 2. Already defined and collected in libraries, to be used by programmers Standard Functions – C has a rich collection of standard functions. A few of them are listed below: stdlib.h int abs ( int ); // absolute value math.h double fabs ( double ); // absolute value for double math.h double ceil ( double ); // ceil(2.2) is 3.0 math.h double floor( double ); // floor(2.2) is 2.0 math.h double sqrt ( double ); // square root math.h double pow ( double, double ); // power: pow(2, 5) is 32.0 stdlib.h int rand ( void ); // returns a random positive integer stdlib.h void srand( unsigned int ); // initializes the seed of the // random number generator; usually used as shown below: srand( time(NULL) ); // time.h #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> int main( void ) { int a = -25; double b = -25.12; srand( time(NULL) ); printf("%d", abs ( a ) ); // 25 printf("%.2f", fabs ( b ) ); // 25.12 printf("%.2f", ceil ( b ) ); // -24.00 printf("%.2f", floor ( b ) ); // -25.00 printf("%.2f", sqrt ( abs(a) ); // 5.00 printf("%.2f", pow ( a, 2 ); // 625.00 printf("%d", rand () % 5; // 0, 1, 2, 3 or 4 return 0; } User Defined Functions Why do we need them? 1.To define functions not defined in libraries. 2.To avoid writing repeated fragments of code. 3.To break a complex problem into smaller, more manageable problems. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CIS 15AG Review Chapter 4: Functions Top-down design – when a program is divided into a main module and its related modules . Each module is in turn divided into sub-modules until the resulting modules are implicitly understood without further division. // page 150 Structure Chart – visual representation of the modules – Reading a structure chart: top down, and left right. Calling module : – if it has sub-modules Called module : – if it is a sub-module. – Each module is designed to do a specific task – Modules in C are written as functions. – No communication can take place directly between modules that do not have a calling-called relationship.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}