Procedures and functions belong to the category of subroutines. They consist of a sequence of instructions designed to execute a particular task or series of tasks.
Functions and procedures differ in their return behavior:
In summary, functions return values, while procedures do not.
Once a subroutine is defined, it only needs to be set up once. Afterward, you can call it multiple times as required for usage.
PROCEDURE calculate_area(length: INTEGER, width: INTEGER)
area ← length * width
OUTPUT "The area is " + area
END PROCEDURE
To call the procedure you would use the following pseudocode:
calculate_area(5,3)
def calculate_area(length, width):
area = length * width
print("The area is ", area)
calculate_area(5,3)
public class Main {
static void calculateArea(int length, int width) {
int area = length * width;
System.out.println("The area is " + area);
}
public static void main(String[] args) {
calculateArea(3,6);
}
}
Sub CalculateArea(length As Integer, width As Integer)
Dim area As Integer = length * width
Console.WriteLine("The area is " & area)
End Sub
CalculateArea(5, 3)
FUNCTION calculate_area(length: INTEGER, width: INTEGER)
area ← length * width
RETURN area
END PROCEDURE
To output the value returned from the function you would use the following pseudocode:
OUTPUT(calculate_area(5,3))
def calculate_area(length, width):
area = length * width
return area
print(calculate_area(5,3))
public class Main {
static int calculateArea(int length, int width) {
int area = length * width;
return area;
}
public static void main(String[] args) {
System.out.println(calculateArea(3,6));
}
}
CalculateArea(5, 3)
Sub CalculateArea(length As Integer, width As Integer)
Dim area As Integer = length * width
Return area
End Sub
92 docs|30 tests
|
1. What is the difference between a subroutine, a procedure, and a function? |
2. How are subroutines, procedures, and functions used in programming? |
3. Can a function call a procedure or another function within its code? |
4. What are some common programming languages that support subroutines, procedures, and functions? |
5. How do subroutines, procedures, and functions help with debugging and troubleshooting in programming? |
|
Explore Courses for Year 11 exam
|