Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Post Top Ad

Your Ad Spot

Tuesday, February 8, 2022

Sequence program using FUNCTION..END FUNCTION

Sequence Program Using Function..End Function


Sequence Program Using Function..End Function


Introduction:

If you're looking to create a program that executes a series of instructions in a particular order, a sequence program may be just what you need. A sequence program using function ..end function can help you simplify your code, reduce errors, and make your code more organized. One way to create a sequence program in many programming languages is by using a function..end function structure. In this article, we will explore how to create a sequence program using function..end function and provide a step-by-step guide to get you started.


What is a sequence program?


A sequence program is a set of instructions that are executed in a specific order. It's a simple yet powerful way to structure your code so that it's easy to read, modify, and maintain. In a sequence program, you define a series of steps that must be performed in order to accomplish a task.

Why use a FUNCTION..END FUNCTION structure?


When creating a sequence program, using a function..end function structure has several advantages. For one, it allows you to define a set of instructions that can be called from anywhere in your code. This makes it easy to reuse code and avoid repetition. Additionally, a function can take parameters, which allows you to pass data to the function and customize its behavior.
How to create a sequence program using function..end function

To create a sequence program using function..end function, you will need to follow these steps:

Defining the function


First, you need to define the function. This involves giving the function a name and specifying any parameters that it should take. Here is an example:



In the above example, we have defined a function called "functionname" that takes two parameters, "parameter1" and "parameter2". The parameters are optional, but if you want to pass data to the function, you will need to define them.
Writing the code

Once you have defined the function, you can write the code that will be executed when the function is called. Here is an example:



In the above example, we have defined a sequence program with three steps. When the function is called, these steps will be executed in order. You can customize the steps by adding your own code. For example, you might want to perform calculations, access data, or call other functions.

Some solved examples of Sequence program using FUNCTION..END FUNCTION


1) WAP to input any 2 numbers and display their sum.
DECLARE FUNCTION add(a,b)
CLS
INPUT "Enter any two numbers:-";a,b
t=add(a,b)
PRINT "Sum=";t
END
FUNCTION add(a,b)
s=(a+b)
add=s
END FUNCTION

2) WAP to calculate the volume of a cylinder. [Hints: volume=πR2H] SLC specification Grid 2065
DECLARE FUNCTION vol(r,h)
CLS
INPUT "Enter Radius and height=";r,h
PRINT "Volume of cylinder=";vol(r,h)
END
FUNCTION vol(r,h)
vol=22/7*r*h
END FUNCTION

3) WAP to calculate and print the simple interest.
DECLARE FUNCTION si(p,t,r)
CLS
INPUT "Enter Principal amount=";p
INPUT "Enter time=";t
INPUT "Rate of interest=";r
PRINT "Simple interest=";si(p,t,r)
END
FUNCTION si(p,t,r)
si=(p*t*r)/100
END FUNCTION

4) Write a program in QBASIC to find the area of four walls of a room (Hints=A=2h(l+b)) (SLC 2064)
DECLARE FUNCTION area (l, b, h)
CLS
INPUT "Enter length="; l
INPUT "enter breadth="; b
INPUT "Enter height="; h
PRINT "The area of four walls="; area(l, b, h)
END
FUNCTION area (l, b, h)
a = 2 * h * (l + b)
area = a
END FUNCTION

5) WAP to convert Nepali currency into Indian currency.
DECLARE FUNCTION currency (n)
CLS
INPUT "Enter currency in Nepali"; n
s = currency(n)
PRINT "Equivalent Indian currency"; s
END

FUNCTION currency (n)
currency = n / 1.6
END FUNCTION


6) WAP to calculate distance traveled by a body. [Hints: s=ut+1/2at2]. Where u is initial velocity t is time, s is distance and a is acceleration. (SLC 2065)
DECLARE FUNCTION distance(u,t,a)
INPUT "enter initial velocity=";u
INPUT "enter time=";t
INPUT "enter acceleration";a
PRINT distance(u,t,a)
END
FUNCTION distance(u,t,a)
distance= u*t+1/2*a*t^2
END FUNCTION

7) WAP using Function module to calculate and print the volume of a box. (SLC 2066)
DECLARE FUNCTION vol (l, b, h)
INPUT "enter length, breadth, height"; l, b, h
PRINT "The volume of the box="; vol(l, b, h)
END
FUNCTION vol (l, b, h)
v = l * b * h
vol = v
END FUNCTION

