The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Oasis Publication, 2023-10-02 03:54:58

Computer 10 New

Computer 10 New

Approved by Curriculum Development Centre 201 Oasis Radiant Computer Science, Book 10 Statement/Statement blocks Next<counter> Here, counter is a numeric variable that controls the loop. It is also called control variable. The execution of the loop begins with assigning of the initial and sentinel value with the step to increase or decrease the value of the counter variable by n. If step is not defined then by default QBASIC assumes STEP as ‘1’ and will increase the value of counter variable by 1. REM Program to print first 20 natural numbers horizontally. CLS FOR I = 1 TO 20 PRINT I; NEXT I END REM Program to print Odd numbers from 49 to 5. CLS FOR I =49 TO 5 STEP-2 PRINT I; NEXT I END REM Program to print your school’s name for 20 times. CLS INPUT “Enter Your School’s Name “; N$ FOR I = 1 TO 20 PRINT “My School’s Name Is “; N$ NEXT I END EXIT FOR Statement This statement exits a FOR loop.


Oasis Radiant Computer Science, Book 10 202 Approved by Curriculum Development Centre Syntax : EXIT FOR Example : FOR A =1 TO 20 STEP 1 PRINT A; IF A = 14 THEN EXIT FOR NEXT A WHILE …WEND LOOP This kind of looping structure executes a series of statements as long as the given condition is true. If the condition is false, then the loop will skip and other statements following WHILE … WEND statements will be executed. ‘WHILE’ keyword is followed by the condition. Syntax : While<condition> Statements/ Statements Block WEND Example : REM Program to print Even numbers from 2 to 50. CLS L =2 WHILE I<=50 PRINT I; I = I + 2 WEND END Example : REM Program to generate the series 7,14,21 ………… 70. CLS


Approved by Curriculum Development Centre 203 Oasis Radiant Computer Science, Book 10 I = 7 WHILE I<=70 PRINT I; I = I+7 WEND END Example : REM Program to print sum of the numbers up to the entered number. CLS INPUT “Enter any number”; N I = 1 S = 0 WHILE <=N S = S + 1 I =I + 1 WEND PRINT S END DO … LOOP statement: It repeats a statement or a block of statements while a condition is true or until a condition becomes false. The WHILE or UNTIL keyword (any one of them) is used with DO …. LOOP. IF we use “WHILE” the loop terminates when the given condition evaluates to be false whereas if we use “UNTIL” the loop terminates when the given condition evaluates to be true. Syntax: DO {WHILE/UNTIL} STATEMENT OR STATEMENTS BLOCK LOOP


Oasis Radiant Computer Science, Book 10 204 Approved by Curriculum Development Centre OR DO Statement or Statements block LOOP {WHILE/UNTIL} EXAMPLE : Program to print first 20 odd numbers. a. Using DO WHILE … LOOP CLS I = 1 DO WHILE I<=19 PRINT I I=I+2 LOOP END b. Using DO … LOOP WHILE CLS I=1 DO PRINT I I=I+2 LOOP WHILE I<=19 END c. Using DO UNTIL … LOOP CLS I=1 DO UNTIL I>19


Approved by Curriculum Development Centre 205 Oasis Radiant Computer Science, Book 10 PRINT I I=I+2 LOOP END d. Using DO … LOOP UNTIL CLS I=1 DO PRINT I I=I+2 LOOP UNTIL I>19 END EXIT DO Statement This statement ends a DO LOOP Syntax : EXIT DO Example : N=1 DO PRINT N; IF N=8 THEN EXIT DO N=N+1 LOOP WHILE N<=10 Functions of QBASIC Functions are ready made programmes, which take some data, manipulate them and return either numeric or string value. They are the programmes, that calculate or manipulate data and return a value. In another word a function is a built-in


Oasis Radiant Computer Science, Book 10 206 Approved by Curriculum Development Centre formula to perform a certain task. Once a function is written it can be used again and again. There are two types of function supported by QBASIC. They are: a. User defined functions b. Library or built-in functions User defined functions are written by the programmer to do the specific task. We can use FUNCTION … END FUNCTIONS statement to create a user defined function. Details about user defined function will be studied in the next chapter. Library or built-in functions are functions provided by the QBASIC to do specific tasks. Library functions can be called (used) by the user according to the requirement. Some common examples of library functions are LEN, LEFT$, SQR, etc. According to the return value, functions are classified into two types they are a. Numeric functions b. String functions a. Numeric Functions This function returns a number or a numeric value as a result of calling them. We can use them in place of numeric variables or numeric constant. So these are called numeric functions. It is possible to give the arguments as string or numeric form but it always returns the value in numeric form. Some of the numeric functions are: S.N. Function name Syntax Work 1 ABS ABS(x) Returns absolute values. It is the actual number without sign. Eg. Absolute value of -5 is 5 2 EXP Exp(x) It gives the exponential value 3 FIX FIX(x) It gives the integer part of value by truncating the number. It ignores the decimal part and displays only the whole number.


Approved by Curriculum Development Centre 207 Oasis Radiant Computer Science, Book 10 4 HEX$ HEX$(x) It gives the value after converting decimal to hexadecimal value. 5 INT INT(x) It gives the whole number part of the given number. 6 LOG LOG(x) It returns the natural logarithmic number and number should be greater than zero. 7 OCT$ OCT$(x) It returns the value after converting into octal number. 8 RND RND It returns and displays the random numbers. 9 SGN SGN(x) It returns the sign of number like –1 , +1 or 0 for negative, positive and zero number. 10 SQR SQR(x) It returns the square root of given number. 11 COS COS(x) It returns the value of Cosine in radian. 12 SIN SIN(x) It returns the value of Sin in radian. 13 TAN Tan(x) It returns the value of Tangent in radian. Some Programmes ABS REM use of ABS function Result= ABS(-98) PRINT “ The absolute value is”, Result END Output 98 After executing the program we will get output as 98 only because absolute value of -98 is 98. EXP REM use of EXP function Result= EXP(5) PRINT “ The exponential value is”, Result


