0

A procedure is a sequence of program instructions that perform a specific task, packaged as a unit (Techopedia).

We use block structure to make our code more readable by using indentations and spaces.

Now, is one block the same as a procedure and is vice versa true?

1 Answers1

1

Such terminology is language specific. In programming languages I've seen, they are not the same.

A procedure (or function) definition is a block of code with a header, that specifies 0 or more arguments, and a name. It allows the procedure to be called by specifying its name and a list of argument values. In many languages, anonymous functions or procedures can be defined, which have a block of code and a header with arguments, but no name. In some languages, a block can act as an anonymous procedure or function without arguments; in other languages, it cannot.

For instance, in C, Java and C#, a block just serves to group statements and create a scope for local variables; it cannot be used as a procedure/function.

In Perl, on the other hand, blocks can be used in contexts that expect procedures/functions (subroutines), and in PostScript, a block denotes a code object (a procedure) that can be bound to a name.

I've never used blocks in Lisp, but apparently, the situation is much the same there: a Lisp procedure (function, in Lisp) contains a block, implicitly, but a block by itself does not constitute a procedure. It cannot be called by giving its name and values for its arguments, if I understand correctly.

reinierpost
  • 5,509
  • 1
  • 21
  • 38