8. WAP to calculate the average of three numbers. (SLC 2067 , SEE 2075)
DECLARE FUNCTION avg(a,b,c)
CLS
INPUT "Enter any three numbers=";a,b,c
PRINT avg(a,b,c)
END
FUNCTION avg(a,b,c)
avg=(a+b+c)/3
END FUNCTION

9. WAP to input string in lowercase and display in uppercase.
DECLARE FUNCTION upper$(a$)
CLS
INPUT "Enter any string in lowercase:";a$
upp$=upper$(a$)
PRINT "String in uppercase:";upp$
END
FUNCTION upper$(a$)
upper$=UCASE$(a$)
END FUNCTION

10. WAP to input distance in kilo meter and convert it into meter.
DECLARE FUNCTION convert(k)
CLS
INPUT "Distance in kilo meter:-";k
m=convert(k)
PRINT "Distance in meter:-";m
END
FUNCTION convert(k)
me=k*1000
convert=me
END FUNCTION

11. WAP to input temperature in centigrade and convert it into Fahrenheit. [Hints: F=9/5*c+32]
DECLARE FUNCTION temp(c)
CLS
INPUT "Enter temperature in Celsius"; c
t=temp(c)
PRINT "Temperature in Fahrenheit:-";t
END
FUNCTION temp(c)
f=(9/5)*c+32
temp=f
END FUNCTION

12. WAP to input length and breadth, calculate area and display it.
DECLARE FUNCTION area(l,b)
INPUT "Enter Length and breadth:-";l,b
a=area(l,b)
PRINT "Area=";a
END
FUNCTION area(l,b)
ar=l*b
area=ar
END FUNCTION

13. WAP to print the area of triangle. [Hits: area=1/2×b×h]
DECLARE FUNCTION area(b,h)
CLS
INPUT "Enter base and height of a triangle=";b,h
PRINT "Area of triangle=";area(b,h)
END
FUNCTION area(b,h)
Area=1/2*b*h
END FUNCTION

14. WAP to calculate and print the total surface area of a box. [Hints: a=2(lb+bh+hl)
DECLARE FUNCTION area(l,b,h)
CLS
INPUT "Enter length, breadth and height=";l,b,h
PRINT "Surface area of a box=";area(l,b,h)
END
FUNCTION area(l,b,h)
area=2*(l*b+b*h+h*l)
END FUNCTION

15. Write a program find area of the triangle.
DECLARE FUNCTION AR(B,H)
CLS
INPUT “ENTER BASE”;B
INPUT “ENTER HEIGHT”;H
PRINT “AREA ”;AR(B,H)
END
FUNCTION AR(B,H)
AR=0.5*(B*H)
END FUNCTION

16. WAP to calculate and print the volume of a cylinder.
DECLARE FUNCTION VOL (R, H)
CLS
INPUT “ENTER RADIUS”; R
INPUT “ENTER HEIGHT”; H
PRINT “VOLUME: ”; VOL(R, H)
END
FUNCTION VOL(R, H)
VO = 3.14 * R ^ 2 * H
VOL = VO
END FUNCTION






Conclusion

A sequence program using function..end function can help you create a clear, organized, and reusable set of instructions in your code. By following the steps outlined in this article and using best practices for creating a sequence program, you can take your programming skills to the next level.





FAQs



1. What programming languages support the function..end function structure?
Ans: Many programming languages, including Visual Basic, MATLAB, and FreeBASIC, support the function..end function structure. The syntax may vary slightly depending on the language, but the basic idea is the same.


2. Can I call a function within a function?
Ans: Yes, you can call a function within a function. This is known as function nesting. However, it's important to keep your code organized and avoid excessive nesting, as it can make your code more difficult to read and debug.


3. How do I pass parameters to a function?
Ans: To pass parameters to a function, you need to define them in the function definition and then provide values for them when you call the function. For example:
function myFunction(parameter1, parameter2)
REM Do something with the parameters
end function
' Call the function with values for the parameters myFunction(value1, value2)


4. What are some common uses for sequence programs?
Ans: Sequence programs are used in a wide range of applications, from simple mathematical calculations to complex simulations and data processing tasks. Some common uses include data analysis, image processing, signal processing, and control systems.


5. How can I optimize my sequence program for performance?
Ans: To optimize your sequence program for performance, you can use techniques like loop unrolling, function inlining, and caching. However, it's important to balance performance with readability and maintainability, as overly complex optimizations can make your code more difficult to understand and debug.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages