Procedures
Procedure is a set of commands which performs a specific task. Procedures are called from main FoxPro program. Procedures offers several advantages. As program grow, number of tasks increases and so need of procedure grows.
As task are added to a program, flow of program becomes difficult to follow. Procedures help to divide programs into small units that are easy to analyze, write and maintain. User can execute common blocks of code from several places. Hence same code can be reused, which saves coding time.
Creating Procedure
Procedure can be coded in the same program i.e. main program or several procedures can be kept under a single program file. Procedures can have any valid FoxPro name. Mostly procedure name should describe its purpose.
Return Statement
The last statement in procedures is a return statement. It indicates the end of the procedures and returns the control to the calling routine. Syntax:
Procedure <Procedure Name>
--- - - - - -- - - --
- - - - - - - - -- - - à FoxPro Code
- - - - - - - - -- - -
Return
If procedures are kept in a separate program file, FoxPro has to be instructed to pick them up from that particular file. With the following command
Set Procedure to <File Name>
Procedures can be called in the main program by
Do <Program Name> Command.
When FoxPro finds a procedure, it executes it until RETURN is encountered. RETURN makes FoxPro Exit and return the control to the caller.
Exiting Procedures
Exiting from procedures can be done with following two commands.
1) CANCEL
Cancel terminates the program. It returns the control to the Command Window and releases all memory variables. Cancel allows to exit an application while remaining in FoxPro.
2) QUIT
Quit also terminates current program, but it quits from main FoxPro and returns to Window.
Return to Master
Memory variables defined in the main program are available to other procedures but memory variables used or defined in other procedures are not available to the main program.
Thus the memories occupied in procedures are maintained as follows.
- Memory variables defined in the main program are available to all other procedures.
- Memory variables defined in all of the other procedures are not available to main program.
Example 1
Set talk off
Set stat off
Clear
Mnum =10 à Memory Variable in main program.
Do Ch à Procedure Ch
? Mnum
Return
Procedure Ch à A procedure/ Program
Mnum=Mnum+5
Return
NOTE: FoxPro treats a program as a procedure.
Example 2 Passing parameters for memory variables.
Set talk off
Set stat off
Clear
Mnum =10
Mcar = “Hello”
Mdate ={ 01/01/1998}
Do Disp with Mnum, Mcar, Mdate à à Procedure
Return
Procedure Disp
Parameters A, B, C
?A
?B
?C
Return
Example 3
Set talk off
Set stat off
Clear
Mnum1=0
Mnum2=0
Mcar1=space(20)
Mcar2=space(20)
@ 10,10 say “Enter 1st No “ get Mnum1
@ 12,10 say “Enter 2nd No “ get Mnum2
Read
Do AddUp with Mnum1 +Mnum2 à à Procedure
@ 18,10 say “Sum Is “ +str (Mnum1)
@ 14,10 say “Enter String1 “ get Mcar1
@ 16,10 say “Enter String2 “ get Mcar2
Read
Do AddUp with Mcar1 +Mcar2 à à Procedure
@ 20,10 say “Result of Concatenation “ +str (Mcar)
Wait Window
Set talk on
Return
Procedure AddUp
Parameters A, B
A = A+B
Return
Example 4
Main.Prg
Set talk off
Set stat off
Clear
Mnum=0
Store 1 to mpush
@ 6,10 get mpush picture ‘@* Add; Subtract ; Multiply; Divide; Okay; Quit’
Read
Do case
Case mpush=1
Do Add
Case mpush=2
Do Subtract
Case mpush=3
Do Multiply
Case mpush=4
Do Divide
Case mpush=5
Do Cancel
Case mpush=6
Do Quit
Endcase
Return
Procedure Add
No1=0
No2=0
@ 10,10 say “Enter No1 “ get No1
@ 12,10 say “Enter No2 “ get No2
Read
Mnum = No1 + No2
? Mnum
Return
Procedure Subtract
No1=0
No2=0
@ 10,10 say “Enter No1 “ get No1
@ 12,10 say “Enter No2 “ get No2
Read
Mnum = No1 - No2
? Mnum
Return
Procedure Multiply
A=0
B=0
@ 10,10 say “Enter No1 “ get A
@ 12,10 say “Enter No2 “ get B
Read
Mnum = A* B
? Mnum
Return
Procedure Divide
N=0
M=0
Mname=space(10)
@ 10,10 say “Enter Your Name “ get Name
Read
@ 12,10 say “Enter No1 “ get N
@ 14,10 say “Enter No2 “ get M
Read
Mnum = N / M
? “Hello “ + Name
? Mnum
Return
Procedure Okay
?”Welcome to Procedure” Font “Times New Roman”, 16
? “Good Bye”
Cancel
Procedure Quit
?”Welcome Again”
Wait Window “Good Bye”
Quit
No comments:
Post a Comment