Title: Tuesday, December 26, 2006
1Tuesday, December 26, 2006
- There was a most ingenious architect who
contrived a new method for building houses, - By beginning at the roof and working downward to
the foundation. - - Jonathan Swift, Gullivers Travels
2Equivalent statements?
Nested Switch
char ch1, ch2 cingtgtch1gtgtch2 switch(ch1)
case 'A' cout ltlt "This A is part of outer
switch\n" switch(ch2) case 'A' cout
ltlt "This A is part of inner switch\n" break
case 'B' cout ltlt "This B is part of inner
switch\n" break case 'B' cout ltlt
"This B is part of outer switch\n" break
3Equivalent statements?
Nested Switch
char ch1, ch2 cingtgtch1gtgtch2 switch(ch1)
case 'A' cout ltlt "This A is part of outer
switch\n" switch(ch2) case 'A' cout
ltlt "This A is part of inner switch\n" break
case 'B' cout ltlt "This B is part of inner
switch\n" break case 'B' cout ltlt
"This B is part of outer switch\n" break
4 5Benefits of Top Down Design
- Subtasks, or functions in C, make programs
- Easier to understand
- Easier to change
- Easier to write
- Easier to test
- Easier to debug
- Easier for teams to develop
6- Hardware design
- Subparts or modules
- Interface
- Software design
7- Three places where variables are declared
- Inside functions local variables
- In the definition of function parameters formal
parameters - Outside of all functions global variables
8- local variables are created when function is
called and destroyed when function is exited. - formal parameters are used like local variables.
Their value is lost once the function terminates.
They have a special task of receiving the value
of arguments. - global variables hold their value throughout the
lifetime of your program
9- int sum(int x, int y)
- int main()
- int answer
- coutltltIn main
- answer sum(2,3)
- coutltltanswer
- return 0
-
- int sum (int x, int y)
- int zxy
- coutltltIn sum
- return z
-
10- int sum(int x, int y)
- int main()
- int answer, num1, num2
- cingtgtnum1gtgtnum2
- coutltltIn main
- answer sum(num1,num2)
- coutltltanswer
- return 0
-
- int sum (int x, int y)
- int zxy
- coutltltIn sum
- return z
-
Function Call
11(No Transcript)
12Examples of Functions
include ltiostream.hgt double pi(void) int
main(void) double answer answerpi() coutltlt"
answer is "ltltanswerltltendl return 0 double
pi(void) double value3.14159 coutltlt"I return
value of pi when called"ltltendl return value
13Examples of Functions
I return value of pi when called answer is
3.14159
14include ltiostream.hgt double area(double
radius) double pi() int main(void) double
answer answerarea(7.5) coutltlt"answer is
"ltltanswerltltendl return 0 double area(double
radius) double valuepi()pi()radius return
value double pi() return 3.14159 //we can
return a constant also
15- Output?
-
- answer is 74.0219
16include ltiostream.hgt double Add2(double d, char
c, int i) int main(void) double dd4.4 char
cc'b' int ii15 double ansAdd2(dd,cc,ii) c
outltltansltltendl coutltltddltlt"\t"ltltiiltlt"\t"ltltccltltend
l return 0 double Add2(double d, char c,
int i) dd2 ii2 cc2 coutltltdltlt"\t"ltltilt
lt"\t"ltltcltltendl return d
176.4 17 d 6.4 4.4 15 b
- Parameter list order is important!
18- int main()
- int x30, y40, z45, ans1, ans2
- x (y) (x/y) 35z
- x (z98-y)78 53/z
- x / (3467y(zx))
- ans1 3x50y
- x70, y80, z85
- x (y) (x/y) 35z
- x (z98-y)78 53/z
- x / (3467y(zx))
- ans2 3x50y
- coutltltans1ltltendlltltans2
19- int calculate(int a, int b, int c)
- int main()
- int x30, y40, z45, ans1, ans2
- ans1 calculate(x, y, z)
- x70, y80, z85
- ans2 calculate(x, y, z)
- coutltltans1ltltendlltltans2
-
- int calculate(int a, int b, int c)
- int value
- a (b) (a/b) 35c
- a (c98-b)78 53/c
- a / (3467b(ca))
- value 3a50b
- return value
20- header files
- predefined functions include ltmath.hgt
21Math library
include ltmath.hgt sin(x), cos(x), tan(x),
asin(x), acos(x), atan(x), sinh(x), cosh(x),
tanh(x), exp(x), log(x), log10(x), pow(x,y),
sqrt(x), ceil(x), floor(x)
22int main() int num double sine_val
for(num1 num lt 100 num) sine_val
sin(num3.14/180.0) cout ltlt num ltlt"
"ltltsine_valltlt "\n" return 0
23- include ltstdlib.hgt
- int rand(void)
24- int sum(int x, int y)
- int main()
- int answer, x, y
- cingtgtxgtgty
- coutltltIn main
- answer sum(x,y)
- coutltltanswer
- return 0
-
- int sum (int x, int y)
- int zxy
- coutltltIn sum
- return z
-
Function Call