Oasis Radiant Computer Science, Book 10 208 Approved by Curriculum Development Centre END Output 148.4132 After executing the program we will get output as 148.4132 because exponential value of 5 is 148.4132. (It is value of e to power 5 ie. E=2.718282… to power 5) FIX REM use of FIX function Result= FIX(3.9) PRINT “ The fixed value is”, Result END Output 3 After executing the program we will get output as 3 HEX$ REM use of Hexa$ function Result= HEX$(1234) PRINT “ The Hexadecimal value is”, Result END Output 4D2 INT REM use of INT function R1= INT(5.56) R2=INT(-5.56) R3=INT(10/3) PRINT “The Integer values are”, R1,R2,R3


Approved by Curriculum Development Centre 209 Oasis Radiant Computer Science, Book 10 END Output 5 -5 3 After executing the program we will get output as 5 -5 3 because these are the integer part of given values. LOG REM use of LOG function Result= LOG(10) PRINT “ The Logarithmic value is”, Result END Output 2.302585 OCT$ REM use of Octal function Result= OCT$(1234) PRINT “ The Octal value is”, Result END Output 2322 RND REM use of Random function FOR T= 1 To 10 PRINT RND*10 Next T END Output It displays the random numbers like -32768 to 32767 ….. …. SGN REM use of SGN function


Oasis Radiant Computer Science, Book 10 210 Approved by Curriculum Development Centre Result= SGN(-5) PRINT “The sign value is”, Result END Output -1 It shows the negative value with sign -1. SQR REM use of SQR function Result= SQR(16) PRINT “ The Square root value is”, Result END Output 4 COS REM use of COS function Result= COS(60) PRINT “ The value of Cosine in radian is”, Result END Output 12 SIN REM use of SIN function Result= SIN(60) PRINT “ The value of Sin in radian is”, Result END Output 32


Approved by Curriculum Development Centre 211 Oasis Radiant Computer Science, Book 10 TAN REM use of TAN function Result= TAN(60) PRINT “The value of Tangent in radian is”, Result END Output 3 b. String Functions The functions that return the value in the form of string are string functions. These are very useful for string manipulation. We can compare two strings; can count the length of the string etc. using such functions. Major string functions are: S.N Function name Syntax Work 1 ASC ASC(String) It returns the ASCII(American Standard Code for Information Interchange) value of given string. 2 CHR$ CHR$(x) It returns the character represented by the ASCII code. It is just reverse of ASCII. 3 INSTR INSTR(N,A$,B$) It searches one string on another string. If found, it returns the position. 4 LEN LEN(String) It displays the length of string 5 LEFT$ LEFT$(String, N) It displays the Nth number of characters from left side. 6 RIGHT$ RIGHT$(String, N) It displays the Nth number of characters from right side. 7 MID$ M I D $ ( S t r i n g , N1,N2) It displays the characters from N1 position to N2 position. 8 STR$ STR$(x) It changes a numeric value into string value.


Oasis Radiant Computer Science, Book 10 212 Approved by Curriculum Development Centre 9 SPACE$ SPACE$(N) It is used to add a specified spaces after any value up to Nth number. 10 SPC SPC(x) It gives fixed number of space 11 STRING$ STRING$(X,Y) It is used to get a string of specified length whose all characters are made of a specified character. 12 VAL VAL(String) It is used to convert the string value into numeric value. 13 LTRIM$ & RTRIM$ LTRIM$(String Expression) RTRIM$(String Expression) To remove the leading and trailing blanks from the string expressions. 14 LCASE$ & UCASE$ LCASE$(String) UCASE$(String) To change he string into uppercase and lower case. Some programmes ASC REM use of ASC function Result= ASC(S) PRINT “ The ASCII value is”, Result END Output 83 (The ASCII value of S is 83) CHR$ REM use of CHR$ function Result= CHR$(83) PRINT “ The character of given ASCII value is”, Result END Output S (The ASCII value of S is 83)


Approved by Curriculum Development Centre 213 Oasis Radiant Computer Science, Book 10 INSTR REM use of INSTR function N1$=”Suyash Shankar” N2$=”Shankar” PRINT INSTR(N1$,N2$) END Output Above program, it starts to search N2 string on the N1 string. It finds on “Shankar” on the 8th position of first string and displays the value as 8. LEN REM use of LEN function N1$=”Suyash Shankar Adhikary” L=LEN(N1$) PRINT “Length of string including space is “,L END Output 23 LEFT$(String) REM use of LEFT$ function N1$=”SUYASH” L$=LEFT$(N1$,3) PRINT “String from left side is “,L$ END Output SUY RIGHT$(String) REM use of RIGHT$ function N1$=”SUYASH”


Oasis Radiant Computer Science, Book 10 214 Approved by Curriculum Development Centre R$=RIGHT$(N1$,4) PRINT “String from Right side is “,R$ END Output YASH MID$(String,N1,N2) REM use of MID$ function N1$=”SUYASH” M$=MID$(N1$,2,3) PRINT “String from Right side is “,M$ END Output UYA STR$(String) REM use of STR$ function V$=STR$(99) PRINT “Our string is”, V$ END Output 99 but not in numeric form but in string form. SPACE$(x) REM use of SPACE$ function N1$=”SUYASH” N2$=”ADHIKARY” S$=SPACE$(10) PRINT N1$, S$, N2$ END Output SUYASH ADHIKARY (There is a space of ten characters between SUYASH and ADHIKARY) SPC(x)


Approved by Curriculum Development Centre 215 Oasis Radiant Computer Science, Book 10 REM use of SPC function N1$=”SUYASH” PRINT “Name”; SPC (10); N1$ END Output NameSUYASH (We space up to ten characters between Name and SUYASH) VAL(x) REM use of VAL function N$=”200” PRINT VAL(N$)+300 Output 500 (First 200 is accepted as string but it is converted into numeric with VAL and it becomes possible to add it with 300) STRING$ REM use of STRING$ function PRINT STRING$(10,65) END Output AAAAAAAAAA (The ASCII code of 65 is A and it is printed ten times) LTRIM$ and RTRIM$ REM use of LTRIM$ and RTRIM$ function N$=” SUYASH “ PRINT LTRIM$(N$), RTRIM$(N$) END Output SUYASHSUYASH Here LTRIM$ removes the gap of left side of string N$ and RTRIM$ removes the gap of right side of string N$ so we have seen both strings are displayed


Oasis Radiant Computer Science, Book 10 216 Approved by Curriculum Development Centre without any gap. LCASE$ and UCASE$ REM use of UCASE$ and LCASE$ function N1$=”SUYASH“ N2$=”adhikary“ PRINT LCASE$(N1$), UCASE$(N2$) END Output suyash ADHIKARY First string is converted into lower case ie in small letters and other is converted into uppercase ie in capital letter. Special Functions Besides the above functions we have some more special functions which are used for special works. These are: S.N. Function name Syntax Work 1 DATE$ DATE$ = Expression string To print date 2 TIME$ TIME$= Expression string To print time 3 INPUT$ INPUT$ To accept string input 4 INKEY$ INKEY$ To accept the character


Approved by Curriculum Development Centre 217 Oasis Radiant Computer Science, Book 10 Points to remember • Statements are the group of commands given to the computer to perform some actions like CLS, INPUT, PRINT, REM, END. • Input/ Output statements give data to the computer and get results from the computer. • Declaration statements declare a variable in the programme. • The statements which are used to assign a numeric or string value to a variable are called assignment statements. • Statements are used to control the flow of execution of the programme statements. • Branching and jumping takes the programme from one location to another within the programme. • The term looping means repeating execution of a sequence of statements in a programme. • Logical operators are used to combine two or more expressions containing relational operators. • An expression can be a variable or constant or combination of constants, variable with operators. Exercises 1. Fill in the blanks: a. The ………………………… statement is used to assign values to the variables. b. IF … THEN statement is ……………. statement. c. FOR … NEXT is …………….statement. d. ………………….. is a programme that calculates data and returns a value. e. There are two types of functions; ………………. and …………… f. ………………………….. command is used to list files. g. To delete a file ………………………… command is used.


Oasis Radiant Computer Science, Book 10 218 Approved by Curriculum Development Centre 2. Write True of False: a. DIM statement is Input/Output statement. b. LET is an optional statement. c. QBASIC supports looping statements. d. DATA is an executable statement. e. The value of symbolic constant can be changed during the execution of the programme. f. The functions created by the user are called user defined functions. g. SQR function returns square root of negative numbers. h. Numeric functions return string values. i. File system commands are used to manage files. j. To create a directory MKDIR command is used. 3. Write the syntax and use of the following statements. a. INPUT Statement b. DIM statements. c. LET Statement. d. READ … DATA Statement. e. GOTO f. IF … THEN Statement. g. FOR … NEXT statement h. DO … LOOP. i. INSTR Statement j. LTRING$ function k. CINT function l. STRING$ function 4. Answer the following questions. a. What is a statement? Give examples. b. Briefly explain the types of statement. c. Define branching and jumping. d. Define control statement with example. e. What is looping? GIVE examples of looping statements. f. What is nested loop? g. Differentiate between DATE$ function and DATE$ statement. h. Differentiate between TIME$ function and TIME$ statement. 5. Write programmes to:


Approved by Curriculum Development Centre 219 Oasis Radiant Computer Science, Book 10 a. Input three different numbers and print their product. b. Input length and print area of square. c. Input Indian currency and convert it into Nepali currency. d. Input a number and print its square and square root. e. Calculate area of rectangle if length is 18cm. and breadth is 10cm. f. Input any five numbers and print total and average. g. Check whether a number is od or even. h. Display welcome to QBASIC 10 times using WHILE … WEND. i. Print first 20 even numbers using DO …. LOOP. j. Find out factorial of a given number using FOR ….NEXT. K. Input marks of all the subjects and print total, percentage, result and division. l. Input a number and check whether it is perfect square number or not [Hint:- SQR(N)=INT(SQR(N))] m. Input initial velocity, time taken and acceleration and calculate distance travelled by the body. [S=UT+1/2T2 ] 6. Write a 0 to generate the following series up to 10 terms. a. 2, 4, 6, 8 ……………… b. 1, 1, 2, 3, 5, 8 …………….. c. 1, 8, 27, 64 ……………. d. 3, 12, 27, 48 …………… e. 100, 98, 96, 94 ………… f. 100, 98.5, 97, 96.5 ……………. g. 1, 4, 9 …………… j. 4, 4, 12, 20 ……………. h. 5, 25, 125 ……… upto 5th term i. 1, 5, 9, 13, 17, …………….. j. 7, 22, 11, 34, 17 ……………. k. 9, 7, 5, ……………… , 1 upto 5th term. 7. Write a programme to display the given output. a. 12345 b. 1 c. 5 1234 22 54 123 333 543 12 4444 5432 1 55555 54321


Oasis Radiant Computer Science, Book 10 220 Approved by Curriculum Development Centre b. 1, 8, 27, 64 …….. c. 3, 12, 27, 48 ……… d. 100, 98, 96, 94 ….. e. 100, 98.5, 97, 96.5 ……. f. 1, 4, 9 ……….. g. 4, 4, 12, 20 …………. h. 5, 25, 125 ….. upto 5th i. 1, 5, 9, 13, 17, …………….. j. 7, 22, 11, 34, 17 ………. k. 9, 7, 5, ………………, 1 upto 5th term. 7. Write a programme to display the given output. a. 12345 b. 1 c. 5 d. 1 e. 4 1234 22 54 121 484 123 333 543 12321 49284 12 4444 5432 1234321 4937284 1 55555 54321 123454321 493817284 8. Write the output for the following programmes. a. CLS b. CLS c. CLS A =15 LET C = 1 A = 1 B = 20 WHILE C<=5 X =1 C = 30 PRINT C; PRINT X SWAP A, B C = C+1 X = X*10+1 PRINT A, B, C WEND A = A=1 END END END d. CLS e. CLS f. CLS FOR I = 5 TO 1 STEP - 1 I = 1 A =5 FOR J = 5 TO I STEP - 1 WHILE I<=10 FOR I = 1 TO 5 PRINT J; PRINT I^2; PRINT A; NEXT J I = I+1 A = A*10+4 PRINT WEND NEXT I NEXT I END END END


