Resources | Subject Notes | Computer Science
A procedure is a sequence of instructions that performs a specific task. It's a set of steps designed to achieve a particular outcome. Think of it as a recipe ÔÇô a series of actions you follow to get a result.
A function is a named block of code that performs a specific task. It's a more structured and reusable version of a procedure. Functions can accept input values (parameters) and can return a value as a result of their operation.
In programming, procedures and functions are defined using specific keywords and syntax. The exact syntax will depend on the programming language being used (e.g., Python, Pascal, etc.).
Generally, a procedure or function definition includes:
Parameters are variables that are passed to a function or procedure when it is called. They allow the procedure/function to work with different data each time it is executed. There are two main types of parameters:
Consider a procedure that calculates the area of a rectangle. It would take the length and width as parameters.
Parameter Name | Purpose |
---|---|
length | The length of the rectangle. |
width | The width of the rectangle. |
The procedure would then use these parameters to calculate the area: Area = length * width
.
Consider a function that adds two numbers. It would take two numbers as parameters and return their sum.
Parameter Name | Purpose |
---|---|
number1 | The first number to add. |
number2 | The second number to add. |
The function would perform the addition: sum = number1 + number2
and then return the value of sum
.