views:

964

answers:

10

Generally speaking, we all hear about the "functions" or the "procedures" in programming languages. However, I just found out that I use these terms almost interchangeably (which is probably very wrong).

So, my question is: What is the difference in terms of their functionality, their purpose and use? An example would be appreciated.

+5  A: 

In general, a procedure is a sequence of instructions.
A function can be the same, but it usually returns a result.

HS
+25  A: 

A function returns a value and a procedure just executes commands.

The name function comes from math. It is used to calculate a value based on input.

A procedure is a set of command which can be executed in order.

In most programming languages, even functions can have a set of commands. Hence the difference is only in the returning a value part.

But if you like to keep a function clean, (just look at functional languages), you need to make sure a function does not have a side effect.

Gamecat
Thats what I was going to say... :)
Nick Haslam
+2  A: 

There's a term subroutine or subprogram which stands for a parameterized piece of code that can be called from different places.

Functions and procedures are implementations of those. Usually functions return values and procedures don't return anything.

sharptooth
+2  A: 

In most contexts: a function returns a value, while a procedure doesn't. Both are pieces of code grouped together to do the same thing.

In functional programming context (where all functions return values), a function is an abstract object:

f(x)=(1+x)
g(x)=.5*(2+x/2)

Here, f is the same function as g, but is a different procedure.

xtofl
A: 

If we're language-agnostic here, procedure usually specifies a series of acts required to reliably and idempotently achieve certain result. That is, a procedure is basically an algorithm.

Functions, on the other hand, is a somewhat independent piece of code within a larger program. In other words, function is the implementation of a procedure.

Anton Gogolev
+2  A: 

Example in C:

// function
int square( int n ) {
   return n * n;
}

// procedure
void display( int n ) {
   printf( "The value is %d", n );
}

Although you should note that the C Standard doesn't talk about procedures, only functions.

anon
+1  A: 

This depends on the context.

In Pascal-like languages, functions and procedures are distinct entities, differing in whether they do or don't return a value. They behave differently wrt. the language syntax (eg. procedure calls form statements; you cannot use a procedure call inside an expression vs. function calls don't form statements, you must use them in other statements). Therefore, Pascal-bred programmers differentiate between those.

In C-like languages, and almost all other contemporary languages, this distinction is gone; in statically typed languages, procedures are just functions with a funny return type. This is probably why they are used interchangeably.

jpalecek
+1  A: 

More strictly, a function f obeys the property that f(x) = f(y) if x = y, i.e. it computes the same result each time it is called with the same argument (and thus it does not change the state of the system.)

Thus, rand() or print("Hello"), etc. are not functions but procedures. While sqrt(2.0) should be a function: there is no observable effect or state change no matter how often one calls it and it returns always 1.41 and some.

Ingo
This usage is relavent in the context of "functional" programming. Be aware that many (often imperative) languages which call their subprograms "functions" do not require this property.
dmckee
I didn't suggest that programming languages require this property. Anyway, one can write strict functions in any language, and I feel it is good habit to program as much as possible in clean functions, then glue the pieces together with some main procedure.
Ingo
A: 

BTW You forgot sub-routine

Rob Wells
A: 

i think , given answer is useless and create more confustion.