Resources | Subject Notes | Computer Science
In computer programming, procedures and functions are fundamental building blocks that allow us to organize code, make it reusable, and improve readability. They help break down complex tasks into smaller, manageable units.
A procedure is a sequence of instructions that performs a specific task. It's a self-contained block of code that can be executed when needed. Procedures do not necessarily return a value.
Example (Python):
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
In this example, `greet` is a procedure that takes a name as input and prints a greeting. It doesn't return any value.
A function is a type of procedure that does return a value. Functions can accept input values (parameters) and produce an output. They are essential for modular programming.
Example (Python):
def add(x, y):
return x + y
result = add(5, 3)
print(result)
Here, `add` is a function that takes two parameters, `x` and `y`, adds them together, and returns the result. The returned value is then stored in the variable `result` and printed.
Parameters are variables that are used to pass data into a function or procedure. They act as placeholders for values that will be used within the code.
There are different types of parameters:
Functions can be categorized based on the number of parameters they accept:
Function Type | Description | Example (Python) |
---|---|---|
No Parameters | The function does not require any input values. |
|
One Parameter | The function requires one input value. |
|
Two or More Parameters | The function requires multiple input values. |
|
By using procedures and functions, you can: