0% found this document useful (0 votes)
3 views6 pages

Subroutines (1)

Subroutines are self-contained code segments that can be called from anywhere in a program, designed to reduce code duplication. There are two types of subroutines: Procedures, which do not return a value, and Functions, which do return a value. Additionally, variables can be classified as local, limited to the subroutine they are declared in, or global, accessible throughout the entire program.

Uploaded by

zayanmahmood2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Subroutines (1)

Subroutines are self-contained code segments that can be called from anywhere in a program, designed to reduce code duplication. There are two types of subroutines: Procedures, which do not return a value, and Functions, which do return a value. Additionally, variables can be classified as local, limited to the subroutine they are declared in, or global, accessible throughout the entire program.

Uploaded by

zayanmahmood2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Subroutines

Chapter 8
Subroutines

• A subroutine is a self-contained piece of code that has an identifier(name) and using that identifier(name) it can be called from

anywhere in the main program.

• When a problem is decomposed into sub-system, each of the sub-system, can be written as a subroutine.

• Subroutines are useful because they reduce code.

• There are two types of subroutine: Procedure and Function

• Procedure and function can both receive one or more values as parameters.

• These are values that are sent to the subroutine from the calling part of the program.
Key Words Key Words Key Words Key Words
Subroutine: Procedure: Function: Parameter:

a self-contained a subroutine a subroutine a value that is


piece of code that does not that does return sent to a
that has an return a value a value to the subroutine.
identifier and to the program program.
can be called that called it.
from elsewhere
in a program.
Procedure

• Procedure: A procedure runs the code inside it and does not return a value to the program that called it. It can be
called from other parts of the program using their name.

• Consider an example of a system that stores the height and


• The pseudocode definition of a procedure is: width of two rectangles and calculates their area. The system
also outputs the Area with a suitable message.

PROCEDURE calculate_area(length:
PROCEDURE Identifier() // procedure INTEGER, width: INTEGER)
header area ← length * width
OUTPUT "The area is “, area
codes to run inside the ENDPROCEDURE
function // procedure body
# Calling the procedure in program:
ENDPROCEDURE calculate_area(5, 3)
calculate_area(6, 3)
# Calling the procedure in program

Pseudocode for Procedure


Function

• Function: A function returns a value to the program that called it. This can be done by using the RETURN keyword. Once
a value is returned, the function stops running.

• Functions can be called from other parts of the program using their name, and their return value can be stored in a
variable.
• Consider an example of a system that stores the height and
width of two rectangles and calculates their area. The system
• The pseudocode definition of a function is:
also outputs the area with a suitable message.

FUNCTION Identifier() // function FUNCTION calculate_area (length: INTEGER,


header width: INTEGER) RETURNS INTEGER
codes to run inside the area ← length * width
function // function body RETURN area
RETURN value ENDFUNCTION
ENDFUNCTION
// Calling function in the main program
# Calling function in the main PRINT(calculate_area(5, 3))
program PRINT(calculate_area(6, 3))

Pseudocode for Function


Local and Global variable

Local variable Global variable

• A local variable can only be  A global variable can be used by


used by the part of the program any part of a program – its scope
it has been covers the whole program.
declared in – its scope is  In most language, it is declared at
restricted to that part of the the top of the program.
program.
• It is declared inside the
subroutine where it is needed.

• Scope of a variable is the areas within a program where the variable can be accessed.
Example

Output

Main program

Procedure

You might also like