Function reusable piece of code या block of code होता है जो कि कोई specific task perform करता है। एक बार define करने के बाद हम इन्हें कितनी ही बार use कर सकते हैं। Python में कई predefined useful function हैं जिन्हें हम Built In Functions कहते हैं।

Python Function Advantages

  1. Code Re-usability : functions use करने का सबसे बड़ा advantage यही है कि , हम code को reuse कर सकते हैं। same processing के लिए एक बार function define करने के बाद उसे हम कही भी और कितनी बार भी use कर सकते हैं।

  2. Less Code : चूंकि हम same code के लिए functions use करते हैं जिससे Program की length कम जाती है।

  3. Reduce Coding Time : Function use करने से coding time reduce होता है , जो कि किसी भी developer के लिए important है।

  4. Easy To Understand : Code को समझना आसान हो जाता है।

  5. Easy To Maintain : समय के साथ - साथ हमें code update करना पड़ जाता है, जिसे हम function की help से आसानी से maintain कर सकते हैं।


Python में function को थोड़ा different तरीके से define किया जाता है। PHP, JavaScript, Java, C etc.. में functions को function keyword का use करके define करते थे, लेकिन Python में ऐसा नहीं है।


Python में functions को def keyword का use करके define किया जाता है। और function की body को colon : के बाद start किये जाता है।

Python Function Syntax

def functionName(): 
   #do whatever you want.
   return something. #it's optional.

Explain :

  1. functionName किसी alphabetic letter या underscore से ही start होना चाहिए। और कोई special character like : !, @, # allow नहीं है। हाँ आप integer numbers use कर सकते हैं function name के बीच में या last में।

  2. function में कम से कम एक statement तो होना ही चाहिए , function without body allow नहीं है।

  3. कोई function कुछ return करे ये जरूरी नहीं है। return statement optional होता है।

Note : Function body में present सभी statements एक particular space के बाद same indentation में चाहिए। आप statements को आगे पीछे नहीं लिखा सकते हैं।

Python Function Example

Copy Fullscreen Close Fullscreen Run
#define function with name : sayhi.
def sayhi() :
   print('Hi !')

#now it's time to call.
sayhi()
Output
C:\Users\Rahulkumar\Desktop\python>python function_ex.py
Hi !

Python Function return

Copy Fullscreen Close Fullscreen Run
def sayhi() :
	return 'Hi !'
    
print(sayhi())
Output
C:\Users\Rahulkumar\Desktop\python>python function_retuen.py
hi !

Python Function Types

Functions दो types के होते हैं -

  1. Predefined / Built In Functions.
  2. User Defined Functions.

Python Predefined Functions

Predefined Functions वो functions होते हैं जिन्हे define करने की जरूरत नहीं होती है , ये Python में built in होते हैं। बस simply करना होता है और अपनी definition के according Output return करते हैं।


type() , int(), float(), bool() , list(), tuple(), set(), etc... ये सभी predefined functions ही हैं।

Python User Defined Functions

Predefined Functions वो functions होते हैं जिन्हे user को define करना होता है। इनके name कुछ भी रख सकते हैं ये user पर depend करता है।

ये भी कई तरह के होते हैं।

  1. Parameterized Function
  2. Recursive Function
  3. labmda Function

Python Functions : Important Tips

  1. बैसे तो same name के functions define करना अच्छा नहीं होता है इससे confusion होती है , लेकिन अगर आप एक ही name के कई function define करते हैं, हमेशा सबसे last में define किया गया function ही call होता है।

  2. function और method दोनों अलग अलग term हैं -


    Methods किसी Class के अंदर define किये जाते हैं इन्हे हमेशा Class Name या Class Object के साथ access किया जाता है। जबकि function out of the class define किया जाते है , ये Class / Object के context में नहीं होते इसलिए इन्हें कहीं भी access किया जा सकता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers