chapter6 function

Page 1

1

6

(Function) Watcharin Puangplia 01/09/2554


2

!"#$%$ C++ !"#$2-3 4-$%$ C++ !"#$ () 5-6$070/89: (/$7 ; 2 3:$;** !"#$ - $</-2 !"#$ !"#$ ()?2,2( - #*;> =, +,-

"! #$ () #*+,- .(/ /,- 0(/1 "! #$ ()=, +,- .(/ /,- 0(/1 "! #$ () #*;> =, +,-


3

C++ !"#$ (Function) 6 E 7 ; 2/, / %$*- -F- -G (/ 1,- Procedure !"#$%$ C++ 2( 2 +E !"#$2-3 4-$ !"#$ () 5-6$0 JK$70/89: (/$7 ; 2


4

C++ (C++ Standard Function) L$ !"#$ ()2( /9,;>:1%$ C++ - (/ %": !"#$ 6>,-$(K3: 2( - 12 (include) :2- ,12%$ - + 2?.>!0:1/ cin, cout ( include ? >! iostream.h) sqrt, log, sin, cos (include ? >! math.h)


5

Math Function Function abs

Include File stdlib.h

Function exp

Include File math.h

acos

math.h

itoa

stdlib.h

asin

math.h

log

math.h

atan

math.h

log10

math.h

atof

stdlib.h

sin

math.h

atoi

stdlib.h

sinh

math.h

cos cosh

math.h math.h

sqrt

math.h

exp

math.h

tan tanh

math.h math.h


6

1 2 34567 5 3 8 (Programmer--Defined Function) (Programmer Function) L$ !"#$ ()89: (/$7 ; 2= :- JK$2-%62, ",1/%6:89: (/$7 ; 2=-2- R;*, - 5- -$ #K 7 ; 2 L$=,1$/, /S () (/ 1,- 7209> (Module) (/$ !"#$+ #K 0(/1 ;3,=-2- R (/ %": -$?0:6>-/+ #K 3#1 /,- 7 ; 2 ()2( - = :- !"#$;** return +,-?0: ;> 2( - #*+,- Parameter int Cal(int x, int y) { return x + y; }


7 #include <iostream> #include <conio.h> using namespace std; int main(void) { int i = 1; cout <<"First time..." << endl; while(i<=10) { cout << i << " "; i++; } cout << endl << "Second time..." << endl; while(i<=10) { cout << i << " "; i++; } cout << endl; getch(); return 0; }

6@@ AB First time... 1 2 3 4 5 6 7 8 9 10 Second time... 1 2 3 4 5 6 7 8 9 10


#include <iostream.h> #include <conio.h> void one_to_ten(); (Function Prototype) int main() { cout << "First time..." << endl; one_to_ten(); cout << endl << "Second time..." << endl; one_to_ten(); (Call Function) cout << endl; getch(); return 0; } void one_to_ten() { int i = 1; while(i<=10) { cout << i << " i++; } }

(Function Definition)

6@@ AB ";

First time... 1 2 3 4 5 6 7 8 9 10 Second time... 1 2 3 4 5 6 7 8 9 10

8


9

8 8 (Function Prototype) 3:$;** !"#$ L$3#1* %6:+ 2?. > ! 9:RJ : "$<0 : 29> ()G =, +,- >#* G5-$1$.- -2< 3 ! ()+-061# 1,-G ?0: #* "$<0 .- -2< 3 !;3,> 3#1 12 #K >5-0#* .- -2< 3 ! 6>,-$#K$ 9 ;**:

"$<0 : 29> (G) =, +,- >#* "E) !"$# ("$<0 .- -2< 3 ! () #* :-); return-value-type

function-name

parameter-list


10

5H 5 8

!"#$"E) my_func ?2,2( - #*+,- :- ;> ?2,2( - =, +,- >#* void my_func(void);

void my_func();

!"#$"E) square #*.- -2< 3 ! 1 3#1 L$G5-$1$ 3Z2 ;> =, +,- >#* L$G5-$1$ 3Z2 int square(int num);

6 E int square(int);

!"#$"E) to_real #*.- -2< 3 ! 2 3#1 L$G5-$1$ 3Z2 #K +9, ;> =, +,- >#* L$G5-$1$ G < float to_real(int x,int y);


11

I I5 (Function Definition) - $</-2 !"#$ L$ - 5-6$0 - 5- -$ -/%$ !"#$ "$<0 : 29> ()G =, +,- >#*

"E) !"#$

"$<0 .- -2< 3 ! () #* :-

