L3 - Functions - 4
L3 - Functions - 4
L3 - Functions - 4
Functions
Functions
basic idea
execution flow
prototype and declaration
scoping rules
C Standard Library
Math Library
Example 1
4
1
4
3
4
5
4
7
4
9
.........
(PI 4/1)
3. Add 4/5 to PI
(PI PI + 4/5)
(PI PI 4/7)
5.
[ CS1010E AY1112S1 Lecture 3 ]
Example 1(contd)
PI Algorithm
even terms are
negative
4
1
4
3
4
5
4
7
4
9
.........
Denominator increases
by 2 every term
1. Initialize PI 4
2. Denom 3
3 nTerm 2
3.
4. while nTerm is <= 1000
i. Calculate new term
ii. Update PI
iii. nTerm nTerm + 1
5. Show PI
odd terms
e sa
are
e
positive
Example 2: Anagrams
a If nTerm is even
a.
PI PI Term
b. If nTerm is odd
PI PI + Term
refines
a. Term 4 / Denom
How
o do you de
determine
e
e if two
o words
o ds X a
and
dY
are anagrams?
Functions
1.
If it is not p
possible, break it down further to simpler
p
sub-problems
Duplicated coding:
2.
10
Given:
R = radius of the plate
Ra, Rb and Rc = radii
of the circles a, b and
c respectively
Duplicate coding!
11
12
Function: Syntax
function header
Visualization:
input
Function
SY
YNTAX
output
Example:
function body
sum
12
13
Function Header
Function header indicates:
14
sum
12
SYNTAX
sum gives
integer result
Th usuall identifier
The
id tifi naming
i rule
l applies
li
15
16
void print_info( )
void print_info( void )
17
Some Functions:
18
Functions in a Program
//Preprocessor directive not shown
result = x + y;
return result;
result = x + y;
return result;
void print_info( )
{
int main( )
{
int input1, input2;
return;
}
}
[ CS1010E AY1112S1 Lecture 3 ]
19
20
fformall parameter
t
3.
int result;
result = x + y;
return result;
4.
Effectively:
int
i t x = 3;
3
int y = 9;
}
sum( 3, 9 );
int main( )
{
i t input1,
int
i
t1 i
input2,
t2 output;
t t
1.
actual arguments
//read inputs
2.
2
5.
6.
}
[ CS1010E AY1112S1 Lecture 3 ]
21
22
is a single value
essentially replaces the function call and can be
used in normal arithmetic operations and
assignment
result = 12 + sum( 5, 2 );
//calculate area of plate
result = 12 + 7;
//print areaPlate not shown
return 0;
result = 19;
[ CS1010E AY1112S1 Lecture 3 ]
}
23
24
It is important to realise:
int
i
t main(
i ( )
{
int x, y;
}
Actual coding
g ((the function body)
y) is not essential
to the user
25
Exa
ample
Portability:
27
You only need to show user the prototypes, not the full
coding
Ease of Maintenance:
Ensure the
E
th prototype
t t
is
i declared
d l db
before
f
any actual
t l
function call in the program
Separation
S
ti off function
f
ti prototype
t t
and
d definition
d fi iti
allows:
Usage:
26
Function Definition
Note:
Function name
Number of input parameter and data type
Output data type
Description of function
28
Example
p
prototype
int sum( int, int );
// sum() takes two integer X, Y
// and return the sum of X + Y
int main( )
{
.........
output = sum( input1, input2 );
.........
function call
}
int sum( int x, int y )
{
definition
int result;
result = x + y;
return result;
}
[ CS1010E AY1112S1 Lecture 3 ]
29
Relyy only
y on its inputs
p
to p
produce the output
p
Perform one task only
be reusable in your program and across programs
P
Program
Ab
Abstraction
t
ti
30
31
32
C Library
known as modularity
Is everything
y
g in main()
() ?
33
C Standard Library
Library:
A collection of functions
P t t
Prototypes
are organized
i d into
i t a header
h d file
fil (XXXX.h)
(
h)
Provides great portability and reusability:
examples
C Standard Library:
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
Input / Output
Mathematical Functions
others ( more later )
35
36
Comp
pilation
HEA
ADER
Power Function
Prototyp
pe
#include <math.h>
Ex
xample
Ex
xample
Prototyp
pe
HEA
ADER
double result;
result = sqrt( 4
4.0
0 );
//result = 2
2.0
0
37
A lot more
double result;
result = pow( 2
2.0,
0 4
4.0
0 );
//result = 16
16.0
0
38
Trigonometry functions:
#include <math.h>
Logarithm functions:
C ili
Ceiling,
Fl
Floor, Ab
Absolution
l i value
l etc.
Reminder:
39
40
C Elementts
Summary
Reference
Function
- Header and Body
- Function call
- Prototype and Definition
Chapter
p 3
Library
- Introduction to Math Library
Pro
ogramming Style
Online C reference:
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
41
42