Break From The Outermost Loop 
main( ) 
{ 
for ( i = 1 ; i <= 22 ; i+= 2 ) 
{ 
for ( j = 5 ; j <= 50 ; j++ ) 
{ 
for ( k = 1 ; k <= 9 ; k += 3 ) 
{ 
… … 
if ( i + j % k >= 2 ) 
break ; 
} 
} 
} 
} 
break ; 
break ; 
Go to work 
only once 
if ( i + j % k >= 2 ) 
if ( i + j % k >= 2 ) 
break ; 
break ;
main( ) 
{ 
for ( i = 1 ; i <= 22 ; i+= 2 ) 
{ 
for ( j = 5 ; j <= 50 ; j++ ) 
{ 
for ( k = 1 ; k <= 9 ; k += 3 ) 
{ 
… … 
if ( i + j % k >= 2 ) 
goto out ; 
} 
} 
} 
} 
Better Way... 
out : 
; 
Never use 
a goto
main( ) 
{ 
in : 
goto there ; 
for ( i = 1 ; i <= 22 ; i+= 2 ) 
{ 
for ( j = 5 ; j <= 50 ; j++ ) 
{ 
there : 
for ( k = 1 ; k <= 9 ; k += 3 ) 
{ 
… … 
if ( i + j % k >= 2 ) 
} goto out ; 
} 
} 
} 
Where Am I... 
out : 
goto in ;
Control Instructions 
 Sequence 
 Decision 
 Repitition 
 Case 
 goto
Communication 
main( ) 
{ 
int a = 10, b = 20, c = 30 ; 
calsum ( ) ; 
printf ( ”%d”, s ) ; 
} 
calsum( ) 
{ 
int a, b, c, s ; 
} 
int s ; 
s = a + b + c ; 
printf ( ”%d”, s ) ; 
Garbage 
Garbage
Passing Values 
main( ) 
{ 
Actual 
Arguments 
int a = 10, b = 20, c = 30 ; int s ; 
calsum ( a, b, c 
) ; 
printf ( ”%d”, s ) ; 
} 
calsum ( ) 
{ 
int s ; 
} 
int x, int y, int z 
s = x + y + z ; 
printf ( ”%d”, s ) ; 
Garbage 
Formal 
Arguments 
60
main( ) 
{ 
Returning Values 
int a = 10, b = 20, c = 30, s ; 
calsum ( a, b, c ) ; 
} 
calsum ( int x, int y, int z ) 
{ 
int ss ; 
ss = x + y + z ; 
return ( ss ) ; 
} 
s = calsum ( a, b, c ) ; 
printf ( ”%d”, s ) ; 
60 
return ; Returns only 
return ; Returns only 
control 
control 
Return control 
and value 
Return control 
and value 
return ( ss ) ; 
return ( 60 ) ; 
return ( x + y + z ) ;
Are These Calls OK? 
calsum ( a, 25, d ) ; 
calsum ( 10 + 2, 25 % 3, d ) ; 
calsum ( a, calsum ( 25, 10, 4 ), d ) ; 
d = calsum ( a, 25, d ) * calsum ( a, 25, d ) + 23 ; 
calsum ( int x, int y, int z ) 
{ 
int ss ; 
ss = x + y + z ; 
return ( ss ) ; 
} 
 
Nested calls are legal. 
Call within an expression are legal. 
Nested calls are legal. 
Call within an expression are legal.
Returning More Than 1 Value 
main( ) 
{ 
int a = 10, b = 20, c = 30 ; 
ssu, mp p=r soudm ( par, obd, c ( )a ;, b , c ) ; 
printf ( ”%d%d”, s, p ) ; 
} 
sumprod ( i n t x , i n t y , i n t z ) 
ss = x + y + z ; 
pp = x * y * z ; 
{ 
int ss, pp ; 
return ( ss, pp ) ; 
} 
int s, p ; 
 
 
A function can return only 
1 value at a time
main( ) 
{ 
One More Try 
int a = 10, b = 20, c = 30 ; 
int s, p ; 
s = sumprod ( a, b, c ) ; 
p = sumprod ( a, b, c ) ; 
printf ( ”%d%d”, s, p ) ; 
} sumprod ( int x, int y, int z ) 
{ 
ss = x + y + z ; 
pp = x * y * z ; 
return ( ss ) ; 
return ( pp ) ; 
} 
int ss, pp ; 
60 60 
Redundant
main( ) 
{ 
The Only Way Out 
int a = 10, b = 20, c = 30 ; int s, p ; 
s = sumprod ( a, b, c ) ; 
p = sumprod ( a, b, c ) ; 
printf ( ”%d%d”, s, p ) ; 
} 
, 1 
, 2 
sumprod ( int x, int y, int z, ) 
{ 
int ss, pp ; 
ss = x + y + z ; pp = x * y * z ; 
} 
int code 
if ( code == 1 ) 
return ( ss ) ; 
else 
return ( pp ) ; 
Sum, Product, 
Average, Variance 
Standard Deviation
A Better Way 
sumprod ( int x, int y, int z, int code ) 
{ 
int ss, pp ; 
ss = x + y + z ; 
pp = x * y * z ; 
} 
if ( code == 1 ) 
return ( ss ) ; 
else 
return ( pp ) ; 
code == 1 ? return ( x + y + z ) : return ( x * y * z ) ; 
return ( code == 1 ? x + y + z : x * y * z ) ;
ANSI V/s K  R 
calsum ( int x, int y, int z ) 
{ 
.. 
} 
calsum ( int x, int y, int z ) 
{ 
.. 
} 
calsum ( x, y, z ) 
int x, y, z ; 
{ 
.. 
} 
calsum ( x, y, z ) 
int x, y, z ; 
{ 
.. 
} 
ANSI 
Kernighan  
Ritchie
main( ) 
{ 
int y ; 
printf ( ”Enter year” ) ; 
scanf ( ”%d”, y ) ; 
romanize ( y ) ; 
} 
romanize ( int yy ) 
{ 
int n, i ; 
n = yy / 1000 ; 
for ( i = 1 ; i = n ; i ++ ) 
printf ( ”m” ) ; 
Decimal Roman 
1000 m 
500 d 
100 c 
50 l 
10 x 
5 v 
1 i 
} 
Roman Equivalent 
1998 m d c c c c l x x x x i i i 
1998 
v
A More General Call 
main( ) 
{ 
int y ; 
printf ( ”Enter year” ) ; 
scanf ( ”%d”, y ) ; 
, 1000 , ’m’ 
y romanize = romanise ( y ( y, 1000, ’) m’ ; 
) ; 
} 
romanize ( int yy , int j , char ch 
) 
{ 
int n, i ; 
n = yy / j ; 
for ( i = 1 ; i = n ; i ++ ) 
printf ( ) ; 
} 
”%c”, ch 
return ( yy % j ) ;
main( ) 
{ 
... 
1998 md c c c c lx xx x v i ii 
, ’m’ 
y = romanise ( y, 1000 ) ; 
y = romanise ( y, 500, ’d’ ) ; 
y = romanise ( y, 100, ’c’ ) ; 
y = romanise ( y, 50, ’l’ ) ; 
y = romanise ( y, 10, ’x’ ) ; 
y = romanise ( y, 5, ’v’ ) ; 
romanise ( y, 1, ’i’ ) ; 
} 
romanize ( int yy ) 
{ 
} 
, int j, char ch 
int n, i ; 
n = yy / j ; 
for ( i = 1 ; i = n ; i ++ ) 
printf ( ”%c”, ch ) ; 
return ( yy % j ) ; 
Works
Advanced Features of 
Functions 
 Returning a non-integer value 
 Call by value / Call by reference 
 Recursion
Returning a Non-Integer Value 
main( ) 
{ 
a = square ( 2.0 ) ; 
b = square ( 2.5 ) ; 
c = square ( 1.5 ) ; 
printf ( ” %f %f %f ”, a, b, c, ) ; 
square ( 2.0 ) ; 
} square ( ) float x 
{ 
float y ; 
y = x * x ; 
printf ( ” %f ”, y ) ; 
return ( y ) ; 
} 
Function 
Prototype 
4.0 6.0 2.0 
float a, b, c ; float square ( float ) ; 
4.0 
6.25 
2.25 
float square ( float x )
115566 -- 116611 ((ee))

