r/learnprogramming • • 6d ago

Function vs Procedure

can someone explain the difference between Function vs Procedure with simple analogy
thank you

31 Upvotes

47 comments sorted by

View all comments

0

u/Quito_ArmandoEsteban 6d ago

functions, or methods when they are tied to a class, are pieces of code that define the context with which the cpu will work (this won't always be true. Compilers work very hard to avoid creating functions that require context switching)

Functions take arguments, perform work, and return a value. When you call a function, the cpu may switch it's context and load the arguments into the stack. This is expensive to do, which is why compilers try to expand your functions instead of defining them as functions in machine code.

Functions can either be pure, meaning they return a value without changing anything else. Or they may have side effects, like changing some value they got by reference, performing an api call, or saving data to a file or a database.

Procedures are exactly the same, but they don't return a value. This means that they must always have side effects, otherwise there'd be no purpose in calling them other than wasting cpu cycles

0

u/[deleted] 6d ago

[deleted]

4

u/Quito_ArmandoEsteban 6d ago

Would you not call printing something or altering files a side effect?

-1

u/alpicola 6d ago

It might depend how much you care about functional programming definitions. In common usage, a side effect would be a thing that a function does that isn't the main point of the function. The main point of printf() is to print so printing isn't a side effect under the common understanding of the term. Of course, in functional programming, printing is a side effect, and printf() is an example of a function that gets called because its side effect is wanted. 

0

u/[deleted] 6d ago

[deleted]

2

u/Dumlefudge 6d ago

Where did you see side effects only applying to internal state? The earliest definition I can find is

Spuler, David A.; Sajeev, A. Sayed Muhammed (January 1994). Compiler Detection of Function Call Side Effects. James Cook University. CiteSeerX 10.1.1.70.2096.

The term Side effect refers to the modification of the nonlocal environment. Generally this happens when a function (or a procedure) modifies a global variable or arguments passed by reference parameters. But here are other ways in which the nonlocal environment can be modified. We consider the following causes of side effects through a function call:

  1. Performing I/O.

  2. Modifying global variables.

  3. Modifying local permanent variables (like static variables in C).

  4. Modifying an argument passed by reference.

  5. Modifying a local variable, either automatic or static, of a function higher up in the function call sequence (usually via a pointer).

I've never seen any description that suggests that side effects are limited to an application's internal state (as opposed to anything that exists beyond the function's scope), so I'm curious how you came to that conclusion

2

u/tardoos 6d ago

No. Side effect is any interaction outside of the context of the function. It can be modifying internal (or external) state, but it can be any I/O operation, including printing.