Title: Engineering Problem Solving with C Fundamental Concepts
1Engineering Problem Solving with C Fundamental
Concepts
- Chapter 4
- Modular Programming with Functions
2Modularity
3Modularity
- Execution of a program begins in the main
function - The main function can call other functions
- Functions defined in the same file
- Function defined in other files or libraries
- Functions are also referred to as modules
- A module is a set of statements that performs a
task or computes a value
4Advantages of using modules
- Modules can be written and tested separately
- Large projects can be developed in parallel
- Reduces length of program, making it more
readable - Promotes the concept of abstraction
5Programmer Defined Functions
6Functions
- Defined to
- return a single value to the calling function
- perform a task
- change the value of the function arguments (call
by reference)
7Functions
- Pre-defined
- standard libraries
- Programmer defined
8Pre-defined FunctionsExample
include ltstdlib.hgt include ltmath.hgt int
main(void) double angle printf( input
angle in radians \n) scanf(1f, angle)
printf( \nthe sine of the angle is
f\n,sin(angle) ) return 0 //end main
9Programmer Defined FunctionsTerminology
- Function Prototype
- describes how a function is called
- Function Call
- Actual parameter
- used in the function call
- Function Definition
- Formal Parameters
- used in function definition
- Formal parameters must match with actual
parameters in order, number and data type.
10Value Returning Functions
- Function returns a single value to the calling
program - Function definition declares the type of value to
be returned - A return expression statement is required in the
function definition
11Example - factorial function
/function definition n! n(n-1)(n-2)1, 0!
1 by definition - fact returns n! assumes
n is non-negative integer / int fact(int n)
int fact 1 while(ngt1) fact
factn n-- //end while block
return(fact) //end fact
12Function prototype - prototype can be included
with preprocessor directives, or with variable
declarations.
include ltstdlib.hgt int main(void) / Declare
variables and function prototypes. / int n
int fact(int n) printf(Enter a positive
integer\n) scanf("i, n) if(ngt0) printf(
i! is i\n, n, fact(n) ) return
0 Note In this example the function fact is
called in the printf statement.
13Calling a function - the value returned by a
function can be assigned to a variable, printed,
or used in an expression
include ltstdlib.hgt int main() /Declare
variables and function prototypes / int
fact(int) int n, factorial printf(enter
positive integer\n) scanf(lf,n) if(ngt0)
factorial fact(n) printf(i! is
i\n, n , factorial) return 0
14void Functions
- A void function may be called to
- perform a particular task (clear the screen)
- modify data
- perform input and output
- A void function does not return a value to the
calling program - if a return statement is used (no return value)
15Example of void function definition
- void print_date(int mo, int day, int year)
-
- /output formatted date /
- printf(iii\n, mo , day , year )
- return
16Parameter Passing
- Call by value
- formal parameter receives the value of the actual
parameter - function can not change the value of the actual
parameter (arrays are an exception) - Call by reference
- actual parameters are pointers (pointers will be
discussed in chapter 6)
17Storage Class and Scope
- Scope refers to the portion of the program in
which it is valid to reference a function or a
variable - Storage class refers to the lifetime of a variable
18Scope
- Local scope - a local variable is defined within
a function or a block and can be accessed only
within the function or block that defines it - Global scope - a global variable is defined
outside the main function and can be accessed by
any function within the program file.
19Storage Class - 4 types
- automatic - key word auto - default for local
variables - Memory set aside for local variables is not
reserved when the block in which the local
variable was defined is exited. - external - key word extern - used for global
variables - Memory is reserved for a global variable
throughout the execution life of the program. - static - key word static
- Requests that memory for a local variable be
reserved throughout the execution life of the
program. The static storage class does not
affect the scope of the variable. - register - key word register
- Requests that a variable should be placed in a
high speed memory register.