QBASIC arguments in modular programming for class 10
What is modular programming?
What are the arguments in modular programming?
How to pass arguments in QBASIC
To pass arguments to a procedure in QBASIC, you simply list them inside the parentheses of the CALL statement. The arguments are separated by commas. For example, the following code calls the add procedure and passes it the two arguments 10 and 20:
SUB add(a AS INTEGER, b AS INTEGER)
END SUB
CALL add(10, 20)
When the add procedure is called, the values 10 and 20 are assigned to the parameters a and b respectively. The add procedure then adds the two values together and prints the result to the screen.
Why use arguments in modular programming?
- Flexibility: Using arguments, you can pass different values to a procedure each time it is called. This increases the procedure's flexibility and reusability.
- Encapsulation: Arguments allow you to hide a procedure's internal implementation from the calling module. This strengthens the process and makes it easier to maintain.
- Efficiency: Passing arguments by value can enhance program performance by minimizing the number of memory copies required.
Example:
1
Note: In above program, constant values are sent to the sub module.
Example: 2
Program for without declaring parameters and without passing arguments.
DECLARE SUB sum ()
CLS
CALL sum
END
SUB sum
INPUT "Enter any two number ="; a, b
s = a + b
PRINT "Sum="; s
END SUB
Note: In above example parameters are not declare and argument also not passing.
Passing Array argument to sub procedure
WAP to input any five numbers and display them in reverse order.
DECLARE SUB number (n())
CLS
DIM n(5)
FOR x = 1 TO 5
INPUT "Enter number="; n(x)
NEXT
CALL number(n())
END
SUB number (n())
FOR x = 5 TO 1STEP -1
PRINT n(x)
NEXT
END SUB
Note: In above program, the user input 5 different numbers in an array variable n(x) and passes it to the sub procedure. The sub procedure displays them in reverse order as output.
No comments:
Post a Comment