Approved by Curriculum Development Centre 221 Oasis Radiant Computer Science, Book 10 Chapter 9 Modular Programming ffl Modular programming ffl Main modules, procedure modules ffl Function/sub procedures ffl Parameters & arguments ffl Concept of global /local variable THIS CHAPTER COVERS :


Oasis Radiant Computer Science, Book 10 222 Approved by Curriculum Development Centre Modular Programming 9 Chapter Development of a programme is very lengthy and complex. A single programme may have so many pages and lines. So to remove such a complexity of the programme, we have to divide the whole programme into different blocks. These blocks act like bricks to a wall. Such building blocks or subprogrammes are called modules or procedures. Modular programming is the art of developing the programme in which the whole is divided into multiple independent units or blocks called procedures or sub-programmes to perform specific work. After decomposition of the single programme into multiple blocks it is essential to collect and use them to make a single programme. These units or procedures can be reused many times throughout the programme. Advantages of modular programming • It makes the programme development process easy and simple. • It saves time as the same module is used for multiple times because we can reuse the module when required. • It is very easy to debug the programme because we can deal with its individual procedures or blocks. • It reduces overlapping parts, also called redundancy, of the programme. • It is possible to develop library of modules applicable for other programmes also so it makes it simple and easy to develop programmes in a shorter time. • We can test each module independently so that it makes the testing process easy and simple. • It is possible to develop the programme using collaborative work between the programmers. It gives the concept of division of work because different programmers can develop different modules as per their knowledge and expertise. Types of Procedures Procedures are also called subroutines. A subroutine is a collection of statements belonging to a process, which requires to be repeated very frequently in the


Approved by Curriculum Development Centre 223 Oasis Radiant Computer Science, Book 10 programme. A subroutine consists of a set of programme statements that may be used repeatedly at different places throughout the main programme. There are two types of procedures: • SUB Procedures • Function Procedures SUB Procedures It is a block of statements arranged in a systematic order to perform some specific work. It usually performs the single task in a programme. We can use the procedures inside another procedure or main part of the programme to perform repeated tasks. We have to declare the SUB procedures using SUB …. ENDSUB statement. Some characteristics of Sub procedures: • It reduces the repeated work in the programme because we can reuse the procedure where applicable. • It is possible to use the recursive concept on SUB procedures. • It is very easy to debug the programme because it is arranged in a modular form. • It does not have any data type. • It does not return any value like the function. How to Declare Sub Procedures We have to declare the SUB Procedures using the following syntax. DECLARE SUB Name (Parameter List) Where, Name defines the name of SUB procedure. We can supply the name of SUB Procedure using maximum of 40 characters. Parameter List defines the type and number of arguments that will be used to call the procedures. For example we can declare a SUB procedure to calculate the area of rectangle as: DECLARE SUB (Length as Integer, Breadth as Integer)


Oasis Radiant Computer Science, Book 10 224 Approved by Curriculum Development Centre What are parameters? Parameters are the identifiers or names of the variables that will receive data or values and supply it to the procedure or function to perform some task. Basically there are two types of parameters. These are Formal parameters and Actual or real parameters. Formal parameters are variables that are used to declare the types of data to be passed to the procedures. Actual parameters are the arguments that are used to declare types of data to the procedure. Actual parameters may be variables or constant. What are variables? Variables can be defined as the identifiers or locations of storage where we can store and retrieve the data during the time of execution of programme. These are addressed by the identifier name. For example, name and address can be defined as the identifier that can store value during the execution of programme. Scope of variable The life of the variable on the programme is called the scope of the variable. According to the scope of variables we can use them on whole programme or within a single module of the programme. Basically, we have two types of variables: Local and Global. Local variable The variable which is declared inside a procedure, and is not accessible from outside the procedure is known as a local variable. The life of the variable is inside the single procedure only. Global variable A variable which is declared outside all procedures and is accessible from any procedure is known as a global variable. The life of the variable is on the whole programme and all the procedures used on the programme can access this variable. COMMON and DIM Statement A variable declared in a COMMON or DIM statement with the SHARED attribute is global variable to a module.


Approved by Curriculum Development Centre 225 Oasis Radiant Computer Science, Book 10 COMMON Statement Syntax: COMMON [SHARED] Variable List This statement declares the variable as global so that all modules can use this variable. DIM SHARED Statement Syntax: DIM SHARED Variable List [Subscript] It helps to share the variable to all programmes. It is declared on main programme and has scope on whole programme. Defining a SUB Procedure After declaring the procedure, we have to define it so that we can use it to perform some task. It is essential to define the procedure after DECLARE it. We can define the procedure by using following syntax: SUB Name (Parameter List) [Statements] ENDSUB Where, Name defines the name of subprogramme and statements are the logical and arithmetical work inside the SUB programme. Parameter list defines the variable to use on the SUB programmes. How to use SUB Procedure We can use and execute the SUB Procedures according to our requirement by using following statement. We can use it by using following syntax. CALL Name (Argument List) Name is the SUB Procedures name and Arguments List is the data and its type to pass on SUB Procedures for execution. Example DECLARE SUB SUM (N1, N2) CALL SUM (N1, N2) END SUB SUM (N1, N2)


