# Modularity using Functions by gjjur4356

VIEWS: 7 PAGES: 11

• pg 1
```									Modularity using Functions

Chapter 4
Modularity
• In programming blocks of code often can be
"called up" and reused whenever necessary, for
example code to calculate GST and PST, or code
to validate inputs or code to convert values (eg
from miles to kilometers).
• A block of reusable code placed in a separate unit
is called a function.
• This reusable code can then be called from
another function.
• main is actually a function itself.
• Programs become more modular and are easier to
maintain through the use of functions.
/* Program to input values between 0-20 for number of large, medium and
small coffees sold and calculate total cost of coffees. */
main ()
{        int large, medium, small;
double cost;
printf ("Enter number of large coffee (0-20): ");
scanf ("%d", &large);
while (large < 0 || large > 20)
{         printf ("0-20 only. Please try again\n");
scanf ("%d", &large);     }
printf ("Enter number of medium coffee (0-20): ");
scanf ("%d", &medium);
while (medium < 0 || medium > 20)
{         printf ("0-20 only. Please try again\n");
scanf ("%d", &medium); }
printf ("Enter number of small coffee (0-20): ");
scanf ("%d", &small);
while (small < 0 || small > 20)
{         printf ("0-20 only. Please try again\n");
scanf ("%d", &small); }
cost = (large * 1.80) + (medium * 1.50) + (small * 1.10);
printf ("The cost for coffee is: \$%.2lf\n", cost);  }
/* Program to input values between 0-20 for number of large, medium and
small coffees sold and calculate total cost of coffees. Uses function called
GetNumber to validate input is between 0-20 */
int GetNumber (char size);                  /* function prototype */
main ()
{       int large, medium, small;
double cost;
large       = GetNumber('L');                     /* function call */
medium = GetNumber('M');                    /* function call */
small      = GetNumber('S');                /* function call */
cost = (large * 1.80) + (medium * 1.50) + (small * 1.10);
printf ("The cost for coffee is: \$%.2lf\n", cost);
}
int GetNumber (char size)                    /* function heading */
{       int num;                              /* local variable */
printf ("Enter number of %c coffee (0-20): ", size);
scanf ("%d", &num);
while (num < 0 || num > 20)
{         printf ("0-20 only. Try again. # of %c coffee:\n",size);
scanf ("%d", &num);
}
return num;                            /* return num to main */ }
Benefits of Modularity
• Less coding required  fewer syntax and
logic errors
• Easier to test
• Easier to understand
• Function can be reused by other programs
Functions

There are 4 elements of a function:
• function body
• function call
• function prototype
A function heading defines three things:
1) the type of value to be returned from the
function
2) the name of the function (the same rules
apply here as for naming variables)
3) the parameter list (the type and name
of the values to be passed into the
function)
Example: int GetNumber (char size)
Function Body
• The function body is placed just after the function
• Function body is enclosed in brace brackets and
contains the code for the function task.
• A function may declare some local variables.
• A function may return only a single value of the type
• Example:
int num;                             /* local variable */
printf ("Enter number of %c coffee (0-20): ", size);
scanf ("%d", &num);
while (num < 0 || num > 20)
{         printf ("0-20 only. Please try again\n");
scanf ("%d", &num);
}
return num;
Function Call
• A function can be called from main or from another
function.
• To call a function, use the name of the function
and a list of arguments (separated by commas)
that are being sent to the function.
• The arguments in the function call must match the
data type and order of the arguments shown in the
parameter list of the function heading.
• If the function returns a value which you want to
store, then you would use an assignment
statement when calling the function.For example:
large = GetNumber('L');
Function Prototype
• Functions may be written above main but most
programs place all functions below main so it is
easy to find the first line that will be executed in the
program (execution always begins with the first
line in main).
• When functions are placed below main, a
function prototype must appear above main.
• The function prototype allows C to find and report
any illegal data type conversions (for example,
sending a double to a function where an integer is
expected).
• Think of the function prototype as the function
declaration
• Example: int GetNumber (char size);
Modularity Guidelines
• Each function should perform a single task (for
example: calculate the volume, or ask the user to
input a mark, or print an invoice) and should be
given a name that reflects that task (for example:
CalcVolume or GetMark or PrintInvoice)
• Each function should be a manageable length
(one page or one screen in length as a guide).
Note: this also includes the main function.
• The number of parameters in a function heading
should be reasonable (5 to 10 as a guide)
• If one function is too long, too complex, or has
too many parameters, perhaps you need to
subdivide it into several functions.

```
To top