Professional Documents
Culture Documents
Functions
Lecture Overview
Functions
basic idea
execution flow
prototype and declaration
scoping rules
C Standard Library
Math Library
Example 1
4 4 4 4 4
= + + .........
1 3 5 7 9
(PI 4/1)
(PI PI + 4/5)
(PI PI 4/7)
5.
4
Example 1(contd)
even terms are
negative
4 4 4 4 4
= + + .........
1 3 5 7 9
Denominator increases
by 2 every term
PI Algorithm
1. Initialize PI 4
2. Denom 3
3. nTerm 2
4. while nTerm is <= 1000
i. Calculate new term
ii. Update PI
iii. nTerm nTerm + 1
5. Show PI
a. Term 4 / Denom
a. If nTerm is even
PI PI Term
b. If nTerm is odd
PI PI + Term
refines
Example 2: Anagrams
Functions
Using the top down approach:
1.
Duplicated coding:
2.
10
Given:
R = radius of the plate
Ra, Rb and Rc = radii
of the circles a, b and
c respectively
11
Rb2
Duplicate coding!
12
A function in C:
Visualization:
input
Function
output
Example:
3
9
sum
12
13
Function: Syntax
function header
{
[0 or more declaration statements]
[0 or more other statements]
[return statement]
}
function body
14
Function Header
sum
12
sum gives
integer result
15
SYNTAX
16
17
18
Some Functions:
int sum( int x, int y )
{
additional variable
int result;
result = x + y;
return result;
void print_info( )
{
printf("Take a 4-digit number X\n");
printf("Rearrange the digits to get Y\n");
printf("Subtract and get R\n");
return;
}
[ CS1010E AY1112S1 Lecture 3 ]
19
Functions in a Program
//Preprocessor directive not shown
int main( )
{
int input1, input2;
//read input1 and input2 from user
//use sum() to add the two inputs
}
[ CS1010E AY1112S1 Lecture 3 ]
20
3.
int result;
result = x + y;
return result;
4.
int main( )
{
int input1, input2, output;
1.
//read inputs
2.
5.
6.
}
[ CS1010E AY1112S1 Lecture 3 ]
21
sum( 3, 9 );
Effectively:
int x = 3;
int y = 9;
actual arguments
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 );
result = 12 + 7;
result = 19;
[ CS1010E AY1112S1 Lecture 3 ]
23
24
It is important to realise:
int main( )
{
int x, y;
}
[ CS1010E AY1112S1 Lecture 3 ]
Function name
Number of input parameter and data type
Output data type
Description of function
26
SYNTAX
Example
Note:
Usage:
27
Function Definition
Portability:
You only need to show user the prototypes, not the full
coding
Ease of Maintenance:
28
function call
}
int sum( int x, int y )
{
definition
int result;
result = x + y;
return result;
}
[ CS1010E AY1112S1 Lecture 3 ]
29
Example
30
Program Abstraction
31
32
known as modularity
Is everything in main() ?
33
C Library
C Standard Library
Library:
A collection of functions
Prototypes are organized into a header file (XXXX.h)
Provides great portability and reusability:
C Standard Library:
Input / Output
Mathematical Functions
others ( more later )
35
examples
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
36
Compilation
Example
Prototype
HEADER
double result;
result = sqrt( 4.0 );
//result = 2.0
37
Example
Prototype
HEADER
Power Function
#include <math.h>
double result;
result = pow( 2.0, 4.0 );
//result = 16.0
38
A lot more
Trigonometry functions:
Logarithm functions:
Reminder:
39
40
Programming Style
C Elements
Summary
Function
- Header and Body
- Function call
- Prototype and Definition
Library
- Introduction to Math Library
41
Reference
Chapter 3
Online C reference:
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
42