Oasis Radiant Computer Science, Book 10 226 Approved by Curriculum Development Centre INPUT “Enter Two Numbers”, N1, N2 R=N1+N2 PRINT R END SUB In above program SUB Procedure name SUM performs the addition and displays the result when it was called as CALL statement. Function Procedures Another modular programme structure is called function module or procedure. This procedure returns a single value when it will be called on the programme. We can pass different values to the function procedure to generate multiple results by using multiple places on the programme. How to declare the function We can declare the function procedure by using following syntax. Syntax: DECLARE FUNCTION Name (Parameter List) Where, Name defines the user defined function name. Parameter List defines the number and type of arguments used to call the procedure. How to define a function After the declaration of a function, we have to define the function to call for execute. We can define the function using following syntax: FUNCTION Name (Parameter List) [Statements] END FUNCTION How to call FUNCTION Procedure We have to use the function name to call the function to use on the programme. We can use following syntax to call the function. VARIABLES= FUNCTION Name (Arguments List) Example DECLARE FUNCTION SUM (N1, N2)


Approved by Curriculum Development Centre 227 Oasis Radiant Computer Science, Book 10 INPUT “Enter any two numbers”, N1, N2 R=SUM (N1, N2) PRINT R END FUNCTION SUM (N1, N2) SUM=N1+N2 END FUNCTION Above programme function name SUM accepts two arguments or parameter or variables called N1 and N2 and displays the result after addition of these two numbers. Passing arguments to SUB procedures and the Function Procedure When a SUB procedure and functions are called to execute, it is essential to pass the data as arguments to the function and SUB procedure. We can pass the arguments to the function and procedure by using following method: • By value • By reference Passing arguments By Value It is the process to pass the actual contents or data instead of the arguments address. When arguments are passed by value, the SUB procedure or function identifies only a copy of the argument. It is suitable for the situation when the function or SUB procedures do not modify the value of original variable on calling program. This is done by enclosing the argument variables in parenthesis. Example Using SUB Procedure DECLARE SUB SUM (N1, N2) CALL SUM ((N1), (N2)) END SUB SUM (N1, N2) INPUT “Enter Two Numbers”, N1, N2 R=N1+N2 PRINT R END SUB


Oasis Radiant Computer Science, Book 10 228 Approved by Curriculum Development Centre Using FUNCTION Procedure DECLARE FUNCTION SUM (N1, N2) INPUT “Enter any two numbers”, N1, N2 R=SUM ((N1), (N2)) PRINT R END FUNCTION SUM (N1, N2) SUM=N1+N2 END FUNCTION Passing arguments by reference on function procedure and SUB procedure Another method to pass the arguments to the SUB procedure or function is called passing arguments by reference. All types of data stored on the memory have their own addresses. We can pass the arguments by given such addresses so that the SUB procedure or functions can carry the value from such addresses. When the SUB procedure or function procedure is called the address of the variable is passed to the procedure or function as arguments and these modules will carry the data from these addresses. Example Using SUB Procedure DECLARE SUB SUM (N1, N2) CALL SUM (N1, N2) END SUB SUM (N1, N2) INPUT “Enter Two Numbers”, N1, N2 R=N1+N2 PRINT R END SUB Using FUNCTION Procedure DECLARE FUNCTION SUM (N1, N2) INPUT “Enter any two numbers”, N1, N2 R=SUM (N1, N2) PRINT R END


Approved by Curriculum Development Centre 229 Oasis Radiant Computer Science, Book 10 FUNCTION SUM (N1, N2) SUM=N1+N2 END FUNCTION Recursive Function Functions are built to call by another procedure or function or main program to perform some work. But sometimes we have to call the function by itself. These functions are called recursive functions. Example DECLARE FUNCTION Factorial (N) INPUT “Enter the value”, N PRINT “Factorial Value”, Factorial (N) END FUNCTION Factorial (a) Factorial=1 IF a>1 THEN Factorial= a*Factorial (a-1) END FUNCTION Some Examples 1. Write a function named reverse(n, rev) to check whether input number is palindrome number or not. Answer DECLARE FUNCTION reverse(n, rev) INPUT “ Any number”; n X=reverse(n,rev) PRINT “Its reverse form is:”; rev END FUNCTION reverse(n,rev) WHILE n>0 R= n MOD 10 rev=rev*10+R


Oasis Radiant Computer Science, Book 10 230 Approved by Curriculum Development Centre n=INT(n/10) WEND END FUNCTION 2. Write a programme to print the following series using SUB procedure. 2, 22, 222, 2222, 22222 Answer DECLARE FUNCTION AAA( ) CALL AAA END SUB AAA A=2 FOR I = 1 TO 5 PRINT A A=A*10+2 NEXT I END SUB 3. To find sum of even numbers among 10 numbers. DECLARE FUNCTION SUM (N ( )) CLS DIM N (10) FOR J= 1 TO 10 INPUT “Enter any 10 number:” N (j) NEXT J PRINT “Sum of even numbers:” SUM (N( )) END FUNCTION SUM (N ( )) S=0 FOR K= 1 TO 10 IF N (k) MOD 2 = 0 THEN S=S+N (K) END IF NEXT K


Approved by Curriculum Development Centre 231 Oasis Radiant Computer Science, Book 10 SUM= S END FUNCTION 4. Write a programme to test whether input word is Palindrome word or not using FUNCTION END FUNCTION. DECLARE FUNCTION PAL$(A$) INPUT “ Any word:”; a$ X$=PAL$(A$) IF A$= x$ THEN PRINT “ It Is Palindrome Word” ELSE print “ It Is Not” END IF END Function pal$(A$) FOR I = LEN(A$) TO 1 STEP -1 B$=mid$(A$,I,1) C$=C$+B$ NEXT I PAL$=C$ END FUNCTION 5. Write a programme to find the HCF and LCM of any two numbers using SUB procedure. DECLARE SUB HCL (A, B) INPUT “Any two numbers:”; A,B CALL HCL (A,B) END SUB HCL (A,B)


Oasis Radiant Computer Science, Book 10 232 Approved by Curriculum Development Centre C=A: D=B AA: R= C MOD D IF R = 0 THEN PRINT “ Hcf:” ; D PRINT “Lcm:” (A*B)/D END END IF C=D D=R GOTO AA END SUB 6. REM To remove odd digits from a supplied number DECLARE FUNCTION remodd (n) CLS INPUT “any number “; a PRINT “Even digits only “; remodd(a) END ============================ FUNCTION remodd (n) p = 1 WHILE n <> 0 r = n MOD 10 IF r MOD 2 = 0 THEN s = s + r * p p = p * 10 END IF n = n \ 10 WEND remodd = s END FUNCTION


