Chapter 3

bahrainiancrimsonSoftware and s/w Development

Nov 13, 2013 (4 years ago)

91 views


   
64
Module
Chapter 3
Custom subroutines
65

   
Custom subroutines
3
Chapter
     Code
Units     
     
     
Form Unit.
Introduction
               
 ! "  #     $       #  
   $    %  &    $    
 !       !    #     
        $   !   '
!      (       )  !   
    $ 
Containers of subroutines
       !      *   $
!           !     
    +) !    
 !    (, , 
( -)      ,/  
(        $ 
     !   
Units   !      
        # 
          
!   !     
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
0     !           $
$   !         1     
!     
 ,( !    !    
,  

   
66
Module
,  $     !          !  )
       !    $     ! 
     ' Math '          ) 
2          !     
rValue := Power(iNumber, 4) ;
33     %2 &
4
5 (!   !      
 $       
         
   )
    
'Power'.
5 (!    #   !  
  "     
  !      
  #     $ 
  78  798    
 : !    !
         
 !  $   !       #    
          ;       
      !   )  !    
    !      <      
                  
Rules and terminology regarding
functions
1 
   
    !   !   
 !  "  #
  $   
1 
  =  
  )   
      !  
+)  ! )
  
        !"
,    $ /#   $  ! >?    $   
   !  (,  !   )   @ 
         $   A>?A
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Math;
67

   
Note:
5 1 !   $     )  (, !  $ 
   $  $    
5   !   !     #     !  
!  !    $ /#   )    
arguments 1     #  actual parameters.
,      #    !  
  ! !  !        !
<variable> := <Name of function > (<argument(s)>);
5    !           !     "
  !   (, !  ! ) =      !
     1 ) !  !         
 !
     #  $  $  %#
&%'%*"
 "          !   extended %   
     &       @ "   
            $  
5 1      $ 
  !  !    $
   !       
   !    ! )
  +-../0!1!"
5        !      $ !  !    
 $    $    ,   ! )
;      % &
        
      $   $ 
   parameters !    ;
        !  
     arguments.
 !  Odd       
 !   !     $      
!  !  C   - $  %     &
The Uppercase !  $   *       !     * 
DE-   !        $  %   D  - &       $  ! 
          !  D E  -      
 


 $ >?   
 $  >?     $ A>?A   
2 ! !  1 
  +2 .+2 3"
  +5 #  6
.7 8 3996
. 8 3"
  8
 #  <>?< . 8*%< . 8*%< . 8*"

   
68
Module
;           ! $      !
   =  0      !       #
F%F              =      =  
  +)  ) 
 !    )
  !
  
The copy !  =     '   %     !&  
  $  %   !      !   &
1) -     ) !       (   )
        $   #
  1) -    ! $ 
Activity 1: Answer questions about custom functions
9 E$

var
sNote : string;
iValue : integer;
rValue : real;
cSymbol : char;
1  !   $  $G (!   $ $ 
  !    
&
sNote := UpperCase(sNote);
&
@ A"
&
@  8  8"
&
sNote := Abs(iValue);
&
@  397-@ 9%9E H 9%99"

H ;  $   !  !  ! ! 
&
Sqr
&
UpCase
&
Copy
&
 
Activity 2: Use custom functions in a program
9 C    frmDisplayInfo_p    ! !   1
$         # !   I   
 !      ;   #   - * J K 
! !        /
5   !  =  % I  %  , !  &&
5   I      * %  ! &
5              !  %,( 
 (, !  &
   !  $   #  #     #
69

   
Activity 3: Use the Help facility and teach one another
;#    !      : !     !
$
5 E   9      RoundTo !      double.
5 E   H      Power !      extended.
+          !   
1L            ) 
      1L    "     
          ! M   

Result
D %9OPQ
&
D %9OPQ H&
D %9OPQ >&
D %@?R?H&
D %9R?

&
+)   
%
>&
%
@H&
%9OR?&
%@HRH&
%HR@H&
+)  +)
Activity 4: Use mathematical functions
9    $  ! 1 !  !  ! 
 /I !"* "
*"
&
+ 1
J K
"
&
+ 1
J K
K< . 1
J"
&
+< .+. H#K
 K"
&
+A/!!JA "
&
+< .A K
"
H ;        !  
&  S )
2
V ) V 
&
@W
X
%
2
@>&
) S
2a

