Introduction to Procedural Programming - Part I

Writing Procedural Programs in QBASIC is simple. First lets understand what do you mean by procedural programs, what is benefit of it, why is it necessary in programming, its significance and how to all will be discussed in brief. You need to follow futher in the school text book. If anything like to know more from me please write to me.
Lets start.
Procedural word comes from procedure that means "set of steps to carryout some task". Ofcourse procedural programming is a such a tactics (approach) of write programs where we will divide our whole problem (program) manageably into many procedures that will be invoked (called) at the time of need in program to perform task as per required.
From above you might notice there are two aspects (components) in the procedural programming (programs) that are: (i) calling program and (ii) called program
Lets be clear about these two aspects first.
(i) What is calling program?
-> Basically the module program or sometime procedure (sub-program) itself may call another sub-program (procedure) or say pass control to it, such program is called calling program. Subroutines are called using optional statement "Call" and functions are invoked simply by using the function name either with "Print statement" or Assignment (Optional LET statement).
In other word we can also say that program from where subroutine is called or function is invoked is regarded as "Calling Program".
(ii) What is called program?
-> The subroutine or function (also say sub-program/procedure) that is called or invoked are regarded as "Called Program". Subroutine is enclosed within SUB...END SUB statements and Function is enclosed within FUNCTION...END FUNCTION statement. One more point to be noted that there is another kind of user defined function as well which resides in the module only cannot be allocated as seperate procedure that is DEF  FN... END DEF statement.

In the next post I will distinguish between SUB and FUNCTION and FUNCTION and DEF FN.
Before please check your text book to get into futher topics. It will be very helpful to understand futher.

Be patient and revise again and again to you can grab the discussions here.

Good Luck.
Have a fun. Enjoy your time with your friends.

Counting vowels in the given string using User Defined Functions

DECLARE FUNCTION countvow(s$)
CLS
INPUT "Enter testing string=";v$
PRINT "Number of vowels=";countvow(v$)
END

FUNCTION countvow(s$)
FOR i=1 TO LEN(s$)
  ch$=LCASE$(MID$(s$, i, 1))
  SELECT CASE ch$
  CASE "a", "e", "i", "o", "u"
     cnt=cnt+1
  END SELECT
NEXT i
countvow=cnt
END FUNCTION

Generate Fibonacci series using Sub Routine

REM a, b, n parameters represents first term, second term and number of terms respectively
DECLARE SUB fibo(a, b, n)
CLS
INPUT "First Term="; x
INPUT "Second Term=";y
REM label number is for data validation that length of terms must be less or equal to 20
10:
INPUT "How many terms="; z
IF z>20 THEN 10
CALL fibo(x, y, z)
END


SUB fibo(a, b, n)
PRINT a, b,
REM first two terms are already printed so started from 3
FOR I = 3 to n
  s=a+b
  PRINT s,
  a=b
  b=s
NEXT I
END SUB

Sum of digits of a number using User Defined Function (UDF)

DECLARE FUNCTION digitsum(n)
CLS
INPUT "Enter testing number=";v
PRINT "Sum of ";n;" is "; digitsum(v)
END

FUNCTION digitsum(n)
a=n
WHILE NOT a=0        'writing NOT a=0 is same as writing a<>0
  r=n MOD 10
  s=s+r
  n=int(n/10)
WEND
digitsum=s
END FUNCTION

Summing up digits of provided number

Rem Sum of digits of Number Example: Sum of digits of 254 is 11
CLS
INPUT "Number=";n
a=n
WHILE a<>0
  r=a MOD 10
  s=s+r
  a=int(a/10)
WEND
PRINT "Sum of  digits of ";a; " is ";s
END

Testing Armstrong Number using User Defined Function

DECLARE FUNCTION arm(n)
CLS
INPUT "Enter a testing number=";v
IF arm(v)=1 THEN
  PRINT v;" is Armstrong Number"
ELSE
  PRINT v; " is not Armstrong Number"
ENDIF
END

FUNCTION arm(n)
f=0
a=n
WHILE a<>0
  r=a MOD 10
  s=s+r^3
  a=int(a/10)
WEND
IF s=n THEN f=1
arm=f
END FUNCTION

Testing Armstrong Number

Rem Number which is equal to the sum of cube of its digits is said to Armstrong Number
CLS
INPUT "Enter testing number=";n
v=n
WHILE V<>0
  r = v MOD 10
  s=s+r^3
  v=int(v/10)
WEND
IF n=s THEN
  PRINT n; " is Armstrong Number"
ELSE
  PRINT n; " is not Armstrong Number"
ENDIF
END

Testing Palindrome

CLS
INPUT "Enter string to be tested: ";t$
t$=LCASE$(t$)
FOR I=LEN(t$) TO 1 STEP -1
  r$=r$+MID$(t$, I, 1)
NEXT I
IF r$=t$ THEN
  PRINT t$; " is Palindrome!"
ELSE
  PRINT t$; " is not Palindrome!"
ENDIF
END

Testing Palindrome Using Function...End Function

DECLARE FUNCTION rev$(s$)
CLS
INPUT "Enter string to be tested"; t$
t$=LCASE$(t$)
IF t$=rev$(t$) THEN
  PRINT t$;" is Palindrome!"
ELSE
  PRINT t$;" is not Palindrome!"
ENDIF
END

FUNCTION rev$(s$)
FOR I = LEN(s$) to 1 STEP -1
  r$=r$+MID$(s$, I, 1)
NEXT I
rev$=r$
END FUNCTION