Approved by Curriculum Development Centre 233 Oasis Radiant Computer Science, Book 10 7. Write a programme to display the below output using a SUB procedure. 1 121 12321 1234321 123454321 Code for the above program: DECLARE SUB show () CLS CALL show END SUB show c# = 1 FOR i = 1 TO 5 PRINT c# ^ 2 c# = c# * 10 + 1 NEXT i END SUB 8. Write a programme to display the below output using a SUB procedure. KA AT TH HM MA AN ND DU DECLARE SUB show () CLS CALL show END SUB show a$ = “KATHMANDU” FOR i = 1 TO LEN(a$) - 1 PRINT TAB(i); MID$(a$, i, 2) NEXT i END SUB


Oasis Radiant Computer Science, Book 10 234 Approved by Curriculum Development Centre 9. To find the reverse of string DECLARE FUNCTION REV$ (ST$) CLS INPUT “Enter string”, S$ LET R$= REV$(S$) PRINT “The reverse string is :”; R$ END FUNCTION REV$(ST$) FOR I = LEN(ST$) TO 1 STEP - 1 RV$= RV$+ MID$(ST$, I, 1) NEXT I REV$=RV$ END FUNCTION 10. Write a programme using a function procedure to find the greatest number among any 10 numbers supplied by the user. DECLARE FUNCTION GreatestNum (N( ) ) CLS PRINT “Enter any 10 numbers” FOR I= 1 TO 10 INPUT N(I) NEXT I GT= Greatest Num (N( ) ) PRINT “The greatest number is::”; GT END ======================================= FUNCTION GreatestNum (N( ) ) H = N (1) FOR I = 2 TO 10 IF N(I)> H THEN H = N(I) NEXT I GreatestNum = H END FUNCTION


Approved by Curriculum Development Centre 235 Oasis Radiant Computer Science, Book 10 11. To check Armstrong or not DECLARE FUNCTION check (n) CLS INPUT “Type a number “; n IF check(n) = 1 THEN PRINT “Armstrong” ELSE PRINT “Not Armstrong” END IF END FUNCTION check (n) a = n WHILE n = 0 r = n MOD 10 s = s + r ^ 3 n = n / 10 WEND IF a = s THEN check = 1 ELSE check = 0 END IF END FUNCTION 12. Write a programme using a sub procedure to generate the following series. 1, 2, 3, 5, 8……………. up to 10th term. DECLARE SUB SERIES ( ) CLS CALL SERIES END =================================== SUB SERIES A=1:B=2 FOR I =1 TO 10


Oasis Radiant Computer Science, Book 10 236 Approved by Curriculum Development Centre C= A+B PRINT A; A=B B=C NEXT I END SUB 13. Write a programme using a function procedure to convert decimal number to octal number without using OCT$() function. DECLARE FUNCTION DecOct$ ( D ) CLS INPUT “ENTER ANY DECIMAL NUMBER::”;D DO$= DecOct$(D) PRINT “THE OCTAL NUMBER IS::”;DO$ END FUNCTION DecOct$ ( D ) P=0 WHILE D<>0 R=D MOD 8 OcNum$ = R + OcNum$ D=D\8 WNED DecOct$=OcNum$ END SUB 14. To count the number of characters of a string DECLARE FUNCTION char (a$) CLS INPUT “Enter any string”;a$ ch = char(a$) PRINT “Total number of Charcter is”; ch END =============================== FUNCTION char (a$)


Approved by Curriculum Development Centre 237 Oasis Radiant Computer Science, Book 10 FOR X = 1 TO LEN(a$) C = C+ 1 NEXT X Char = C END FUNCTION 15. Write a programme to check whether the supplied number is divisible by 3 or not using a function procedure. DECLARE FUNCTION div$ (n) CLS INPUT “Enter any number”; n di$ = div$(n) PRINT n; di$ END FUNCTION div$ (n) r = n MOD 3 r1 = n MOD 5 IF r = o AND r1 = 0 THEN d$ = “Divisible by 3 and 5” ELSE d$ = “Not Divisible by 3 and 5” END IF div$ = d$ END FUNCTION 16. Write a sub programme to check whether the supplied number is perfect square or not using a sub procedure. DECLARE SUB Square (n) CLS INPUT “Enter any number”; n CALL Square(n) END


Oasis Radiant Computer Science, Book 10 238 Approved by Curriculum Development Centre SUB Square (n) CLS r = SQR(n) IF r = INT(r) THEN PRINT n; “Is a Perfect Square” ELSE PRINT n; “Is not a perfect square” END IF END SUB 17. Write a programme to check whether the number is odd or even using a function procedure. DECLARE FUNCTION no$ (n) CLS INPUT “Enter any number”; n r$ = no$(n) PRINT r$ END FUNCTION no$ (n) r = n MOD 2 IF r = 0 THEN e$ = “Even” ELSE e$ = “Odd” END IF no$ = e$ END FUNCTION 18. Write a sub-programme to input any integer value from the keyboard and display the sum of all entered digits. For eg. if the entered numbers are 123 then the sum of digits should be 6. DECLARE SUB res (n)


Approved by Curriculum Development Centre 239 Oasis Radiant Computer Science, Book 10 CLS INPUT “Enter the digits:”; n CALL res(n) END SUB res (n) WHILE n >= 1 r = n MOD 10 s = s + r n = (n - r) / 10 WEND PRINT “The sum is:”; s END SUB 19. Write a Program (WAP), a basic programme, to check entered letter is capital or small (uppercase or lowercase) using declare function procedure. DECLARE FUNCTION UC$ (A$) CLS INPUT “Enter a letter”; A$ PRINT UC$(A$) END FUNCTION UC$ (A$) CH$ = UCASE$(A$) IF A$ = CH$ THEN UC$ = “It is capital letter” ELSE UC$ = “It is small letter” END IF END FUNCTION


