Sie sind auf Seite 1von 27

LESSON 6

FUNCTIONS &
MODULAR PROGRAMMING
Modular Programming
Scenario:
A program need to handle multiple tasks:
Display user menu information
Based on users input, do
Read a file
Write to a file
Calculate salary based on different rates
Calculate income tax based on different rates
Calculate annual leave balance
Handle medical claims
Etc
Etc
Modular Programming
Experience show:
The best way to develop and maintain a large
program is to construct the large program from
pieces or modules
Module - each of which is more manageable than the
original program

Task 1 MASTER
Task 2
Task 3
Task 4 Task Task Task Task
1 2 3 4
Modular Programming in C
C program is made up of one or more
component functions
Exactly one of which must be named main.
Functions
May or May NOT return any value
2 types of function:
Built-in functions
Programmer-defined functions
Built-in Functions
Provided by C library
Function Description Example
sqrt( x ) square root of x sqrt( 900.0 ) is 30.0
sqrt( 9.0 ) is 3.0

Example: exp( x ) exponential function ex exp( 1.0 ) is 2.718282

<stdio.h>
exp( 2.0 ) is 7.389056

log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0


log( 7.389056 ) is 2.0

Input/ Output log10( x ) logarithm of x (base 10) log10( 1.0 ) is 0.0


log10( 10.0 ) is 1.0

functions
log10( 100.0 ) is 2.0
fabs( x ) fabs( 5.0 ) is 5.0

<math.h>
absolute value of x
fabs( 0.0 ) is 0.0
fabs( -5.0 ) is 5.0

C math library
ceil( x ) rounds x to the smallest integer ceil( 9.2 ) is 10.0
ceil( -9.8 ) is -9.0
not less than x

functions floor( x ) rounds x to the largest integer floor( 9.2 ) is 9.0


floor( -9.8 ) is -10.0

<string.h>
not greater than x

pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128.0

String processing
pow( 9, .5 ) is 3.0

fmod( x, y ) remainder of x/y as a floating fmod( 13.657, 2.333 ) is


1.992
functions
point number

<stdlib.h>
sin( x ) trigonometric sine of x sin( 0.0 ) is 0.0
(x in radians)
cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0

Memory allocation, tan( x )


(x in radians)
tan( 0.0 ) is 0.0
random numbers,
trigonometric tangent of x
(x in radians)

other utilities Fig. 5.2 Commonly used math library functions.

functions
Built-in Functions
Example:
Programmer-defined Functions
Functions can be used
To display messages, header information (without
involving process, thus no return value)
Example:
Error message
Menu header
To accept value, for processing and display (no
return value)
To accept value for processing and return value to
calling module.
Example:
A formula accept a variable, calculate, and return the result
Programmer-defined Functions
Function definition:
return-value-type/void function-name(parameter_list)
{
declaration;
statement(s);
Function-name }

user defined / valid identifier


return-value-type
data type of value that need to return to calling
module.
when define a return-value-type indicates that a
function must return a result
void indicate the function does not return any
value
parameter_list
list of values from calling module
Programmer-defined Functions
Example Function
Finding maximum prototype

of three integers.

Parameters Arguments

Return statement
Programmer-defined Functions
Function Prototype
It is important because it tells the compiler
the type of data returned by the function
the number of parameters the function expects to
receive
the types of parameters
the order in which these parameters are expected.
The compiler uses function prototypes to validate
function calls.
Programmer-defined Functions
Arguments vs Parameters
Arguments
Original value of variables or statements passed to the
function.
Parameters
Variable that represents and will receive the values passed by
arguments.
Rules
Number of arguments passed must be the same as number of
parameters
Data type of arguments must match the data type of parameter
Argument and parameter need not have the same name
** When the function is invoked and the data type of an argument is
different from the data type declared for the parameter:-
The system converts the data type of the argument to the data type
declared, if possible.
If the system is unable to concert the data type, and error message is
issued.
Programmer-defined Functions
Return statement
Return statement is optional in a function that
does not return a value
Syntax:
return;
return (expression);
return expression;
A function can return at most one value per
invocation
return(45 * 6 + adds(x));
return 45 * 6 + add1(x);
Variable Scope
Scope
The region of the program in which it is visible. A
variable is visible in a region if it can be
referenced in that region.
Local variables
Variables defined locally within the function.
Global variables
Variables defined outside of any function
Thus are available from any function in the
program.
Variable Scope
NOTES:
If Local variables with the same name as
global variables:
Local variables do not change the global variables
Local variables hides the global variable
If a function has a variable with the same name as a
global variable, the name refers to the local variable
not the global when used within the function.
Variable Scope (Example 1)
Variable Scope (Example 2)

Example
Function Calling
Call by value
A copy of arguments value is made and passed to the called
function.
Changes to the copy do not affect an original variables value to the
caller.
Should be used whenever the called function does not need to modify
the value of the callers original variable.
Function Prototype:
int callbyvalue(int) ;
Call by reference
Given the address of the variable.
The caller actually allow the called function to modify the original
variables value
Function Prototype:
int callbyreference (int *);
By default
all the function in C is call by value.
Call-By-Value vs Call-By-Reference
Example of Call-by-value
Call-By-Value vs Call-By-Reference
Example of Call-by-reference
Call-By-Value vs Call-By-Reference
Example of Call-by-reference(2)
Macro
Defined by #define statement
i.e. single identifiers that are equivalent to
expression, complete statements or group of
statements. Macros resemble functions in this
sense.
Defined in an altogether manner than
functions
They are treated differently during the
compilation process.
Example 1
Example 2
Recursive Functions
A function that calls itself
2 parts of a recursive function
base case to stop the function
recursive step
Recursive Functions
Example 1: Factorial of n, n! = n * (n-1)!
where: 0! = 1
n! = n*(n-1)!
Recursive problem
Other popular recursive problems
Summation
Exponential (xy)
GCD
Fibonacci Series
Additional Exercise
The formula n = 1+2+3++(n-1)+n can be
defined recursively as:
n = 0, when n is zero
n = n+ (n-1), otherwise.
Write a program to print the value of the n formula
for the first 10 positive integers. Your program will
define a recursive function Summation to find and
return the value of the formula. Summation has the
following function prototype:
int Summation (int);

Das könnte Ihnen auch gefallen