Functions, Procedures, Scope & Parameters.
Function: Returns a value.
Procedure: Does NOT return a value (just performs an action).
50 10
Explanation: `update_score` creates a new local variable `score`. The global `score` remains 10.
(a) Line 02
(b) The parameter `user_input` is overwritten by a new input inside the function, making the passed argument useless and the function not reusable.
| a | b | c | Output |
|---|---|---|---|
16
(c=8. 8 is not > 10. Returns 8 * 2).
get_discount(price).
function get_discount(price)
if price > 100 then
price = price * 0.9
endif
return price
endfunction
Note: Can also use return price inside if/else blocks.
(a) Use a Sub-program (Function/Procedure).
(b) Code Reusability (write once, use many times), Easier Maintenance/Debugging (fix in one place), Decomposability (break down complex problems).
Arguments. (Parameters are the variable names in the definition).