Vcs9

  • 1.
    Break From TheOutermost Loop main( ) { for ( i = 1 ; i <= 22 ; i+= 2 ) { for ( j = 5 ; j <= 50 ; j++ ) { for ( k = 1 ; k <= 9 ; k += 3 ) { … … if ( i + j % k >= 2 ) break ; } } } } break ; break ; Go to work only once if ( i + j % k >= 2 ) if ( i + j % k >= 2 ) break ; break ;
  • 2.
    main( ) { for ( i = 1 ; i <= 22 ; i+= 2 ) { for ( j = 5 ; j <= 50 ; j++ ) { for ( k = 1 ; k <= 9 ; k += 3 ) { … … if ( i + j % k >= 2 ) goto out ; } } } } Better Way... out : ; Never use a goto
  • 3.
    main( ) { in : goto there ; for ( i = 1 ; i <= 22 ; i+= 2 ) { for ( j = 5 ; j <= 50 ; j++ ) { there : for ( k = 1 ; k <= 9 ; k += 3 ) { … … if ( i + j % k >= 2 ) } goto out ; } } } Where Am I... out : goto in ;
  • 4.
    Control Instructions Sequence Decision Repitition Case goto
  • 5.
    Communication main( ) { int a = 10, b = 20, c = 30 ; calsum ( ) ; printf ( ”%d”, s ) ; } calsum( ) { int a, b, c, s ; } int s ; s = a + b + c ; printf ( ”%d”, s ) ; Garbage Garbage
  • 6.
    Passing Values main() { Actual Arguments int a = 10, b = 20, c = 30 ; int s ; calsum ( a, b, c ) ; printf ( ”%d”, s ) ; } calsum ( ) { int s ; } int x, int y, int z s = x + y + z ; printf ( ”%d”, s ) ; Garbage Formal Arguments 60
  • 7.
    main( ) { Returning Values int a = 10, b = 20, c = 30, s ; calsum ( a, b, c ) ; } calsum ( int x, int y, int z ) { int ss ; ss = x + y + z ; return ( ss ) ; } s = calsum ( a, b, c ) ; printf ( ”%d”, s ) ; 60 return ; Returns only return ; Returns only control control Return control and value Return control and value return ( ss ) ; return ( 60 ) ; return ( x + y + z ) ;
  • 8.
    Are These CallsOK? calsum ( a, 25, d ) ; calsum ( 10 + 2, 25 % 3, d ) ; calsum ( a, calsum ( 25, 10, 4 ), d ) ; d = calsum ( a, 25, d ) * calsum ( a, 25, d ) + 23 ; calsum ( int x, int y, int z ) { int ss ; ss = x + y + z ; return ( ss ) ; } Nested calls are legal. Call within an expression are legal. Nested calls are legal. Call within an expression are legal.
  • 9.
    Returning More Than1 Value main( ) { int a = 10, b = 20, c = 30 ; ssu, mp p=r soudm ( par, obd, c ( )a ;, b , c ) ; printf ( ”%d%d”, s, p ) ; } sumprod ( i n t x , i n t y , i n t z ) ss = x + y + z ; pp = x * y * z ; { int ss, pp ; return ( ss, pp ) ; } int s, p ; A function can return only 1 value at a time
  • 10.
    main( ) { One More Try int a = 10, b = 20, c = 30 ; int s, p ; s = sumprod ( a, b, c ) ; p = sumprod ( a, b, c ) ; printf ( ”%d%d”, s, p ) ; } sumprod ( int x, int y, int z ) { ss = x + y + z ; pp = x * y * z ; return ( ss ) ; return ( pp ) ; } int ss, pp ; 60 60 Redundant
  • 11.
    main( ) { The Only Way Out int a = 10, b = 20, c = 30 ; int s, p ; s = sumprod ( a, b, c ) ; p = sumprod ( a, b, c ) ; printf ( ”%d%d”, s, p ) ; } , 1 , 2 sumprod ( int x, int y, int z, ) { int ss, pp ; ss = x + y + z ; pp = x * y * z ; } int code if ( code == 1 ) return ( ss ) ; else return ( pp ) ; Sum, Product, Average, Variance Standard Deviation
  • 12.
    A Better Way sumprod ( int x, int y, int z, int code ) { int ss, pp ; ss = x + y + z ; pp = x * y * z ; } if ( code == 1 ) return ( ss ) ; else return ( pp ) ; code == 1 ? return ( x + y + z ) : return ( x * y * z ) ; return ( code == 1 ? x + y + z : x * y * z ) ;
  • 13.
    ANSI V/s K R calsum ( int x, int y, int z ) { .. } calsum ( int x, int y, int z ) { .. } calsum ( x, y, z ) int x, y, z ; { .. } calsum ( x, y, z ) int x, y, z ; { .. } ANSI Kernighan Ritchie
  • 14.
    main( ) { int y ; printf ( ”Enter year” ) ; scanf ( ”%d”, y ) ; romanize ( y ) ; } romanize ( int yy ) { int n, i ; n = yy / 1000 ; for ( i = 1 ; i = n ; i ++ ) printf ( ”m” ) ; Decimal Roman 1000 m 500 d 100 c 50 l 10 x 5 v 1 i } Roman Equivalent 1998 m d c c c c l x x x x i i i 1998 v
  • 15.
    A More GeneralCall main( ) { int y ; printf ( ”Enter year” ) ; scanf ( ”%d”, y ) ; , 1000 , ’m’ y romanize = romanise ( y ( y, 1000, ’) m’ ; ) ; } romanize ( int yy , int j , char ch ) { int n, i ; n = yy / j ; for ( i = 1 ; i = n ; i ++ ) printf ( ) ; } ”%c”, ch return ( yy % j ) ;
  • 16.
    main( ) { ... 1998 md c c c c lx xx x v i ii , ’m’ y = romanise ( y, 1000 ) ; y = romanise ( y, 500, ’d’ ) ; y = romanise ( y, 100, ’c’ ) ; y = romanise ( y, 50, ’l’ ) ; y = romanise ( y, 10, ’x’ ) ; y = romanise ( y, 5, ’v’ ) ; romanise ( y, 1, ’i’ ) ; } romanize ( int yy ) { } , int j, char ch int n, i ; n = yy / j ; for ( i = 1 ; i = n ; i ++ ) printf ( ”%c”, ch ) ; return ( yy % j ) ; Works
  • 17.
    Advanced Features of Functions Returning a non-integer value Call by value / Call by reference Recursion
  • 18.
    Returning a Non-IntegerValue main( ) { a = square ( 2.0 ) ; b = square ( 2.5 ) ; c = square ( 1.5 ) ; printf ( ” %f %f %f ”, a, b, c, ) ; square ( 2.0 ) ; } square ( ) float x { float y ; y = x * x ; printf ( ” %f ”, y ) ; return ( y ) ; } Function Prototype 4.0 6.0 2.0 float a, b, c ; float square ( float ) ; 4.0 6.25 2.25 float square ( float x )
  • 19.