chap11 - QBASIC Chapter 11 QBASIC Chapter 11 Subprograms...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: QBASIC Chapter 11 QBASIC Chapter 11 Subprograms, Functions, and Chaining Created by Richard D. Taylor Illustrated by Saundra J. Taylor Introduction Introduction ­ Subprograms - more independent than Subprograms subroutines and are often reusable by other programs programs ­ Functions - called the same way a Qbasic Functions function such as SQR by using its name in an expression expression Introduction Introduction ­Procedures - subprograms and functions are Procedures collectively procedures collectively ­Main program - code that calls the highest-level Main procedures procedures ­ Chaining - linking different programs together Chaining through the CHAIN statement through Subprograms Subprograms ­ - a unit of code delimited by the SUB and unit END SUB statements and called by the CALL statement ­ END SUB statement transfers control back END to the calling program to ­ Subroutines Versus Subprograms ­Subroutines and Subprograms are very similar Subroutines in their operation in Subprograms Subprograms ­ Types of Subprograms ­Two types of subprograms: Internal ­ External ­ ­Internal subprogram - call an associated Internal subprogram is one that belongs to the same unit of code as the main program of Subprograms Subprograms ­External subprogram - is independent from the External calling program and resides in auxiliary storage in one of the following three forms: in a separate and distinct program ­ part of another program ­ A Qbasic library ­ Subprograms Subprograms ­ Form: SUB subprogramname ­ END SUB ­ Purpose: defines subprogram The Purpose: subprogram is called by a CALL statement from the main program or from another subprogram subprogram ­ Examples: ­SUB Wrap.Up ­SUB Compute.Gross (Gross, Hours, Rate, Dependents) Subprograms Subprograms ­ Form: subprogramname (ax…by) ­ subprogramname is the name of the subprogram ­ Purpose: Purpose: Causes control to transfer to a subprogram represented by subprogramname Causes the location of the next executable statement following the CALL to be retained statement ­ Examples: ­CALL Wrap.UP ­CALL Print.Detail(E.Name$, Age, Date.In$) Subprograms Subprograms ­ Parameters and Argument ­Major difference between subroutines and Major subprograms subprograms Subroutines - all variables defined in the program Subroutines are global variables, available to all modules throughout the program throughout ­ Subprograms - variables are by default local Subprograms variables, available only to the subprogram in question question ­ Subprograms Subprograms ­Parameters in a SUB statement must be a Parameters variable variable ­Argument in a CALL statement can be a Argument constant, variable, expression, array element, or complete array complete ­ Passing No Values to a Subprograms ­Not all subprograms require that the calling Not program pass values because it lacks an argument list argument Subprograms Subprograms ­ Passing Constants and Expressions ­Constants and expressions can appear in the Constants argument list of a CALL statement argument Numeric constants and expressions can only be Numeric passed to numeric variables passed ­ String constants and expressions can only be passed String to string variables to ­ Subprograms Subprograms ­ Passing Variables and Individual Array Passing Elements Elements ­The values of simple variables and individual The array elements can be passed to the parameters in a SUB statement the same way as constants and expressions are passed and Subprograms Subprograms ­Pass-by-reference - all values of the variables Pass-by-reference in the parameter list of a SUB statement are passed back to the variables in the argument list of a CALL statement of ­Pass-by-value - only values of the argument are Pass-by-value passed not the address passed Subprograms Subprograms ­ Passing an Complete Array ­Can pass all the elements of an array by Can including the array name followed immediately by left and right parentheses in the argument list of a CALL statement list ­ Passing Values Between Subprograms ­Pass values to a lower-level subprogram by Pass using the argument-parameter-list technique using Subprograms Subprograms ­ Sharing Variables with Individual Sharing Subprograms Subprograms ­SHARED and COMMON statements can share SHARED variables variables Subprograms Subprograms ­ Form: SHARED v1…vx ­ Purpose: Purpose: shares the value of variables in both directions between a subprogram or function and the main program function ­ Examples: ­SHARE A, B, C ­SHARE Factor () Subprograms Subprograms ­ Sharing Variables with All Associated Sharing Subprograms Subprograms ­ The COMMON statement can make selected The variables global to all associated subprograms variables Subprograms Subprograms ­ Sharing Variables with Individual Subprograms Sharing ­ Form: COMMON attribute v1…vx ­ Purpose: Purpose: identifies variables that are to be shared between the main program and all associated subprograms, or between the main program and external subprograms program ­ Examples: ­COMMON SHARED Sum, Count, Amount ­COMMON Rate(), Cost Using the Qbasic Editor to Enter Using the Qbasic Editor to Enter Subprograms ­ The QBASIC Editor includes all the tools The you need to enter, modify, and print subprograms subprograms ­Editing Subprograms Select the SUBs command (F2) in the View Menu Select and then select the name of the main program or subprogram to display subprogram ­ Press Shift + F2 to display the next subprogram in Press alphabetical order by name alphabetical ­ Using the Qbasic Editor to Enter Using the Qbasic Editor to Enter Subprograms ­ Splitting the View Window ­to edit the main program and a subprogram or to two subprograms two Select the Split command in the View Menu ­ Press F6 to activate the lower window ­ Press F2 or select the SUBs command in the View Press window, when the dialog box displays, select the subprogram you want to display in the second window and press the Enter Key window ­ Using the Qbasic Editor to Enter Using the Qbasic Editor to Enter Subprograms ­ Saving, Loading, and Executing the Main Saving, Program and Associated Subprograms Program ­Save As command in the File Menu to save the Save main program and associated subprograms main ­Open command in the File Menu to load the main Open program and associated subprograms from disk into main storage main ­Press Shift + F5 while the main program or any one Press of the associated subprograms is in the view window of Using the Qbasic Editor to Enter Using the Qbasic Editor to Enter Subprograms ­ Printing the Main Program and Associated Printing Subprograms Subprograms ­QBasic prints the main program first followed QBasic by the subprograms in alphabetical order by name name Functions Functions ­ The FUNCTION and END FUNCTION The statements are similar to the DEF FN and END DEF statements in that they allow you to create user-defined functions Functions Functions ­ Form: FUNCTION functionname v1…vx STATIC STATIC ­ END FUNCTION ­ Purpose: defines a function that is relevant to a Purpose: particular application that can be called as frequently as needed frequently ­ Examples: ­ FUNCTION AREA (Side1, Side2) Functions Functions ­ An Example of a Recursive Function ­The symbol n! is read n factorial - defined as The the product of all integers between 1 and n the Functions Functions ­ Using the QBasic Editor to Enter Functions ­Entered into the PC the same way as a Entered subprogram except that you type the keyword FUNCTION rather than SUB ­You can also initiate a function by selecting the You NEW FUNCTION command in the Edit menu NEW Chaining Chaining ­ Chaining - writing external subprograms Chaining that are linked together by the CHAIN Statement. Statement. ­ Chaining Program - COMMON Statement Chaining is used in conjunction with the CHAIN statement to pass selected variables from the current program the Chaining Chaining ­ Form: CHAIN “filespec” ­ Purpose: instructs the PC to stop executing Purpose: the current program, load into main storage another program from auxiliary storage, and start executing the new program start ­ Examples: ­CHAIN “B:PROG2” ­CHAIN “PROG3” Summary Slide Summary Slide ­ Introduction ­ Subprograms ­ Using the Qbasic Editor to Enter Subprograms ­ Functions ­ Chaining ...
View Full Document

This note was uploaded on 06/06/2011 for the course MIS 105 taught by Professor Grigoletti during the Spring '11 term at Moraine Valley Community College.

Ask a homework question - tutors are online