I need some clarification about the concept of functions and procedures. I know some characteristics of each one, but I'm still not sure about one thing. When writing pseudocode for a program (the algorithm), do I necessarily need to put a variables or its optional?
This an example of what I mean:
Algorithm Test
Function Set(a: integer): integer
begin
a <-- 0
return a
end.
I'm not even sure if it's a function or should I call it a procedure? Notice I didn't mention the variables.
CodePudding user response:
Your pseudo code example describes a function.
There are several programming languages that make a distinction between procedure and function, like Pascal and PL/SQL: the difference is that a procedure does not return a value to the caller, while a function does (as does your code). Other programming languages do not make this distinction and allow the creation of functions that do not return anything (e.g. void
in Java).
When there are local variables that are not parameters, then it is good practice to declare them as such, even in pseudo code. But in your example, the only local variable (a
) is a parameter, which is already declared in the function header, so nothing more is needed.
Some programming languages have a separate section for declaring variables. Again, Pascal is an example of such a language. It can have a var
section before the begin
keyword, and following that var
keyword the local variables are declared. How you write this in pseudo code is less important, as pseudo code is not standardised. The more important requirement is that pseudo code leaves no doubt to the reader of what is intended.
CodePudding user response:
Both the term function and the term procedure are very similar, but generally: A procedure is a set of steps for solving a problem A function is a modular set of steps for solving a problem in code.
The example you gave lends itself to being called a function, although this could be an attribute of fuzzy English.