type function_name(parameter lists) {

-[3#1; +5-=#) ;

;

return type; }

type +E "$<0 : 29> ()R9 +E$+,- ?0:; ,"$<0 : 29>.E$K 4-$3,- S C++ 6 E ()= :- JK$%62, R:-?2,2( - #*;> =, +,- G 5-6$0 type 0:1/ void function_name +E "E ) !"#)$ (3) #K 3-26># - 3#K "E) 3#1; parameter lists +E .- -2< 3 !3,- S () #*2- R:-2(2- 1,- 1 .- -2< 3 !G 2( + E) 62-/G]> -++#)$?1: ",$ void func(int x, int y) return +E +5-=#) ()* %6: !"#)$/, /=<$K =]0 - 5- -$ (2( 6 E ?2,2( ?Z 0:)


12

KK H H 8@ 1H LH #include <iostream.h> #include <conio.h> void one_to_ten();

// 3:$;** "! #)$

int main() { cout << "Print 1 to 10" << endl; one_to_ten();

// - (/ %": !"#)$

6@@ AB Print 1 to 10 1 2 3 4 5 6 7 8 9 10

"E) !"#$

getch(); return 0;

void one_to_ten()

} void one_to_ten()

// $</-2 !"#)$

{ int i; for(i=1;i<=10;i++) cout << i << " "; }

?2,2( - #*;> =, +,- G 5-6$0 type 0:1/ void


13

5H 5

K H 8@ 1H LH 1. G (/$ !"#$"E) test_name _J) !"#$$(KG ?2,2( - #*;> =, +,-.- -2< 3 ! 70/%6: ;=0 "E) ` $-2= ]> $# [J F- %": E) $? - 1$ * (Loop for 6 E Loop while)

6@@ AB

M – 1 N@ ( )

M – 1 N@ ( )

M – 1 N@ ( )

M – 1 N@ ( )

M – 1 N@ ( )


14

OP Q 8 M K H ? #include <iostream.h> #include <conio.h> int main() { cout << "Print 1 to 10" << endl; one_to_ten(); getch(); return 0; - (/ %": } void one_to_ten() { int i; for(i=1;i<=10;i++) cout << i << " "; }

!"#$G 5-?0: Z3, 2E) !"#$$#K$?0:2( - -[3:$;** !"#$6 E $</-2 !"#$ , $ ()G (/ %": !"#$ 0# $#K$ - (/$;**$(KRE 1,- 8<0!


15

OP Q 8 M K H ? #include <iostream.h> #include <conio.h> void one_to_ten() { int i; for(i=1;i<=10;i++) cout << i << " "; }

5 N - = :- !"#)$%62,

-?2,G5- L$3: -[

3:$;** !"#)$ Z?0: S 1 5@ 54

K 1H 3 8 H O S7 5 M 5 H I K@

(Inline

Function) int main() { cout << "Print 1 to 10" << endl; one_to_ten(); getch(); return 0; }


16

LH 4 5 #include <iostream.h> #include <conio.h> void my_print(char ch,int loop); int main() (Argument) { my_print(‘A',4); getch(); return 0; (Parameter) } void my_print(char ch,int loop) { int i; 6@@ AB for(i=0;i<loop;i++) A A A A cout << ch << " "; }

(Copy)

UPass by ValueX ‘A’

4

ch

loop


17

1H 1H 6H LH

- =, 8,-$+,-%6: #* !"#$%":1<e( - () (/ 1,- UPass by ValueX _J) L$ - +#0> (Copy) +,- - ! <1 2$3! %6: #*.- -2< 3 ! !"#$ ()%":%$ - #*+,- >()/$; > +,- .- -2< 3 ! -/%$ !"#$ G ?2,=, 8>3, +,- - ! <1 2$3! () =, 2-3 $ (/ %": !"#$


18

1H 1H 6H LH ( H ) #include <iostream.h> #include <conio.h> void inc_print(int num); int main() { int a = 1; inc_print(a); cout << "a = " << a << endl; getch(); return 0; }

- +#0> +,- - ! <1 2$3! +E +,- a _J) 2(+,- a = 1 %6: #*.- -2< 3 ! !"#$ inc_print

void inc_print(int num) { num = num + 1; cout << "num = " << num << endl; }

6@@ AB num = 2 a = 1


19

1H LH @ 4 5 #include <iostream.h> #include <conio.h> 6@@ AB int sum_one_to_ten(); int main() sum from 1 to 10 = 55 { int a; a = sum_one_to_ten(); cout << "sum from 1 to 10 = " << a << endl; getch(); return 0; } int sum_one_to_ten() { int i,sum = 0; for(i=1;i<=10;i++) sum = sum + i; return sum; }

//return

=, +,- >#*


20

main 1H LH @ G- 0<2 ()=,1$6#1 !"#$ main G %": L$ void main() _J) 2E) + 2?.>!G 2(+5- 3E $1,return type of `main' is not `inti !"#$ main ()R9 3: +1 %": L$ int main() {

+5-=#) ; return 0; } return 0 L$=, +,- 0 >#*? %6: #* ** j<*#3< - .E) * 1,-7 ; 2 5- -$

= ZG70/=2*9 k!


21

2 LH 8@ 1H LH @ #include <iostream.h> !" # $ %

$ & #include <conio.h> ' "( x = 9 ) * +, "- 81 int test_double(int a); ! & ' ' $ %

$ & int main() ! . + { int x; cout << "Enter x : "; cin >> x; cout<<"Double "<<x<<"*"<<x<<" is= "<< test_double(x)<<endl; getch(); return 0; } int test_double(int a) { return a*a; }

% / type + "- 0 1 '

' + & #" 2' 3 ' "- '


22

2 LH 8@ 1H LH @ BI 53LY4 #include <iostream.h> 9 ;** !"#)$ #include <conio.h> int test_double(int a) G %": int test_double(int a); int main() type L$ int 1- 6$:-"E) !"#)$ .E) * 1, - ! <1 2$3! (Argument) { !"#)$ ()G = :- %62, G +E$+,- L$"$<0 int int x; %$1 >Z* (int a) G #*+,-.- -2< 3 ! 1 cout << "Enter x : "; 3#1 ;> L$ : 29>"$<0 int ,-$#K$ cin >> x; cout<<"Double "<<x<<"*"<<x<<" is= "<< test_double(x)<<endl; getch(); .- -2< 3 ! (Parameter) return 0; } int test_double(int a) { return +,- $<.G$! return a*a; }

6@@ AB Enter x :9 Double 9*9 is = 81


23

2 LH 8@ 1H LH @ #include <iostream.h> #include <conio.h> 6@@ AB int test_double(int a); Enter x :9 int main() Double 9*9 is = 81 - ! <1 2$3! (Argument) { int x; cout << "Enter x : "; cin >> x; cout<<"Double "<<x<<"*"<<x<<" is= "<< test_double(x)<<endl; getch(); .- -2< 3 ! (Parameter) return 0; } int test_double(int a) { return +,- $<.G$! return a*a; }

+,- () return G L$3#1; , +,-+ (,) 6 E $<.G$! ?Z 0:


24

IB LLHH 1H @ O

- %": -$+,- ()=, >#*G- !"#$2( /9, 3 9 ;**+E 1. %":3#1; 2- #*+,2. L$ - ! <1 2$3! !"#$ E)$ 3. L$=,1$6$J) $<.G$!


25 #include <iostream.h> #include <conio.h> int square(int x); int main() #" { int a=2,b; "- ' b = square(a); cout << "b = " << b << endl; cout << "Square of (a+1) = " << square(a+1) << endl; b = 1 + square(a+2); cout << "b = " << b << endl; "- & 2' getch(); return 0; }

6@@ AB

int square(int x) { return x*x; }

b = 4 Square of a+1 = 9 b = 17


26

5H 5 Z @ 5 2

11

#include <iostream.h> add(10,1) #include <conio.h> int add(int a,int b); add(add(2,8),1) int main() { int sum = add(add(2,add(5,3)),1); cout << "sum = " << sum << endl; getch(); return 0; } int add(int a,int b) 6@@ AB { return a+b; sum = 11 }


27

3O 5 5H G (/$7 ; 2=5-6 #*6-+,-[# /!? m-_J) 2(=2 - 0# $(K V = I * R 70/ () V +E +,-[# /!? m- , I +E +,- ;=? m- =,1$ R +E +,-+1-23:-$ -$ ;> #K =-2+,-$(K L$G5-$1$G < 70/ 5-6$0%6:=,1$ ()+5-$1k+,- V /9,%$ !"#$ get_volt =5-6 #*=,1$ () #*+,- I ;> R G- 89:%": 12 #K =,1$ ();=0 8>>#.e! +,- V %6: /9,%$ !"#$ main I, R main

get_volt

V


28

#include <iostream.h> #include <conio.h> float get_volt(float I,float R); int main() { float i,r,v; cout << "Enter I : "; cin >> i; cout << "Enter R : "; cin >> r; v = get_volt(i,r); cout << "Volt = " << v << endl; getch(); return 0; } float get_volt(float I,float R) { float V = I*R; return V; }


29

3O 5 5H G (/$7 ; 2=5-6 #*6-+,- F(x) _J) 2(=2 - 0# $(K F(x) = x2 + 2x + 1 R:- x 2(+,-?2,3)5- 1,- 0 F(x) = 0 R:- x 2(+,-3)5- 1,- 0 5-6$0%6:=,1$ ()%":%$ - +5-$1k+,- F(x) /9,%$ !"#$ get_fx =5-6 #*=,1$ () #*+,x G- 89:%": ;> ;=0 8>>#.e! +,- F(x) /9,%$ !"#$ main (x ;> F(x) L$ G5-$1$ 3Z2) x main

get_fx F(x)


30

#include <iostream.h> #include <conio.h> int get_fx(int a); int main() { int x,fx; cout << "Enter x : "; cin >> x; fx = get_fx(x); cout << "f(" << x << ") = " << fx << endl; getch(); return 0; } int get_fx(int a) { if(a >= 0) return a*a + 2*a + 1; else return 0; }


31

3O 5 5H G (/$ !"#$"E) maximum _J) 2( - #*.- -2< 3 ! 3 3#1 L$G5-$1$ 3Z2 #K 620 ;> %6:=, +,- >#* L$+,- ()2- ()=]0 G5-$1$ 3Z2 #K =-2 int maximum(int x,int y,int z) { int max = x; if(y > max) max = y; if(z > max) max = z; return max; }


32 #include <iostream.h> #include <conio.h> int maximum(int x,int y,int z); int main() { int a, b, c, ans; cout << "Enter a : "; cin >> a; cout << "Enter b : "; cin >> b; cout << "Enter c : "; cin >> c; ans = maximum(a,b,c); cout<<"anser is = "<<ans<<endl; getch(); return 0; }

int maximum(int x,int y,int z) { int max = x; if(y > max) max = y; if(z > max) max = z; return max; }


33

3O 5 5H 1. G (/$7 ; 2+5-$1k.EK$ () _J) -/> (/00# $(K -/> (/0 ()/1 #*7 ; 2 1.1 !"#)$/, / function circum_a 3#1 >E () 1 .E) +5-$1k6- =:$ *1 circum = 2 * 3.14156 * radius 1.2 !"#)$/, / function area_a 3#1 >E () 2 .E) +5-$1k6-.EK$ () 9 1 >2 area = 3.14156 * radius * radius 1.3 !"#)$6># main program .E) (/ %": !"#)$/, / 7 ; 2 #* : 29> .E) +5-$1k0# $(K 2.1 3#1 >E (choice) 2.2 #[2(1 >2 (radius)


34

5H 814 7@ 3 8 3#1 /,- () 1 1. Circumference of the circle 2. Area of the circle 3. Quit Please Select Choice => 1 Please Enter Redius1 => 5.9 Area is => 31.4156 1. Circumference of the circle 2. Area of the circle 3. Quit Please Select Choice => 2 Please Enter Redius2 => 25.5 Area is => 2042.8


#include <iostream.h> #include <conio.h> float circum_a(float radius); float area_a(float radius); int main() { int choice; float radius; cout << "1. Circumference of the circle" <<endl; cout << "2. Area of the circle" <<endl; cout << "3. Quit" <<endl << endl; cout << "Please Select Choice => "; cin >> choice; while ( choice != 3) { if (choice == 1) { cout << "Please Enter Redius => "; cin >> radius; circum_a(radius); } else { cout<<"Please Enter Redius => "; cin>>radius; area_a(radius); }

35


36 cout << endl << "1. Circumference of the circle"<<endl; cout << "2. Area of the circle" <<endl; cout << "3. Quit" << endl << endl; cout << "Please Select Choice => "; cin >> choice; } // loop while getch(); return 0; } float circum_a(float radius) { float circum; circum = 2 * 3.14156 * radius; cout << "Area is => " << circum << endl; } float area_a(float radius) { float area; area = 3.14156 * radius * radius; cout << "Area is => " << area << endl; }


37

;**st 6#0 1. G (/$7 ; 2 >()/$ ]k6 92<G- 6$,1/ [- _> _(/=? L$6$,1/ - $?u3! 6 E 7 2 ! _J) -/> (/0 0# $(K 3#1 >E 0# $(K 1.1 >()/$ ]k6 92<G- 6$,1/ [- _> _(/=? L$6$,1/ - $?u3! fahrenheit = c / 5 * 9 + 32 2.2 >()/$ ]k6 92<G- 6$,1/ [- _> _(/=? L$7 2 ! romer = c / 4 * 5 -/> (/0 ()/1 #*7 ; 2 1.1 !"#)$/, / function fahrenheit_a 3#1 >E () 1 .E) >()/$ ]k6 92<G- 6$,1/ [- _> _(/=? L$ 6$,1/ - $?u3! 1.2 !"#)$/, / function romer_a 3#1 >E () 2 .E) >()/$ ]k6 92<G- 6$,1/ [- _> _(/=? L$7 2 ! 1.3 !"#)$6># main program .E) (/ %": !"#)$/, / 7 ; 2 #* : 29> .E) +5-$1k0# $(K 1.1 3#1 >E (choice) 1.2 [- _> _(/= (c)


38

5H 814 7@ 3 8 1. Celsius - > Fahrenheit 2. Celsius - > Romer 3. Quit Choose the menu 1 or 2 : 1 Enter temperature in celsius : 27 Temperature in Fahrenheit = 80.6 Fahrenheit 1. Celsius - > Fahrenheit 2. Celsius - > Romer 3. Quit Choose the menu 1 or 2 : 2 Enter temperature in celsius : 21 Temperature in Romer = 26.25 Romer


39

2. G (/$7 ; 2+<0=,1$>0 6:- Big C /]e/- _J) -/> (/00# $(K - !00# $(K 2.1 "$<0*#3 Silver Card >0 3% 2.2 "$<0*#3 Gold Card >0 5% 2.3 "$<0*#3 Platinum Card >0 7% -/> (/0 ()/1 #*7 ; 2 2.1 !"#)$/, / function silver_card .E) +5-$1k6-+,-=,1$>0G- "$<0*#3 Silver Card 2.2 !"#)$/, / function gold_card .E) +5-$1k6-+,-=,1$>0G- "$<0*#3 Gold Card 2.3 !"#)$/, / function platinum_card .E) +5-$1k6-+,-=,1$>0G- "$<0*#3 Platinum 2.4 !"#)$6># main program .E) (/ %": !"#)$/, / 7 ; 2 #* : 29> .E) +5-$1k0# $(K 2.1 - !0 (type_card) 2.2 -+-=<$+:- (price)


40

3#1 /,- - ;=0 : 29>%$7 ; 2 1. 2. 3. 4.

Silver Card Gold Card Platinum Card Quit

Choose the menu : 1 Enter price : 850 Discount is : 25.5 Bath Money is 1. Silver

824.5 Card

2. Gold Card 3. Platinum Card 4. Quit Choose the menu : 4 Bye Bye !

Bath


41

3. G (/$7 ; 2+5-$1k+,-%":G,-/ - G- - 3<2$K5-2#$ () 2 { Esso 3-2 : 5-6$0 ;> E) $? 3, ? $(K 7 72"#)$0# $(K 3.1 3<22<$< 20 ><3 +<0 200 *- ><3 3, ? +<0 ><3 > 25 *- 3.2 3<2 >- 20 ><3 ><3 > 250 *- ><3 3, ? +<0 ><3 > 20 *- 3.2 3<22- 20 ><3 ><3 > 300 *- ><3 3, ? +<0 ><3 > 15 *- -/> (/0 ()/1 #*7 ; 2 3.1 !"#)$/, / function Mini .E) +5-$1k+,-%":G,-/ - G- - 3<2$K5-2#$ 7 72"#)$ () 1 3.2 !"#)$/, / function Medium .E) +5-$1k+,-%":G,-/ - G- - 3<2$K5-2#$ 7 72"#)$ () 2 3.3 !"#)$/, / function BigBig .E) +5-$1k+,-%":G,-/ - G- - 3<2$K5-2#$ 7 72"#)$ () 3 3.4 !"#)$6># main program .E) (/ %": !"#)$/, / 7 ; 2 #* : 29> .E) +5-$1k0# $(K 3.1 7 72"#)$ (promotion) 3.2 G5-$1$><3 () 3<2$K5-2#$ (num)


42

3#1 /,- - ;=0 : 29>%$7 ; 2 1. 2. 3. 4.

Promotion Number 1 >> Mini Promotion Number 2 >> Medium Promotion Number 3 >> BigBig Quit

Please Select Promotion Number : 2 Input Number(Liter) : 50 Cost is : 850 Bath 1. 2. 3. 4.

Promotion Number 1 >> Mini Promotion Number 2 >> Medium Promotion Number 3 >> BigBig Quit

Please Select Promotion Number : 3 Input Number(Liter) : 50 Cost is : 750 Bath


43

4. G (/$7 ; 2+5-$1k+,-%":G,-/%$ - %":7 [#. ! * <F # -".} F! >+ 229$< +"#)$ G5- #0 (26-"$) _J) 2(7 72"#)$0# $(K 7 72"#)$0# $(K 4.1 +]/=#K$ 2 $- (; 1 *- $- (R#0? $- (> 1.50 *- 4.2 +]/$-$ 2 $- (; 3 *- $- (R#0? $- (> 0.20 *- -/> (/0 ()/1 #*7 ; 2 4.1 !"#)$/, / function Shorter .E) +5-$1k+,-%":G,-/%$ - %":7 [#. !7 72"#)$ () 1 4.2 !"#)$/, / function Longer .E) +5-$1k+,-%":G,-/%$ - %":7 [#. !7 72"#)$ () 2 4.3 !"#)$6># main program .E) (/ %": !"#)$/, / 7 ; 2 #* : 29> .E) +5-$1k0# $(K 4.1 7 72"#)$ (promotion) 4.2 1>-%$ - %": -$ (TimeToUse)


44

3#1 /,- - ;=0 : 29>%$7 ; 2 Please Select Promotion 1.Short Talk 2.Long Talk 3.Quit : Please input Time to Use (minutes) : 12 Your Cost is 17 Baht

1

Please Select Promotion 1. Short Talk 2. Long Talk 3.Quit : 2 Please input Time to Use (minutes) : 32 Your Cost is 13.50 Baht Please Select Promotion 1. Short Talk 2. Long Talk 3.Quit : 3 Thank You for use program.


45

5 Z2P (Recursive Function) #include <iostream.h> #include <conio.h> int fac(int x); int main() 3 * fac(2) 2 { int y = fac(3); cout << "3! = " << y << endl; 2 * fac(1) getch(); 1 6 return 0; } int fac(int x) { if(x <= 1) 6@@ AB return 1; else 3! = 6 return x* fac(x-1); }


46

8 5 8@ 8 5 3#1; -/%$ (Internal Variable) +E 3#1; () -[ -/%$ !"#$ ;> =-2- R%": -$ ?0: Â .- -/%$ !"#$ ,-$#K$ 3#1; -/$ (External Variable) +E 3#1; () -[?1: -/$ !"#$ =-2- R%": -$?0:%$ ] !"#$ 6 E #K 7 ; 2 6- ?2,2( - 5-6$0+,- <)23:$ 3#1; -/$ G R9 5-6$0%6: L$ 0 70/ #37$2#3< ;3, =5-6 #*3#1; -/%$G %":+,- ()+:- /9,%$6$,1/+1-2G5-


47

5H 8 5

#include <iostream.h> #include <conio.h> void my_func(); int main() { double x = 1.1; my_func(); cout << "In main, x = " << x << endl; getch(); return 0; 6@@ AB } In my_func, x = 2.5 In main, x = 1.1

void my_func() { double x = 2.5; cout << "In my_func, x = " << x << endl; }


48

5H 8 5 8 5 #include <iostream.h> #include <conio.h> 6@@ AB double x; void my_func(); In my_func, x = 2.5 In main, x = 2.5 int main() { x = 1.1; my_func(); cout << "In main, x = " << x << endl; getch(); return 0; } void my_func() { x = 2.5; cout << "In my_func, x = " << x << endl; }


49

5 N 6- ?2,2(+1-2G5- L$G < S ?2,+1 %":3#1; -/$ . - 3#1; $(K=-2- R; :? +,-?0: %$ ] !"#$_J) 5-%6: <0+1-28<0.>-0?0: ,-/ 6- 3#1; -/%$2("E) 0(/1 #*3#1; -/$ %6:/J0 -3#1; -/%$ L$6># 6- 3: - :-RJ +,-3#1;


50

8 5 M 4 5 8 5 #include <iomanip.h> #include <iostream.h> #include <conio.h> In my_func, x = 2.5 double x; In main, x = 1.1 void my_func(); int main() { double x = 1.1; my_func(); cout << "In main, x = " << setprecision(3) << x << endl; getch(); return 0; } void my_func() { x = 2.5; cout << "In my_func, x = " << setprecision(3) << x<<endl; }

6@@ AB


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.