Oasis Radiant Computer Science, Book 10 240 Approved by Curriculum Development Centre 20. Write a Program (WAP), a basic programme, to check entered letter is capital or small (uppercase or lowercase) using declare SUB procedure. DECLARE SUB UC(A$) CLS INPUT “Enter a letter”; A$ CALL UC(A$) END SUB UC(A$) CH$ = UCASE$(A$) IF A$ = CH$ THEN PRINT “It is capital letter” ELSE PRINT “It is small letter” END IF END SUB Fibonacci series is the series in which the next number is obtained by the sum of two numbers just front of it. The first two numbers are explained by the user. It can be obtained up to any term. You can change the number of output to any term just by changing the looping number. Here is example of a Fibonacci series. suppose you entered the first two numbers-1 & 2 and up to the tenth term then your output will be as: 1,2,3,5,8,13,21,34,55,89 Here in the beginning 1 & 2 are the entered numbers. 3 is the product of 1 & 2 as the definition of Fibonacci series given in first. Likewise 5 is the sum of 2 & 3 and 8 is the sum of 3 & 5 and so on. 21. Write a programme in QBASIC to print the Fibonacci series up to tenth term. Using loops-FOR...NEXT & WHILE...WEND. Using declare sub procedure Using FOR...NEXT DECLARE SUB FIB () CLS CALL FIB END


Approved by Curriculum Development Centre 241 Oasis Radiant Computer Science, Book 10 SUB FIB A = 1 B = 2 PRINT A PRINT B FOR I = 1 TO 10 C = A + B PRINT C A = B B = C NEXT I END SUB Using WHILE...WEND DECLARE SUB FIB () CLS CALL FIB END SUB FIB I = 1 A = 1 B = 2 PRINT A PRINT B WHILE I < = 10 C = A + B PRINT C A = B B = C I = I + 1 WEND END SUB


Oasis Radiant Computer Science, Book 10 242 Approved by Curriculum Development Centre 22. Differentiate between FUNCTION and SUB SUB FUNCTION 1. SUB cannot return any value. 1. FUNCTION returns one and only one value. 2. CALL statement can invoke subs in main program. 2. CALL cannot be used to invoke functions. 3. “()” is required around the parameter list if CALL statement is used else “()” is not required. 3. “()” must follow the function call. Points to remember • Breaking a large programme into small procedures or modules is modular programming. • The module which includes and controls the sub-modules is called main module. Whereas a sub-module is an individual and independent page (programme) declared in the main module. • DECLARE statement is used to include the sub module. • CALL statement with the sub procedure name is used for calling the sub module. • Local variable is used only within a sub module whereas Global variable can be used both inside and outside the module. • Passing arguments by reference means passing the address of a variable. • Passing arguments by value means passing the copy of the variable which is destroyed when the procedure is over.


Approved by Curriculum Development Centre 243 Oasis Radiant Computer Science, Book 10 Exercises 1. Answer the following question: a. What is modular programming? b. Write the advantages of modular programming? c. What is main module? d. What is sub module? e. What are the features of function procedure? f. Differentiate between sub procedure and function procedure. g. What is library function? h. Define argument passed by reference with suitable example. i. Define argument passed by value with suitable example. j. Differentiate between formal and real (actual) parameters. k. Define local and global variables. l. Give the function of SHARED and COMMON SHARED statements. m. Define STATIC variable. n. Differentiate between Arguments and Parameters. o. Differentiate between Argument Pass by Value and Argument Pass by Reference. 2. Write the programme codes. a) To find area of a rectangle using SUB procedure. b) To find the perimeter of a rectangle having 5cm length and 2cm breadth, using SUB…END SUB. c) Using SUB…END SUB, find the area of a square. d) Using SUB procedure, find the perimeter of a square. e) Find the perimeter of triangle, if measures of three sides are given by using SUB…END SUB. f) Find the simple interest, if principle, rate and time are given, by using a sub procedure. g) Using a sub procedure, input a number from the keyboard and check whether it is Armstrong number or not. [A number whose sum of cube digits is the number itself is Armstrong number, like 13 +53 +33 =153] h) Using SUB…END SUB, input a number and find out whether it is Palindrome or not.[A number which reads the same from both sides is


Oasis Radiant Computer Science, Book 10 244 Approved by Curriculum Development Centre Palindrome, like 12321,121, etc.] i) Using sub procedure, input a number and check whether that number is a perfect square of not. j) Using sub procedure, input a number and print the product of digits of that number. [if the number is 345 then result is 3*4*5=60] k) Find the reverse of given number using SUB procedure. l) Using sub procedure, check whether the given number is prime number or composite number. m) Using SUB…END SUB, print the given series. i. 1, 1, 2, 3, 5, 8, 13, ………10th term. ii. 1, 2, 3, 6, 11, 20, ……….10th term. iii. 7, 22, 11, 34, 17, ……….10th term. iv. 2, 8, 18, 32, 50, ………...10th term. v. 3, 12, 27,48, 75, ………..10th term. n) Input 10 different numbers and arrange them in ascending order using FUNCTION…END FUNCTION o) Using Function procedure, input 10 numbers and arrange them in descending order. p) For the following patterns by using SUB… END SUB i) S ii) A ii) A SU NA HAA SUH ANA UHAAN SUHA HANA SUHAANA SUHAN UHANA SUHANA SUHANA iv) 1 v) 54321 vi) 5 12 4321 44 123 321 33 1234 21 2222 12345 1 11111 3. Write codes. a) Using Function procedure, input length and breadth and calculate total surface area of a box. [tsa=2h (lb + bh +hl)] b) Input temperature in Celsius and convert it into Fahrenheit using


