Junior — Middle
What is the difference between a code block and a separate function in programming?
sobes.tech AI
Answer from AI
A code block is a sequence of instructions grouped together, for example, inside curly braces or indentation, which are executed sequentially. It does not have a name of its own and cannot be called separately.
A separate function is a named block of code that can be called from different parts of a program, pass parameters, and receive results. Functions help structure code, make it reusable, and facilitate testing.
Example in Python:
# Code block
if x > 0:
print("Positive number")
y = x * 2
# Function
def double(number):
return number * 2
result = double(x)
print(result)