3

I have read about function vs procedure function and procedure both are subroutines but function returns a value and procedure doesn't.

Can a function returning void be called a procedure?

Give reasons with definitions...

Christophe
  • 74,672
  • 10
  • 115
  • 187

3 Answers3

6

Yes, this is a common terminology.

Pascal goes so far as to have a separate procedure keyword for subroutines that do not return.

Telastyn
  • 108,850
  • 29
  • 239
  • 365
  • 1
    Yep VB has this too (at least in VB.NET) where function/sub are different keywords. – Graham Mar 27 '20 at 13:44
  • Yes, pascal indeed was a forerunner on that distinction (at that time vb didn’t exist and basic used gosub that doesn’t know return values). In mathematics for a very long time there was no 0 so "nothing" had always to be treaded as a special case. 0 allowed to unify this and handle nothing exactly as any other value. Likewhise, void did the same for types and allowed to unify functions and procedures. – Christophe Mar 27 '20 at 17:13
2

There is some consensus among programmers of a certain age about the meaning of the terms. Just be aware that it is all culture related.

When a new language or paradigm was introduced, the people selling it would choose new terms to go with their product to make it stand out. As more of these products were born, we soon ran out of words that made sense. To some however being unique and distinct was more important. That is how we got the method. It does not get any more stupid than that and fortunately everyone realized that so the craze was put to an end and from that point on we did with the monikers we had.

It depends on the scene you are in whether people insist on the use of certain terms and whether they attribute a particular meaning to them. Ultimately they are just words that have existed centuries before the first computers emerged. The etymology of function and procedure is not strong in a computer science context, although a Pascal programmer may have a strong opinion about it.

Martin Maat
  • 18,218
  • 3
  • 30
  • 57
1

It must be borne in mind that most of these terms, in computing contexts, are more or less synonyms. Some appear to have been drawn from mathematics, others from what might broadly be called administrative science, which predate computers as we know them.

I would tentatively suggest that they are all small variations of what programmers recognise as methods or calls.

A function tends to imply that a value is "returned" - that is, it produces what I believe C programmers know as an "r-value".

A subroutine or procedure meanwhile may provide an "out" parameter or modify a parameter passed in by reference, without being considered a function. This is typical of procedural programming, where a series of calls may manipulate a common parameter which is passed to each of the calls in turn.

For a programmer this distinction is typically a matter of style or convenience rather than fundamental (and many calls to methods involve a mix and match of both return values and out parameters).

In many languages there is no syntax distinction between functions and procedures, and even where the distinction seems to exist such as in VB, in reality the return value of a function can be ignored, and both functions and subroutines are capable of providing out parameters.

Now, can a "function" returning void be called a procedure? I would say no, because such a method is not a function in the first place, unless the word function is used synonymously with procedure (thus no distinction in meaning exists anyway).

Steve
  • 6,998
  • 1
  • 14
  • 24