Approved by Curriculum Development Centre 245 Oasis Radiant Computer Science, Book 10 FUNCTION…END FUNCTION. [F=9c/5+32] c) Input initial velocity, time taken, and acceleration and calculate the distance travelled by the body. [S=UT+1/2AT2 ] d) Using FUNCTION…END FUNCTION, input three sides of a triangle and print its perimeter. [p=a + b + c] e) Check whether the word input by the user is palindrome or not using Function procedure. f) Using FUNCTION…END FUNCTION, enter a string and count number of vowels in that string. g) Print the string in alternate capital letters using Function procedure. [if the string is Nepal result will be Nepal] h) Test whether an input number is palindrome number or not using FUNCTION…END FUNCTION. i) For a programme that asks length and breadth of a room and calculates its perimeter using a Function procedure. [2h(l+b)] j) Count the repetition of the character in input string using FUNCTION… END FUNCTION. [ Like if the string is MISSISSIPPI and the character is I the result is 4] k) Using FUNCTION…END FUNCTION, enter a string and count numbers of consonants in that string. l) Using FUNCTION…END FUNCTION, enter a string and count numbers of word in that string. m) Using FUNCTION…END FUNCTION, enter the salaries of 200 employees and count the number of employees getting salaries more than 40000 and less than 50000 rupees. 4. Rewrite the programme after correcting the bugs a) Rem to display the Fibonacci series DECLARE SUB SERIES () CLS EXECUTE SERIES END SUB SERIES A=2 B=2 FOR CTR = 1 TO 5


Oasis Radiant Computer Science, Book 10 246 Approved by Curriculum Development Centre DISPLAY A; B; A=A+B B= A+B LOOP CTR END SERIES () b) Rem to display the sum of two numbers DECLAE FUNCTUION SUM (A , B) INPUT “Enter first number”; X INPUT “Enter second number”; Y DISPLAY SUM (A , B) END FUNCTION SUM (X,Y) SUM = A+B END c) Rem to print multiples of N number CLS DECLARE MUL (N) INPUT “Enter no.”; A CALL MUL () END SUB MUL (N) I=1 WHILE I>=10 PRINT N*I I=I+1 LOOP SUB END d) Rem program to find the square root of given number DECLARE SUB SQUARE (N) ENTER N DISPLAY SQUARE (N) END SUB SQUARE (N) S= M^0.5 PRINT S FINISH


Approved by Curriculum Development Centre 247 Oasis Radiant Computer Science, Book 10 e) Rem find sum of all numbers from 1 to n DECLARE SUB SUM (N) INPUT “ENTER any number”; A CALL SUM (N) END SUB SUM (N) S=0 FOR I=1 TO A S=S+I NEXT I PRINT “SUM=”; A END f) DECLARE SUB CURMTH(D$) Rem program to print current month from the system data. DT=DATE$ DISPLAY CURMTH(D$) END SUB CURMIT(D$) N= VAL(LEFT$(D$,2)) FOR M= 1 TO TWELVE READ MIN$ IF M=N THEN PRINT “CURRENT MONTH IS”; MTH$ NEXT M DATA JAN, FEB, MAR, APRL, MAY, JUNE, JULY, AUG, SEPT, OCT, NOV, DEC END SUB g) Rem to display the reverse of the number. DECLARE FUNCTION Rev (N) CLS INPUT “Any number”; a PRINT “Reverse=; Rev (N) END FUNCTION Rev (N) WHILE N=0 R= N MOD 10 S= S+10*R N=N/10


Oasis Radiant Computer Science, Book 10 248 Approved by Curriculum Development Centre WEND A=Rev END FUNCTION. h) Rem to display reverse of string DECLARE FUNCTION Rev$ (ST$) CLS INPUT “Enter a string”; S$ LET R$=Rev$(ST$) PRINT “The Reverse string is”; Rev$ END FUNCTION Rev$(ST$) FOR I= LEN(ST$) RV$=RV$+ MID$(ST$, I, 1) NEXT I END FUNCTION 5. Write output of the given programs a) DECLARE FUNCTION RESULT$(N$) CLS PRINT “Output is”, RESULT$ (“NEPALGUNJ”) END FUNCTION RESULT$(N$) FOR I=1 TO LEN(N$) C$=MID$(N$, I, 1) IF I MOD 2 =0 THEN C$= LCASES$(C$) ELSE C$=UCASES$(C$) END IF W$=W$+C$ NEXT I RESULT$=W$ END FUNCTION b) DECLARE SUB RESULT () CLS CALL RESULT


Approved by Curriculum Development Centre 249 Oasis Radiant Computer Science, Book 10 END SUB RESULT A=11111 C=5 WHILE C>=1 PRINT A; A=A\10 C=C-1 WEND END SUB c. DECLARE FUNCTIO AREA (A, B) LET A=30 LET B=40 LET D=AREA (A, B) PRINT D FUNCTION AREA (A, B) PRINT A,B AREA=A * B END FUNCTION d) DECLARE SUB RES(ST$) CLS ST$= “BOOKREADKEYED” DATA 4,6,11,1,2,7,5,13 CALL RES (ST$) END SUB RES (ST$) FOR SK = 1 TO 8 READ N PRINT MID$(ST$, N, 1); NEXT SK END SUB e) DECLARE SUB OUTPOOT () CLS CALL OUTPOOT END


Oasis Radiant Computer Science, Book 10 250 Approved by Curriculum Development Centre SUB OUTPOOT X$ = “POKHARA” FOR I = 1 TO LEN(X$) STEP 2 PRINT MID$(X$, N, 1); NEXT I END SUB f) DECLARE FUNCTION TAKE (A, B) DECLARE SUB PROCESS (A) CLS FOR I=1 TO 5 STEP 1 READ C, D A= TAKE (C, D) CALL PROCESS (A) NEXT I DATA 78,57,34,53,22,34,76,56,44,31 END SUB PROCESS (X) IF X MOD 3=0 THEN PRINT X*2+1; ELSE PRINT X/2+1; END IF END SUB FUNCTION TAKE (A,B) IF A<B THEN SWAP A,B TAKE =A MOD B END FUNCTION 6. Read the given programme and answer the questions that follow: a) DECLARE SUB RESULT () CLS DATA 4,1,7,9,5 END SUB RESULT S$= “DUSTGARD” FOR I = 1 TO 5


Click to View FlipBook Version