User defined function: defining, parameters, default values, return value

Functions पुन: उपयोगी mini-machines हैं जो input parameters लेते हैं, values ग़ायब होने पर default settings लागू करते हैं, और एक साफ़ return result देते हैं।

11 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

दोहराए Code का दुःस्वप्न

आपके Semester 1 BCA104 C labs में, अगर आपको एक percentage calculate करना या एक currency symbol format करना पाँच अलग बार चाहिए, आपको बिल्कुल वही math lines फिर से लिखनी या copy paste करनी पड़तीं। अगर आप एक छोटी formula ग़लती करते, आपको पाँचों जगहों को ठीक करने के लिए अपने पूरे main block में खोजना पड़ता। हम अपनी calculation को एक बार एक custom, named package में package करके और नए numbers के साथ जब चाहें call क्यों नहीं कर सकते?

Theory

Juicer Machine

एक function को अपनी kitchen में एक electronic juicer machine की तरह सोचिए। machine ख़ुद अपने design (code block) से परिभाषित है। जो fruits आप ऊपर के slot में डालते हैं वे input parameters हैं। speed knob setting जो medium पर default होती है जब तक आप इसे हाथ से न घुमाएँ एक default parameter value की तरह काम करती है। आख़िर में, spout से आपके glass में गिरता ताज़ा juice returned value है।

Theory

एक Function की संरचना

एक user defined function statements का एक named, पुन: उपयोगी block है जो एक ख़ास computation करता है। आप इसे def keyword इस्तेमाल करके परिभाषित करते हैं, उसके बाद function नाम और optional parameters रखते parentheses। C के उलट, Python inputs के लिए dynamic typing इस्तेमाल करता है, यानी आप कभी argument data types declare नहीं करते। code block केवल तब चलता है जब call किया जाए। यह return statement इस्तेमाल करके main program को एक अंतिम value वापस return कर सकता है।

At a glance

Table 1: C और Python function mechanics के बीच संरचनात्मक अंतर।

Component SyntaxBCA104 C Lang नियमBCA204 Python नियम
Declaration Keywordint या float जैसे स्पष्ट data type चाहिए।सारे functions के लिए generic def keyword इस्तेमाल करता है।
Parameter Typesarguments के अंदर हर type स्पष्ट रूप से declare करना होगा।बिना किसी datatype prefixes के केवल names सूचीबद्ध करता है।
Default ValuesStandard ANSI C default parameters की अनुमति नहीं देता।brackets के अंदर param = value जैसे assignments की अनुमति देता है।
Return Statementकेवल declared header type से मेल खाती values return कर सकता है।return keyword के ज़रिए किसी भी data object को dynamically return कर सकता है।

Theory

एक Worked Example: Automated Balance Deductions

आइए देखें कि हमारा PocketMoney application automated calculations कैसे सँभालता है। हम एक ऐसा function चाहते हैं जो हमारे balance से एक expense घटाए। हम इसे miscellaneous नामक एक default category के साथ configure करेंगे ताकि program सहजता से चले भले user एक expense description type करना भूल जाए।

Practical

PocketMoney Function Ledger Engine

def calculate_remainder(balance, spend, category="Misc"):
    print("Category processed: " + category)
    remaining = balance - spend
    return remaining

# Call 1: Providing all parameters manually
wallet1 = calculate_remainder(500, 45, "Food")
print(wallet1)

# Call 2: Skipping the optional category parameter
wallet2 = calculate_remainder(100, 20)
print(wallet2)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Twin Function Calls को ट्रेस करें

ऊपर के Python script का मन में विश्लेषण कीजिए। screen पर क्रमिक रूप से कौन सी बिल्कुल textual lines और numeric balances दिखेंगी? tap करने से पहले अपनी भविष्यवाणी का खाका बनाइए।

Show the answer

script output करेगी:

Category processed: Food

455

Category processed: Misc

80

क्यों? Call 1 में, स्पष्ट string "Food" default setting को overwrite करती है। Call 2 में, तीसरा argument छोड़ना Python को preset value "Misc" पर वापस गिरने का कारण बनता है। दोनों calculations पूरी होती हैं और अपने results को wallet1 और wallet2 update करने के लिए साफ़-सुथरे return करती हैं।

Quiz

क्या होगा अगर आप अपनी def line के अंदर एक default value वाले parameter को बिना default value वाले एक mandatory parameter से पहले position करते हैं?

  1. script सामान्य रूप से चलती है और मानती है कि ग़ायब items दाएँ से बाएँ भरे जाते हैं।
  2. Python एक तुरंत SyntaxError फेंकता है: non-default argument follows default argument।
  3. function perfectly काम करता है पर अपने-आप सारे parameters को strings में बदलता है।
  4. program एक standard C application की तरह compile होता है पर calculation चरण skip करता है।
Show the answer

Python एक तुरंत SyntaxError फेंकता है: non-default argument follows default argument।

Python को ज़रूरी है कि सारे mandatory parameters (जिनमें default values नहीं) पहले रखे जाएँ। default values वाले किसी भी optional parameters को parentheses checklist के बिल्कुल अंत में group किया जाना चाहिए। इस क्रम को मिलाना एक तुरंत syntax crash पैदा करता है।

Quiz

इस छोटी function structure का अध्ययन कीजिए: def add_bonus(balance): total = balance + 50 print(add_bonus(500)) screen पर क्या print होगा?

  1. 550
  2. SyntaxError: missing return declaration
  3. None
  4. 500
Show the answer

None

Python में, अगर एक function एक स्पष्ट return keyword से टकराए बिना अपनी statements execute करना पूरा करता है, यह अपने-आप None नामक एक ख़ास built-in object वापस भेजता है। यह एक signature university viva सवाल है!

Watch out

Classic जाल: Vanishing Variable

university laboratory exams में एक classic marks-गँवाने वाली ग़लती एक definition block के अंदर remaining = balance - spend जैसा एक function variable लिखना, और फिर अपने main script body में बाहर remaining print या inspect करने की कोशिश करना है। Python एक NameError: name remaining is not defined फेंकेगा क्योंकि एक function scope के अंदर बनाया कोई भी variable उस ख़ास functional boundary के अंदर पूरी तरह जीता और मरता है।

Theory

Functions को Semester 3 Database Modules से जोड़ना

dynamic parameters पर महारत back-end development को अत्यधिक modular बनाती है। Semester 3 (BCA303) में, जब आप Python scripts को एक SQLite database से connect करते हैं, आप SQL execution lines को def query_db(sql_string, limits = 10): जैसे single functions के अंदर encapsulate करेंगे। यह आपको सरल single line triggers इस्तेमाल करके अपने system में कहीं भी records तुरंत fetch करने देता है।

Summary

Key takeaways

  • Functions custom naming identifiers के बाद def keyword इस्तेमाल करके declare किए जाते हैं।
  • Python स्पष्ट parameters और return type declaration labels पूरी तरह छोड़ देता है।
  • Default values को सख़्ती से argument lists के दाएँ छोर पर रखा जाना चाहिए।
  • एक स्पष्ट return statement छोड़ना एक function को अपने-आप None की एक value देने पर मजबूर करता है।
  • एक function body के अंदर declare किए variables उस ख़ास block scope के local रहते हैं।
  • Memory Hook: Packages procedures रखते हैं, values सुरक्षित रूप से return करते हुए!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Fundamentals

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

User defined function: defining, parameters, default values, return value · Programming Skills · Gri-Learn