Have Queries? Live Training Schedules. We use cookies to make interactions with our websites and services easy and meaningful. Please read our Privacy Policy for more details. Remember me Forgot Password? Login Please Wait Sign in with Facebook. Don't have DotNetTricks account? Sign Up. The Password must contains atleast 8 chars including digit, lowercase and uppercase. Sign Up Please Wait Already have DotNetTricks account?
In a textbook, specific concepts are covered on a section-by-section or paragraph-by-paragraph basis. Similarly, in a computer program, specific functionality is divided up into named functions and procedures. Programs usually integrate blocks of code and modules that have already been created in other projects. The algorithms a program uses are implemented as the functions and procedures in these modules. BBC's Simon Lumb describes functions and how they are used in programming. In a computer program there are often sections of the program that we want to re-use or repeat.
Chunks of instructions can be given a name - they are called functions and procedures. Algorithms can be broken down into procedures or functions. I object with something I keep seeing over and over in most of these answers, that what makes a function a function is that it returns a value. A function is not just any old method that returns a value. Not so: In order for a method to be a real function it must return the same value always given a specific input.
An example of a method that is not a function is the random method in most languages, because although it does return a value the value is not always the same. A function therefore is more akin to a map e. This is a very important distinction between regular methods and functions because when dealing with real functions the timing and the order in which they are evaluated should never matter where as this is not always the case with non functions. Here's another example of a method that is not a function but will otherwise still return a value.
I further object to the notion that procedures do not return values. A procedure is just a specific way of talking about a function or method. So that means if the underlying method that your procedure defines or implements returns a value then, guess what that procedure returns a value.
Take for example the following snippet from the SICP :. Have you heard of recursive procedures much lately? They are talking about a recursive function a real function and it's returning a value and they are using the word "procedure".
So what's the difference, then? Well another way of thinking of a function besides the meaning mentioned above is as an abstract representation of an ideal like the numeral 1. A procedure is that actual implementation of that thing. I personally think they are interchangeable. Note, if you read that chapter from the link I provide you may find that a harder concept to grasp is not the difference between a function and a procedure, but a process and a procedure.
Did you know that a recursive procedure can have an iterative process? An analog for procedures are recipes. For example; suppose you have a machine called make-pies this machine takes in ingredients of fruit, milk, flower, eggs, sugar, heat and this machine returns a pie. That analogy is OK but it breaks down when you take into account that when you are dealing with a computer program everything is an abstraction.
So unlike in the case of a recipe to a machine we are comparing two things that are themselves abstractions; two things that might as well be the same thing. And I hold that they are for all intent and purposes the same thing. In the context of db : Stored procedure is precompiled execution plan where as functions are not. Procedures: 1. Procedures are the collections of statements that defines parameterized computations. Procedures cannot return values. Functions 1.
Functions structurally resemble procedures but are semantically modeled on mathematical functions. It can return values 3. Function can be called from procedures. Procedures and functions are both subroutines the only difference between them is that a procedure returns multiple or at least can do values whereas a function can only return one value this is why function notation is used in maths as usually only one value is found at one given time although some programming languages do not follow these rules this is their true definitions.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is the difference between a "function" and a "procedure"? Ask Question. Asked 12 years, 7 months ago.
Active 10 months ago. Viewed k times. So, my question is: What is the difference in terms of their functionality, their purpose and use? An example would be appreciated. See also: stackoverflow. I think SICP gets this right. Functions exist only in math, and they represent what is knowledge. Procedures exist in programming languages including functional ones , and they represent how to knowledge. Procedure : define sqrt x newtons-method lambda y - square y x 1. Add a comment.
Active Oldest Votes. 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 commands which can be executed in order. Toon Krijthe Toon Krijthe How can you assure no side effects either in an imperative java, c or declarative language scala, scheme? Their scope restrictions prevent them from having side effects.
On the other hand, imperative languages exploit their side effects explicitly. Side effects are not always bad. I'm reading the following Ada tutorial goanna. A procedure can contain return statements. Is this an error in the text? Or does it mean that it can have return statements but don't return any values? In pascal, procedures do not have return statements, only functions do. Must be an error in the text. However, a procedure can have an "exit" statement, which could act as a "return" statement without arguments, meaning no return values.
Show 4 more comments. This depends on the context. That's because it only has functions. A function that returns nothing is a void function. The answer is not wrong, and it is a good example of the difference between pure functions and procedures.
C might be a better language if it distinguished real functions from procedures, this would help with static analysis, performance optimization, and parallelization.
Basic Differences A Function must return a value but in Stored Procedures it is optional: a procedure can return 0 or n values.
0コメント