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

How to connect to a database?

571


What are the different types of recovery operation?

538


How many types of recording modes in qtp? Which will be used when?

540


What ate the Technologies supported by QTP?

636


How do you test the text displayed in the header portion of times of india epaper. Hot news(banner) are scrolling in the top of the page, how do you test using QTP?

1494






regular expression in qtp standar check point for months from accepting months from january to december only

1465


What is Distributed testing? How does UFT support it?

597


How to handle dynamic objects in quicktest professional?

486


How to start recording using quicktest professional (qtp)?

586


banking domain description for software tester for interview

3456


Explain the check points in quicktest professional?

533


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

1590


Suppose I want fail that check point? How can you do that?

592


How to attach a file to TD?

642


Hi all Can u give me the link where we can download full version of qtp my mail id is karthic.venkitapathi@gmail.com

1338