What is difference between function and procedure?

Answer Posted / smruti

ALL THE BELOW ARE NOT CORRECT GUYS. WHY DON'T VERIFY ONE BY
ONE : CHECK THE COMMENTS


1. Function is mainly used in the case where it must return
a value. Where as a procedure may or may not return a value.
or may return more than one value using the OUT parameter.


CORRECT.(DIFFERENCE NUMBER # 1) MAKE A NOTE.

2. Function can be called from SQL statements where as
procedure can not be called from the sql statements

THIS IS PARTIALLY TRUE, BECAUSE A STATEMENT IS TRUE IF
EVERY TEST CASE IS TRUE. A FUNCTION CONTAINING DMLS CAN'T
BE CALLED FROM SQL STMT SO ITS SAME AS PROCEDURE.

-- > NO DIFFERENCE


3. Functions are normally used for computations where as
procedures are normally used for executing business logic.

WHO SAID FUNCTIONS ARE ONLY USED FOR CALCULATIONS. CAN'T
YOU UPDATE A TABLE OR VALIDATE SOMETHING?

--> NO DIFFERENCE

4. You can have DML (insert,update, delete) statements in a
function. But, you cannot call such a function in a SQL
query.

DIFINED ABOVE

--> NO DIFFERENCE

5. Function returns 1 value only. Procedure can return
multiple values (max 1024).

-- > DIFINED ALREADY IN POINT 1 THE ONLY DIFFERENCE

6.Stored Procedure: supports deferred name resolution.
Example while writing a stored procedure that uses table
named tabl1 and tabl2 etc..but actually not exists in
database is allowed only in during creation but runtime
throws error Function wont support deferred name resolution.

WHO SAID PROCEDURE DONT RAISE ERROR IF YOU CREATE IT
WITHOUT A DATABASE TABLE. IT GIVES WARNING AND IF U CREATE
A FUNCTION ALSO WITHOUT A TABLE IT GETS CREATED WITH
WARNING.

--> NO DIFFERENCE

SQL> CREATE OR REPLACE PROCEDURE PC1 (P1 IN NUMBER) IS
2 DUMMY NUMBER;
3 BEGIN
4 SELECT 1 INTO DUMMY FROM PROD;
5 END;
6 /

Warning: Procedure created with compilation errors.

SQL> SHOW ERROR
Errors for PROCEDURE PC1:

LINE/COL ERROR
-------- ---------------------------------------------------
--------------
4/1 PL/SQL: SQL Statement ignored
4/26 PL/SQL: ORA-00942: table or view does not exist

==========================================================

SQL> CREATE OR REPLACE FUNCTION FUNC2 (P1 IN NUMBER,
2 P2 OUT NUMBER)
3 RETURN NUMBER IS
4 L_DUMMY NUMBER;
5 BEGIN
6 PC1 (1);
7 SELECT 1 INTO L_DUMMY FROM PROD;
8 RETURN L_DUMMY;
9 END;
10 /

Warning: Function created with compilation errors.

SQL> SHO ERR
Errors for FUNCTION FUNC2:

LINE/COL ERROR
-------- ---------------------------------------------------
--------------
7/1 PL/SQL: SQL Statement ignored
7/28 PL/SQL: ORA-00942: table or view does not exist



7.Stored procedure returns always integer value by default
zero. where as function return type could be scalar or table
or table values

--> NOT ALWAYS TRUE SO NO DIFFERENCE

8. Stored procedure is precompiled execution plan where as
functions are not.


--> WRONG NO DIFFERENCE

9.A procedure may modify an object where a function can only
return a value The RETURN statement immediately completes
the execution of a subprogram and returns control to the
caller.

--> FUNCTION CAN MODIFY AN OBJECT BY DMLS
--> NO DIFFERENCE



SO FINALLY YOU WILL GET ALMOST NO DIFFERENCE EXCEPT ONE
FUNCTION RETURNS ONLY 1 VALUE WHERE AS PROCEDURE MAY OR MAY
NOT OR MORE THAN ONE, WHICH IS ALSO FALSE WHEN PROCEDURE
MADE TO RETURN ONE VALUE.

-> MAY BE YOU WILL FIND THIS USEFUL.

RUN AN EXECUTION PLAN ON PROC AND FUNC YOU WILL FIND
FUNCTION EXECUTES MUCH MUCH FASTER THAN PROC BECAUSE THE
RETURN CLAUSE IN FUNCTION IS A PRECOMPILER DIRECTIVE
LIKE "PRAGMA" WHICH SHOW THIS IS GOING TO RETURN ONE VALUE
DEFINITELY AND THAT TO OF SPECIFIC DATATYPE SO COMPILER
GETS READY FOR IT WITH THE PRE-INSTRUCTIONS.

Guys this is nothing personal but the facts i found by
experimenting. Let me know if you found anything false.

Tx

Is This Answer Correct ?    13 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why do we use "Text checkpoint" If we can make use of it in "standard checkpoint" ?

1486


what is object and child object.. explain ..if possible give some example for better understanding ..

1533


1.Write a CRITICAL test cases for calculator ? 2.What is the difference b/w FRS and SRS documents ? 3.What is Component ? 4.What is object ? 5.what is the difference b/w static and dynamic descriptive programming ?

1592


i am new QTP... please tell me automation frameworks , types & for whiich type of applications frameworks are used & how to generate script in keyword driven framework

1610


how will load the object during runtime?

1586






what is meant by Implicit and explicit requirements?

3627


how can you do retesting (datadriven test) using notepad amd by using functions NOTE u should not use datatable methods

1384


Hello friends..... On which areas of an application we can not test with QTP

1559


describe some problems that u had with automating testing tool?

1417


In a flight window we have to enter the name and meal request for every passenger.In that window if we give Total passengers=1 then the Psngr1 name field and psngr1 meal request field will reflects.if we give Total passengers=2 then Psngr1 name field and psngr1 meal request field,Psngr2 name field and psngr2 meal request field and so on. how we can handle this scenerio thru Descriptive programming?

1444


What are the differences between qtp and winrunner?

537


what is command for executing files?

1517


Whats the realtime QTP testing process

1991


How qtp recognizes the object?

673


what is defenetion of kiran can u tell me plz CVS, SVN

1572