“기능”과“절차”의 차이점은 무엇입니까?
일반적으로 우리 는 프로그래밍 언어 의 기능 이나 절차 에 대해 듣습니다 . 그러나 나는이 용어를 거의 상호 교환 적으로 사용한다는 것을 알았습니다.
그래서 내 질문은 :
기능, 목적 및 사용 측면에서 차이점은 무엇입니까?
예를 들어 주시면 감사하겠습니다.
함수는 값을 반환하고 프로시 저는 명령을 실행합니다.
이름 함수는 수학에서 나옵니다. 입력을 기준으로 값을 계산하는 데 사용됩니다.
프로시 저는 순서대로 실행될 수있는 명령 세트입니다.
대부분의 프로그래밍 언어에서 함수조차도 일련의 명령을 가질 수 있습니다. 따라서 차이는 값을 반환하는 부분에만 있습니다.
그러나 함수를 깨끗하게 유지하려면 (기능 언어 만 살펴보십시오) 함수에 부작용이 없는지 확인해야합니다.
상황에 따라 다릅니다.
파스칼과 같은 언어에서 함수와 프로시 저는 값을 반환하는지 아닌지에 따라 다른 엔티티입니다. 그들은 다르게 행동한다. 언어 구문 (예 : 프로시 저는 양식 문을 호출합니다. 식 내부에서는 프로 시저 호출을 사용할 수 없습니다. 함수 호출은 문을 구성하지 않습니다. 다른 문에서 사용해야합니다). 따라서 파스칼 품종 프로그래머는 이들을 구별합니다.
C와 같은 언어와 다른 많은 현대 언어에서 이러한 차이는 사라졌습니다. 정적으로 유형이 지정된 언어에서 프로시 저는 재미있는 반환 유형을 가진 함수일뿐입니다. 이것이 아마도 그것들이 상호 교환 적으로 사용되는 이유 일 것입니다.
기능적 언어에서는 일반적으로 프로 시저와 같은 것은 없습니다. 모든 것이 기능입니다.
C의 예 :
// function
int square( int n ) {
return n * n;
}
// procedure
void display( int n ) {
printf( "The value is %d", n );
}
C 표준은 절차에 대해서만 언급하지 않으며 기능 만 수행합니다.
일반적으로 절차는 일련의 지침입니다.
함수는 동일 할 수 있지만 일반적으로 결과를 반환합니다.
다른 장소에서 호출 할 수있는 매개 변수화 된 코드를 나타내는 서브 루틴 또는 서브 프로그램 이라는 용어 가 있습니다.
기능과 절차는 그것들의 구현입니다. 일반적으로 함수는 값을 반환하고 프로시 저는 아무것도 반환하지 않습니다.
기본 차이점
- 함수는 값을 반환해야하지만 저장 프로 시저에서는 선택 사항입니다. 프로시 저는 0 또는 n 값을 반환 할 수 있습니다 .
- 함수는 입력 매개 변수 만 가질 수있는 반면, 프로시 저는 입력 / 출력 매개 변수를 가질 수 있습니다.
- 함수의 경우 하나의 입력 매개 변수를 사용해야하지만 저장 프로시 저는 0- n 개의 입력 매개 변수 를 사용할 수 있습니다 .
- 함수는 프로 시저에서 호출 할 수 있지만 프로시 저는 함수에서 호출 할 수 없습니다.
고급 차이점
- 프로 시저에서 try-catch 블록으로 예외를 처리 할 수있는 반면 try-catch 블록은 함수에서 사용할 수 없습니다.
- 트랜잭션 관리는 프로 시저에서 할 수 있지만 기능에서는 할 수 없습니다.
SQL에서 :
- 프로 시저가 있습니다
SELECT
DML (뿐만 아니라INSERT
,UPDATE
,DELETE
기능 만 허용하는 반면, 거기에) 문SELECT
안에 문을. SELECT
명령문에는 프로 시저를 사용할 수 없지만 명령문에는 함수를 임베드 할 수 있습니다SELECT
.- 저장 프로시 저는
WHERE
(또는 aHAVING
또는SELECT
) 블록의 어느 곳에서나 SQL 문에서 사용할 수 없지만 함수는 사용할 수 있습니다. - 테이블을 반환하는 함수는 다른 행 집합으로 취급 될 수 있습니다.
JOIN
다른 테이블과 함께 블록에 사용할 수 있습니다 . - 인라인 함수는 매개 변수를 사용하는 뷰로 생각할 수 있으며
JOIN
블록 및 기타 행 집합 작업에 사용할 수 있습니다 .
더 엄격하게, 함수 f는 x = y 인 경우 f (x) = f (y)라는 속성을 따릅니다. 즉 , 동일한 인수로 호출 될 때마다 동일한 결과를 계산 하므로 (따라서 상태를 변경하지 않습니다. 체계.)
따라서 rand () 또는 print ( "Hello") 등은 함수가 아니라 절차입니다. sqrt (2.0)는 함수 여야하지만, 호출 횟수와 상관없이 관찰 가능한 효과 나 상태 변경은 없으며 항상 1.41과 일부를 반환합니다.
내부 프로 시저에서는 DML (삽입 / 업데이트 / 삭제) 문을 사용할 수 있지만 내부 함수에서는 DML 문을 사용할 수 없습니다.
프로시 저는 입력 / 출력 매개 변수를 모두 가질 수 있지만 기능은 입력 매개 변수 만 가질 수 있습니다.
저장 프로 시저에서 Try-Catch 블록을 사용할 수 있지만 기능상 Try-Catch 블록을 사용할 수 없습니다.
Select 문에서는 저장 프로 시저를 사용할 수 없지만 기능에서는 Select 문에서 사용할 수 있습니다.
저장 프로시 저는 0 또는 n 개의 값 (최대 1024)을 반환 할 수 있지만 함수는 필수 값인 1 개의 값만 반환 할 수 있습니다.
저장 프로시 저는 함수에서 호출 할 수 없지만 저장 프로 시저에서 함수를 호출 할 수 있습니다.
We can use transaction in Stored Procedure, But In function we can not use transaction.
We can not use Stored Procedure in Sql statement anywhere in the Where/Having/select section, But In function we can use.
We can not join Stored Procedure, But we can join function.
for more.. click here...http://dotnet-developers-cafe.blogspot.in/2013/08/difference-between-stored-procedure-and.html
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.
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.
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.
Function can be used within a sql statement whereas procedure cannot be used within a sql statement.
Insert, Update and Create statements cannot be included in function but a procedure can have these statements.
Procedure supports transactions but functions do not support transactions.
Function has to return one and only one value (another can be returned by OUT variable) but procedure returns as many data sets and return values.
Execution plans of both functions and procedures are cached, so the performance is same in both the cases.
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.g. where x -> x'
for a one dimensional function). 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.
// The following is pseudo code:
g(x) = {
if (morning()) {
g = 2 * x;
}
else {
g = x;
}
return g;
}
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:
// We can immediately translate this definition into a recursive procedure
// for computing Fibonacci numbers:
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
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
.
A representation of this machine might look like
make-pies (fruit, milk, flower, eggs, sugar, heat) = {
return (heat (add fruit (mix eggs flower milk)))
}
Of course that's not the only way to make a pie.
In this case we can see that:
A function is to a machine
as a procedure is to a recipe
as attributes are to ingredients
as output is to product
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.
In terms of С#/Java, function is the block of code, which return particular value, but procedure is the block of code which return void (nothing). In C#/Java both functions and procedures more often called just methods.
//This is a function
public DateTime GetCurrentDate()
{
return DateTime.Now.Date;
}
//This is a procedure(always return void)
public void LogMessage()
{
Console.WriteLine("Just an example message.");
}
Procedures: 1.Procedures are the collections of statements that defines parameterized computations. 2.Procedures cannot return values.
3.Procedures cannot be called from function.
Functions 1.Functions structurally resemble procedures but are semantically modeled on mathematical functions. 2.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
참고URL : https://stackoverflow.com/questions/721090/what-is-the-difference-between-a-function-and-a-procedure
'Programing' 카테고리의 다른 글
Gitignore가 작동하지 않습니다 (0) | 2020.05.17 |
---|---|
PHP가 null이거나 비어 있습니까? (0) | 2020.05.17 |
파이썬에서 float를 정수로 변환하는 가장 안전한 방법은 무엇입니까? (0) | 2020.05.16 |
Windows에서 파일의 대소 문자를 변경 하시겠습니까? (0) | 2020.05.16 |
MySQL의 열에서 동일한 값을 가진 행 찾기 (0) | 2020.05.16 |