Download presentation
Presentation is loading. Please wait.
1
Tuesday, 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, Gulliver’s Travels
2
Nested Switch char ch1, ch2; cin>>ch1>>ch2; switch(ch1) { case 'A': cout << "This A is part of outer switch\n"; switch(ch2) { case 'A': cout << "This A is part of inner switch\n"; break; case 'B': cout << "This B is part of inner switch\n"; } break; case 'B': cout << "This B is part of outer switch\n"; break; } Equivalent statements?
3
Nested Switch char ch1, ch2; cin>>ch1>>ch2; switch(ch1) { case 'A': cout << "This A is part of outer switch\n"; switch(ch2) { case 'A': cout << "This A is part of inner switch\n"; break; case 'B': cout << "This B is part of inner switch\n"; } break; case 'B': cout << "This B is part of outer switch\n"; break; } Equivalent statements?
4
§Top down design
5
Benefits of Top Down Design §Subtasks, or functions in C++, make programs l Easier to understand l Easier to change l Easier to write l Easier to test l Easier to debug l Easier for teams to develop
6
§Hardware design l Subparts or modules l Interface §Software design
7
Three places where variables are declared: 1.Inside functions: local variables 2.In the definition of function parameters: formal parameters 3.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; cout<<“In main”; answer = sum(2,3); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; }
10
int sum(int x, int y); int main(){ int answer, num1, num2; cin>>num1>>num2; cout<<“In main”; answer = sum(num1,num2); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; } Function Call Pass by Value
12
Examples of Functions # include double pi(void); int main(void){ double answer; answer=pi(); cout<<"answer is "<<answer<<endl; return 0; } double pi(void){ double value=3.14159; cout<<"I return value of pi when called"<<endl; return value; }
13
Examples of Functions I return value of pi when called answer is 3.14159
14
#include double area(double radius); double pi(); int main(void){ double answer; answer=area(7.5); cout<<"answer is "<<answer<<endl; return 0; } double area(double radius){ double value=pi()*pi()*radius; return value; } double pi(){ return 3.14159; //we can return a constant also }
15
§Output? answer is 74.0219
16
#include double Add2(double d, char c, int i); int main(void){ double dd=4.4; char cc='b'; int ii=15; double ans=Add2(dd,cc,ii); cout<<ans<<endl; cout<<dd<<"\t"<<ii<<"\t"<<cc<<endl; return 0; } double Add2(double d, char c, int i){ d=d+2; i=i+2; c=c+2; cout<<d<<"\t"<<i<<"\t"<<c<<endl; return d; }
17
§Output? 6.4 17 d 6.4 4.4 15 b §Parameter list order is important!
18
int main() { int x=30, y=40, z=45, ans1, ans2; x += (++y) * (x/y) + 35*z; x *= (z*98-y)*78 + 53/z; x /= (34*67*y*(z%x)); ans1 = 3*x+50+y; x=70, y=80, z=85; x += (++y) * (x/y) + 35*z; x *= (z*98-y)*78 + 53/z; x /= (34*67*y*(z%x)); ans2 = 3*x+50+y; cout<<ans1<<endl<<ans2; }
19
int calculate(int a, int b, int c); int main(){ int x=30, y=40, z=45, ans1, ans2; ans1 = calculate(x, y, z); x=70, y=80, z=85; ans2 = calculate(x, y, z); cout<<ans1<<endl<<ans2; } int calculate(int a, int b, int c){ int value; a += (++b) * (a/b) + 35*c; a *= (c*98-b)*78 + 53/c; a /= (34*67*b*(c%a)); value = 3*a+50+b; return value; }
20
header files predefined functions #include
21
Math library #include 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)
22
int main() { int num; double sine_val; for(num=1; num < 100; num++) { sine_val = sin(num*3.14/180.0); cout << num <<" "<<sine_val<< " \n " ; } return 0; }
23
#include int rand(void)
24
int sum(int x, int y); int main(){ int answer, x, y; cin>>x>>y; cout<<“In main”; answer = sum(x,y); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; } Function Call Pass by Value
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.