Actual and Formal Parameters in QBasic

Codynn
4 Min Read

Introduction

Programming languages are built on important ideas that help developers make software. One of these ideas is about parameters, which are like tools that help programmers create solutions. QBasic, a version of the BASIC programming language, is really good for beginners to learn from. In this blog post, we’ll explore the concepts of actual and formal parameters in QBasic. These ideas are important, and we’ll use examples to help us understand them better.

Demystifying Actual and Formal Parameters

Formal Parameters:

Formal parameters, also called formal arguments, are like empty slots inside a function or subroutine. They work as blueprints that tell the function what kind of information to expect when it’s called. These placeholders set up the format for the data that will be given to the function.

Actual Parameters:

Actual parameters, which can also be called actual arguments, are the actual values that you give to a function or subroutine when you use it. These values are given by the code that calls the function, and they fill in the empty slots that were set up by the formal parameters.

Illustrative Code Examples

Let’s unravel the concepts of actual and formal parameters in QBasic by exploring some practical code examples.

Example 1: Basic Subroutine Call

SUB DisplayMessage(message AS STRING)
    PRINT message
END SUB

' Calling the subroutine with an actual parameter
DISPLAYMESSAGE "Hello, World!"

In this example, there’s a part called DisplayMessage that uses a placeholder called message. When we use DisplayMessage, we put in the real message like “Hello, World!” which takes the place of that placeholder. The real message goes into the empty slot where the placeholder was.

Example 2: Function with Return Value

FUNCTION CalculateSum(a AS INTEGER, b AS INTEGER) AS INTEGER
    RETURN a + b
END FUNCTION

' Calling the function with actual parameters
LET x = 5
LET y = 3
sum = CALCULATESUM(x, y)
PRINT "Sum:", sum

In this situation, there’s a function called CalculateSum that needs two things (a and b) to work with. When we use CalculateSum, we give it real values like x and y. The function then does its thing and gives back an answer, which we put into the sum variable.

Example 3: Nested Subroutine Calls

SUB PrintRectangle(width AS INTEGER, height AS INTEGER)
    FOR i = 1 TO height
        FOR j = 1 TO width
            PRINT "*";
        NEXT j
        PRINT
    NEXT i
END SUB

SUB PrintSquare(size AS INTEGER)
    CALL PrintRectangle(size, size)
END SUB

' Calling the nested subroutines with actual parameters
PRINTSQUARE 4

In this instance, the PrintSquare action uses PrintRectangle with the same measurements for width and height to make a square. The way actual and formal parameters work together also applies smoothly when one subroutine is called from inside another.

Conclusion

Understanding actual and formal parameters in QBasic is really important if you want to make organized and efficient programs. When you share values between different parts of your code, you can create programs that can change and adjust as needed. This post explained the basic ideas of actual and formal parameters with helpful code examples. As you get better at programming, having a strong understanding of these basic ideas will help you make more complex and strong programs using QBasic.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *