When and Where to Use Functions and Procedures
Both functions and procedures help break down a program into modular units,
making it easier to develop, test, and maintain.
Aspect Function Procedure
Returns Value ✅ Yes (returns a value) ❌ No (performs actions only)
Use Case When result is needed When task/action is needed
Calculations (like sum, Displaying data, printing
Example Use
factorial) reports
Use Inside
✅ Yes ❌ No
Expressions
📌 Examples (in pseudocode)
✅ Function Example – Calculate square of a number:
FUNCTION Square(Number)
RETURN Number * Number
END FUNCTION
Main Program:
Result ← Square(5)
OUTPUT Result // Output: 25
🔹 Use a function when you need a result you can use elsewhere (like assigning it to a variable or
passing it into another function).
✅ Procedure Example – Print welcome message:
PROCEDURE PrintWelcome(Name)
OUTPUT "Welcome, " & Name & "!"
END PROCEDURE
Main Program:
PrintWelcome("Muhammad")
🔹 Use a procedure when you're performing an action, such as displaying something or writing to a file,
and don't need a return value.
Where in Programming?
Use functions when:
o You want to calculate and reuse results (e.g. GetMaxValue, CalculateTax)
oYou're writing algorithms where the result is used multiple times
Use procedures when:
o You want to modularize a sequence of steps
o You're managing input/output, file operations, or encryption routines
(which you've been exploring!)
Function Example – Encrypt a character using Caesar Cipher
FUNCTION CaesarEncrypt(Char, Shift)
EncryptedChar ← CHR(ASC(Char) + Shift)
RETURN EncryptedChar
END FUNCTION
Main Program:
C ← CaesarEncrypt("A", 3)
OUTPUT C // Output: "D"
Use case: This function returns the encrypted form of a single character using ASCII shifting—perfect
for modular encryption routines.
Procedure Example – Display array elements
PROCEDURE ShowArray(Arr, Size)
FOR i ← 1 TO Size
OUTPUT Arr[i]
NEXT i
END PROCEDURE
Main Program:
Declare MyArray[5] ← [10, 20, 30, 40, 50]
ShowArray(MyArray, 5)
Use case: A procedure is perfect here because you're only performing an action (displaying values), no
return is needed.
Function Example – Count vowels in a string
FUNCTION CountVowels(Text)
Count ← 0
FOR i ← 1 TO LEN(Text)
Ch ← UCASE(MID(Text, i, 1))
IF Ch = "A" OR Ch = "E" OR Ch = "I" OR Ch = "O" OR Ch = "U"
THEN
Count ← Count + 1
END IF
NEXT i
RETURN Count
END FUNCTION
Main Program:
Result ← CountVowels("Muhammad")
OUTPUT Result // Output: 3
This is handy when parsing user data or messages—say, as part of a compression or analytics module.
Procedure Example – Log message to a file
PROCEDURE LogMessage(Message)
OPEN "log.txt" FOR APPEND AS #1
WRITE #1, Message
CLOSE #1
END PROCEDURE
Main Program:
LogMessage("Encryption completed successfully.")
You're performing file output, which doesn’t need to return a result—ideal for
logging, auditing, or saving user actions.
Great start, Muhammad! Let’s break it down.
In programming, ByVal (by value) and ByRef (by reference) are two ways to pass
parameters into procedures or functions:
ByVal (By Value)
You pass a copy of the variable. Any changes made inside the procedure don’t
affect the original value.
🧪 Example:
PROCEDURE IncreaseByVal(ByVal Num)
Num ← Num + 5
OUTPUT "Inside procedure (ByVal): ", Num
END PROCEDURE
Main Program:
A ← 10
IncreaseByVal(A)
OUTPUT "Outside procedure: ", A // Output: 10
A remains unchanged because only a copy was modified.
ByRef (By Reference)
You pass the actual variable, not just a copy. Changes do affect the original
variable.
🧪 Example:
PROCEDURE IncreaseByRef(ByRef Num)
Num ← Num + 5
OUTPUT "Inside procedure (ByRef): ", Num
END PROCEDURE
Main Program:
A ← 10
IncreaseByRef(A)
OUTPUT "Outside procedure: ", A // Output: 15
This time, A is updated, because we passed it by reference.
Summary:
Parameter Affects
Passes Ideal For
Type Original?
A copy of
ByVal ❌ No Safe calculations, formulas
data
ByRef Actual ✅ Yes Modifying arrays, updating
Parameter Affects
Passes Ideal For
Type Original?
variable values
Want to try applying this to one of your encryption or array-based routines? I can
walk you through how to structure your parameters and why one method might be
better than the other depending on your goal. Ready to dive deeper?
Let’s break it down.
In programming, ByVal (by value) and ByRef (by reference) are two ways to pass
parameters into procedures or functions:
ByVal (By Value)
You pass a copy of the variable. Any changes made inside the procedure don’t
affect the original value.
Example:
PROCEDURE IncreaseByVal(ByVal Num)
Num ← Num + 5
OUTPUT "Inside procedure (ByVal): ", Num
END PROCEDURE
Main Program:
A ← 10
IncreaseByVal(A)
OUTPUT "Outside procedure: ", A // Output: 10
A remains unchanged because only a copy was modified.
ByRef (By Reference)
You pass the actual variable, not just a copy. Changes do affect the original
variable.
Example:
PROCEDURE IncreaseByRef(ByRef Num)
Num ← Num + 5
OUTPUT "Inside procedure (ByRef): ", Num
END PROCEDURE
Main Program:
A ← 10
IncreaseByRef(A)
OUTPUT "Outside procedure: ", A // Output: 15
This time, A is updated, because we passed it by reference.
Summary:
Parameter Affects
Passes Ideal For
Type Original?
ByVal A copy of ❌ No Safe calculations, formulas
Parameter Affects
Passes Ideal For
Type Original?
data
Actual Modifying arrays, updating
ByRef ✅ Yes
variable values
Want to try applying this to one of your encryption or array-based routines? I can
walk you through how to structure your parameters and why one method might be
better than the other depending on your goal. Ready to dive deeper?
Let’s look at a few more examples to solidify the difference between functions
(which return values) and procedures (which perform actions without returning a
value). I’ll tailor them to align with your programming interests in encryption,
arrays, and algorithmic logic.
Function Example – Encrypt a character using Caesar Cipher
FUNCTION CaesarEncrypt(Char, Shift)
EncryptedChar ← CHR(ASC(Char) + Shift)
RETURN EncryptedChar
END FUNCTION
Main Program:
C ← CaesarEncrypt("A", 3)
OUTPUT C // Output: "D"
Use case: This function returns the encrypted form of a single character using
ASCII shifting—perfect for modular encryption routines.
Procedure Example – Display array elements
PROCEDURE ShowArray(Arr, Size)
FOR i ← 1 TO Size
OUTPUT Arr[i]
NEXT i
END PROCEDURE
Main Program:
Declare MyArray[5] ← [10, 20, 30, 40, 50]
ShowArray(MyArray, 5)
Use case: A procedure is perfect here because you're only performing an action
(displaying values), no return is needed.
Function Example – Count vowels in a string
FUNCTION CountVowels(Text)
Count ← 0
FOR i ← 1 TO LEN(Text)
Ch ← UCASE(MID(Text, i, 1))
IF Ch = "A" OR Ch = "E" OR Ch = "I" OR Ch = "O" OR Ch = "U"
THEN
Count ← Count + 1
END IF
NEXT i
RETURN Count
END FUNCTION
Main Program:
Result ← CountVowels("Muhammad")
OUTPUT Result // Output: 3
This is handy when parsing user data or messages—say, as part of a compression
or analytics module.
Procedure Example – Log message to a file
PROCEDURE LogMessage(Message)
OPEN "log.txt" FOR APPEND AS #1
WRITE #1, Message
CLOSE #1
END PROCEDURE
Main Program:
LogMessage("Encryption completed successfully.")
You're performing file output, which doesn’t need to return a result—ideal for
logging, auditing, or saving user actions.