The Wayback Machine - https://web.archive.org/web/20240809010401/https://www.geeksforgeeks.org/difference-between-function-and-procedure/
Open In App

Difference between Function and Procedure

Last Updated : 01 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Pre-requisites: What is SQL?

Structured Query Language is a computer language that we use to interact with a relational database.SQL is a tool for organizing, managing, and retrieving archived data from a computer database.

In this article, we will see the difference between Function and Procedure.

Function:

The function is one of the fundamental thoughts in computer programming. It is used to calculate something from a given input. Hence it got its name from Mathematics. The function can be either user-defined or predefined. The function program has a block of code that performs some specific tasks or functions. 

CREATE [OR REPLACE] FUNCTION function_name 
[(parameter_name  type [, …])]

// this statement  is must for functions 
RETURN return_datatype  
{IS | AS}

BEGIN 
   // program code

[EXCEPTION
  exception_section;

END [function_name];

Example:

create function MultiplyNumbers(@int1 as int,@int2 as int)
As
BEGIN
Return (@int1 * @int2)
end

Procedure:

In programming a particular set of instructions or commands along known as a procedure. Counting on the programming language it is known as a procedure, subroutine, function, or subprogram. 

CREATE or REPLACE PROCEDURE name(parameters)

IS

variables;

BEGIN

//statements;

END;

Example:

CREATE or REPLACE PROCEDURE INC_SAL(eno IN NUMBER, up_sal OUT NUMBER)
IS
BEGIN
UPDATE emp_table SET salary = salary+1000 WHERE emp_no = eno;
COMMIT;
SELECT sal INTO up_sal FROM emp_table WHERE emp_no = eno;
END; 

Difference between Function and Procedure:

S.NO Function Procedure
1. Functions always return a value after the execution of queries. The procedure can return a value using “IN OUT” and “OUT” arguments.
2. In SQL, those functions having a DML statement can not be called from SQL statements. But autonomous transaction functions can be called from SQL queries. A procedure can not be called using SQL queries.
3. Each and every time functions are compiled they provide output according to the given input. Procedures are compiled only once but they can be called many times as needed without being compiled each time.
4. A Function can not return multiple result sets.
 
A procedure is able to return multiple result sets.
5. The function can be called using Stored Procedure.
 
While procedures cannot be called from function.
6. A function used only to read data. A procedure can be used to read and modify data. 
 
7. The return statement of a function returns the control and function’s result value to the calling program. While the return statement of the procedure returns control to the calling program, it can not return the result value.
8. The function does not support try-catch blocks. Procedure supports try-catch blocks for error handling.
9. A function can be operated in the SELECT statement. While it can’t be operated in the SELECT statement.
10. Functions do not permit transaction management.
 
It allows transaction management.
11. In functions, we can use only a table variable. Temporary tables can not be created in function.
 
In procedures, we can use temporary tables or table variables to store temporary data.


Similar Reads

Difference between Trigger and Procedure in DBMS
1. Procedures: A procedure is a combination of SQL statements written to perform specified tasks. It helps in code re-usability and saves time and lines of code. Advantages of Procedures: A Stored Procedure can be used as modular programming, which means that it can be created once, stored, and called multiple times as needed. This allows for speed
2 min read
Difference between Macro and Procedure
Assembly language is a common intermediate level programming language which is used for microprocessor programming. This macro and procedure are two concepts in assembly by which modular programming is implemented. So now let's understand how macro and procedure are different from each other. 1. Macro : Macro is a set of instruction and the program
3 min read
Basic Query in PL/SQL procedure
In this article, we will discuss the overview of PL/SQL and then our main focus on performing basic query operations in PL/SQL. And finally will conclude with Parameter modes in PL/ SQL subprograms. Let's discuss it one by one. Introduction :PL/SQL Stands for procedural language extension to SQL. In a procedure, the role of the subprogram is to per
3 min read
Difference between virtual function and inline function in C++
Virtual function: Virtual function is a member function which is declared within a base class and is redefined by a derived class. Inline function: Inline function is a normal function which is defined by the keyword inline, it is a short function which is expanded by the compiler and its arguments are evaluated only once. The syntax of defining th
2 min read
Difference Between Friend Function and Virtual Function in C++
A friend class can access private and protected members of other classes in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other classes. Just likely, a friend function is a function that is declared outside the scope of a class. This function can be invoked like a normal function and
3 min read
Difference between friend function and member function in C++
Friend Function: It is basically a function that is used to access all private and protected members of classes. It is considered as a non-member function of class and is declared by the class that is granting access. This function is prefixed using the friend keyword in the declaration as shown below: Class definition using friend function: C/C++
4 min read
Difference between Function.prototype.apply and Function.prototype.call
JavaScript treats everything as an object, even functions, and every object has its own properties and methods. Function objects have both apply() and call() methods on them. However, there is confusion about the two functions in JavaScript. The main difference between them is how they handle function arguments. There is no difference between these
2 min read
What Is the Difference Between VAR.S Function and VAR.P Function in Excel?
Functions in Excel are the pre-defined formulae used to obtain a certain value and to perform calculations using the specific value in a certain order. The basic functions are SUM, AVG, MAX, MIN, etc used for the range of cells. In order to obtain the correct results from the function, the correct syntax of the functions is required. In this articl
6 min read
Difference between ‘function declaration’ and ‘function expression' in JavaScript
Functions in JavaScript allow us to carry out some set of actions, important decisions, or calculations and even make our website more interactive. In this article, we will learn the difference between ‘function declaration’ and ‘function expression’. The similarity is both use the keyword function and the most prominent difference is that the func
2 min read
Difference between Virtual function and Pure virtual function in C++
Virtual Function in C++ A virtual function is a member function which is declared within a base class and is re-defined(Overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. Pu
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg