← Back to MySQL Course | Chapter 15: Stored Procedures & Functions | Lesson 3 of 5

User-Defined Functions

एक user-defined function आपके अपने calculator button जैसा है: इसे कुछ numbers दें और यह exactly एक answer return करता है जिसे आप किसी भी query के अंदर इस्तेमाल कर सकते हैं।
Syntax
sql
CREATE FUNCTION function_name(param datatype)
RETURNS return_datatype DETERMINISTIC
BEGIN
  RETURN value;
END

एक User-Defined Function क्या है?

एक user-defined function reusable logic package करता है जो हमेशा exactly एक value return करता है, जो आपको इसे किसी built-in function जैसे ही सीधे एक SELECT statement की column list या WHERE clause में डालने देता है।

उदाहरण: What is a User-Defined Function?

sql
DELIMITER //
CREATE FUNCTION FullName(first_name VARCHAR(50), last_name VARCHAR(50))
RETURNS VARCHAR(100) DETERMINISTIC
BEGIN
  RETURN CONCAT(first_name, ' ', last_name);
END //
DELIMITER ;
SELECT FullName('Amit', 'Sharma');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

कई Arguments वाले Functions

Functions built-in functions जैसे ही कई arguments accept कर सकते हैं, जो calculations के लिए उपयोगी है जो कई inputs combine करने पर depend करते हैं, जैसे एक price और एक percentage से discount compute करना।

उदाहरण: Functions with Multiple Arguments

sql
DELIMITER //
CREATE FUNCTION ApplyDiscount(price DECIMAL(10,2), pct INT)
RETURNS DECIMAL(10,2) DETERMINISTIC
BEGIN
  RETURN price - (price * pct / 100);
END //
DELIMITER ;
SELECT ApplyDiscount(100, 20);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Functions बनाम Stored Procedures

stored procedures से key distinction यह है कि functions को एक single value return करना ज़रूरी है और उन्हें किसी query के अंदर से call किया जाता है, जबकि procedures को CALL से invoke किया जाता है और वे कई result sets या कोई नहीं return कर सकती हैं।

उदाहरण: Functions vs Stored Procedures

sql
-- A function returns exactly one value and can be used in a SELECT list:
SELECT ApplyDiscount(100, 20);
-- A procedure is invoked with CALL and can return multiple result sets:
CALL GetAllUsers();

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

एक Function Drop करना

DROP FUNCTION database schema से permanently एक user-defined function हटाता है, बिल्कुल जैसे DROP PROCEDURE एक stored procedure हटाता है।

उदाहरण: Dropping a Function

sql
DROP FUNCTION IF EXISTS ApplyDiscount;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Deterministic और Non-Deterministic

एक deterministic mark किया गया function same inputs के लिए हमेशा same output produce करता है, जबकि एक non-deterministic वाला — जैसे current time पढ़ने वाला — identical arguments के साथ भी हर call पर अलग results return कर सकता है।

उदाहरण: Deterministic and Non-Deterministic

sql
DELIMITER //
CREATE FUNCTION CurrentYear() RETURNS INT NOT DETERMINISTIC
BEGIN
  RETURN YEAR(NOW());
END //
DELIMITER ;
SELECT CurrentYear();

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. RETURNS भूल जाना, या function body के अंदर एक RETURN statement।
  2. DETERMINISTIC (या READS SQL DATA) छोड़ देना, जो binary logging enabled होने पर एक error का कारण बनता है।
  3. किसी function के लिए CALL इस्तेमाल करना, जबकि functions SELECT FullName(...) जैसे expressions के अंदर इस्तेमाल होते हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.