&   S H  Y %ZV[&
   ! Z  [    $      
   (  $     9QR     
!        
&  S ?)
P
V H
9
& 1        9  HR %  &  
!& 1        ?  9? %  &  

   
70
Module
Activity 5: Use string functions
9 ;     !  !  ! !     
  29 H 2 88 29"
&
L L 9 9% 2"
&
iL := Length(sString);
&
L L 9 2 89% 2"
&
L 
L 99% 2"JJ .
 

&
sWord := Copy(sString,3,4);
!&
R .# & 2% L 
/%I"JJ6H   L 
  . .
Activity 6: Use custom functions in programming code
9 1        !
,/,   !    #  !
,/,    !   1 
      H   ; 
         
  !  ,/,     D 
      
H 0  #         ( !] !   
   $      1,< (  0  
DH?    % !  !   & %  (!        !
H    9     $   !   ^&
0              $ !  
            
5    !  !  
5    !  
5  $   !  
5        (        
5    !  
C  ! $          
  !       ( !]

         ! ?RR   ! ? !
D?Q?R ;           !    
  1        !        0  
       
> ;          !         
          
5     !    %  "  * !     " 
* !  &
5     !  I # !     
 ! !   $
 $    ! 
# C    
     $
 
D   
1) -
      
   1) - , 
the   ceil !  
71

   
  * !       $  
*         
? ;               _!
 $              _ %0 
             ! 
    
5     "   !     
5 5 0       !  # % &    
5 5      !    !  #     !
  
Rules and terminology regarding
procedures
1   
   
    !   !   
 )   "  #   #
  $            
1   
  =  
  )   
       !  !  
 $    !           
 )        `    !     
  %! &  $ ,    !   #  
      $  
Procedures that perform tasks
<#   ! ) '     I $     
!   #

.8
 T 
#
.X
"
begin
Randomize; JJ
     !  D !   #     
Beep"JJ
 #    
8
 #  <>?< . 8*%< . 8*%< . 8*"
8
 <5"
Sleep(200); JJ
     ! H  %HRR   &
8
 #  <>?< . 8*%< . 8*%< . 8*"
8
 <5"
Beep; JJ
 #    
ShowMessage(‘Disco done!’) ; JJ
            
end ;

   
72
Module
5       !     $   
5   ! !         !
        
,      #       !  # 
      %! ) #  ,    
  &
Names of
 
1 
   
!   
 !#    %   
 &    !   
     
1 $           $ I
     !# !  " !     
   $ !  
73

   
Procedures that return results
          $    +) 
! )

.$ 8/ +..#
.X
"
var
iNumber, iAdd : integer;
begin
iNumber := sedNum.Value ;
iAdd := sedAdd.Value ;
Inc(iNumber, iAdd);
 5 - 2  78"
end;
The Inc     )     ' 2   1 -! 
(      $  ! 2   HR   $  ! 1 
 ; 
(      )     2      
      $  ! H
  $  ! 1   

   !   !     (  ! $ 
   2 
;      %  &      
      $     # b1D  ! !  $
% & (!   Inc(    @      
 !   
sedNum
sedAdd
 "    
# var  ! !  
     reference
%$& 
;       
!   address of the
         
       !
  $      !
        
$ C     
     $  !   
   
;        $ 
parameter, the value !    
        
! #     ! 
$     $  !    
  M
  
 I $  # var 
! !    
   value parameter.
C ) !    !    !
                  

9 2'Z9"
Delete(sSentence, 9, 1);
A(  ^A
9 9%
%0"
A(  ^ A

   
74
Module
  1) -    ! $ 
 $ 2     $  !
   #  )  1L  # 
)   $  ! 2      ?R
  2 
,      #           !
parameters.
Activity 7: Determine the correct use of procedures
9 E$

var
sNote : string;
iValue : integer;
rValue : real;
cSymbol : char;
1  !   $  $G (!   $ $   
!    
&
7 
*"

&
T
"
&
Sleep(“tomorrow”);

&
sNote := Abs(iValue);
H ;  $   !  !  ! ! 
&
Delete
&


&
Val
Activity 8: Write Delphi programs using string functions and procedures
9 ;        
         
 $ !     
Pos !    Delete   
$  "    !   !
        

H ;            ! RQ
>>R>>HH %
 &   $      ! VHPQ
>>R>>HH

 ;        I  !    ! 00//
  $    ! 00003//3 0       
  L HRRR3R93R9
 ! !  
$  $  
 !  # C 
    
   $  
75

   
Study support

 !   "  !  !#!     !    
5 #   !    
         !      
5 $ 
(     !    !    !   #  )   "   
     !      $
5 #      
0   $          "   (  !    
       $     (!        ! 
                 
5 $   !          ! 
0      !   )         
  I $      
5 $    !  
*   $      !      
 ! 
Advantages of subroutines
> ;  )          
     
5 ,       $         FRdI
%  RdH Rd
 &      !      
   FRdI        !    %   
L  d&  ) RdH   RdHH  RdO   RdOO
;      #           !
%RdH&H
>?O9H   $      $ 
 

   
76
Module
Overview of the use of Units
   ! M      !     $$
!   !  
  !      $ 
       
$       !
    $    
  @$  
Object Oriented Programming % ! 
E 9H    &
1       !
 @$
 
 @$
 
   
   
    
 !   
programmer
 !
for the
the unit
name can
or
or
for the
the unit
name can
   
Code Units.
   
Form Units.
1 ) !     !  A
  $   9
/  !   $  
      
    !        
        !
      @
"     
-       
   ! )
  ;  Math, . . .
    
      
! )   ; 
/    ,     
77

   


5    !   
5 !  "  #
5  $      
The difference between subroutines and methods
       
  (, D   Di
       
  ,   , 
 @ %@ &  
!  !  $  !  
`     +) 
,  1 :  
$     !   
    $ (  
       
programmer.
5    !   
5 )   "  #   #
5  $     
        !
!  
   *    
      " 
$  !   
    
 $
 %  
 
 
& ' 
 
 
& " 
    
 
curriculum, but it will be used in
Chapter 6 to demonstrate the
    
    !

   
78
Module
Important terms and concepts
&  (& 
  
)      "  *  "  ! +
,  "       *   $ !     
     !         
$ 1       !   !     !  "  # 
 $   
'  1    !   "  $  !  `   
 
%   ;        $      $ 
       "     
covered in chapter 6.)
%   1       !   !      )   "  #
  #    $              
  ! !  
-    
   # !"
   
1     !  $       ! 
            #   $  !
    !
"          ! "  #    
$    
Unit 1   !    
5      !
5    
5  @"  
5   @"  
5    
    1    $       !     
        #        
$    M