Python Function Overview

Programming domain me function ek important role play karta hai. Function codes ko properly optimize karta hai aur program ke execution aur sath hi process ko fast bhi banata hai. Function ek block of code hota hai – means ek particular function ko define karke usme use hone wale codes ko likha jata hai.

Function na sirf locally accessible hota hai balki yah globally bhi accessible hota hai. Local means – project ki wah file jaha hum coding kar rahen hain lekin Global means –  defined function ko project ke dusre file me bhi call kiya ja sakta hai.

Python me Function ko create kaise karte hain?

Python me function ko “def” keyword ka use karke kiya jata hai. 

def my_foo():
    print(“This is my_foo function”)

Python me Function ko call kaise karte hain?

Function ko define karne ke baad usse call tabhi access kar sakte hain jab usse call kiya jata hai. Example me my_foo naam ka function hai

def my_foo():
    print(“This is my_foo function”)

my_foo()

Python Function me Argument aur parameter kya hota hai?

Koi bhi information jise function me call ke dauran pass kiya jata hai use Argument kahte hai. Dekha jaye to ek function ko def keyword ke sath define kiya jata hai, jaise def my_foo() , iss function me parameters ko parenthesis ke andar define kiya jata hai.

Example se samajhte hain:

def my_foo(para1):
    print(arg1 + ” tech”)

my_foo(“vrim”)
my_foo(“esco”)
my_foo(“addo”)

Diye gaye example me para1 ek parameter hai. Aap apne program ke hisab se jitne chahe utne parameters define kar sakte hain. Function ko call karte time para1 ki value change hoti rahegi means first call par para1 = vrim, second call par para1 = esco aur third call par para1 = addo. Example me “vrim”, “esco”,”addo” arguments hain. Important baat yah hai ki arguments ki sankhya(number) parameters ke barabar honi chahiye.

Python Function me default parameter kya hota hai?

Jaisa ki hum jante hain ki function me parameters aur arguments ke number same hone chahiye, agar aisa nhi hua to program error generate karega. 

Kabhi-kabhi hum program ke kuch limited jagah par changes chahte hain aur maximum content ko same rakhna chahte hain ya fir by mistake hum function call ke time argument pass karna bhul jate hain, tab aise case me hum default parameter ka use karte hain. Example se samajhte hain , jaha country=”India” ek default parameter hai.

def my_foo(country = “India”):
    print(“I am from “ + country)

my_foo(“USA”)
my_foo(“UK”)
my_foo()
my_foo(“Russia”)

Arbitrary Argument (*args) kya hota hai?

Arbitrary arguments ka use tab kiya jata hai jab hum sure nhi hote ki kitne argument pass karne hain. Arbitrary Argument ko define karne ke liye * symbol ko parameter name ke front me add kiya jata hai.

def my_foo(*name):
    print(“My name is “ + name[1])

my_foo(“Amar”“Akbar”“Anthony”)

Jyadatar aisi condition run time execution me aati hai means jab large set of codes run condition me arguments ke numbers me changes karte hain, tab Arbitrary argument use karna ek best option hota hai. 

Keyword Argument kya hota hai?

Python function me keyword argument means key aur value ke syntax ko follow karna, means argument ko key=value ke pair me kisi function me pass karna. Example se samajhte hain:

def my_foo(city2, city1, city3):
    print(“The smallest city is “ + city2)

my_foo(city1 = “bhopal”, city2 = “goa”, city3 = “indore”)

Arbitrary Keword Arguments (**kwargs) kya hota hai?

Arbitrary keyword arguments ka use tab kiya jata hai jab hum sure nhi hote ki kitne keyword arguments pass karne hain. Arbitrary Keyword Arguments ko define karne ke liye ** symbol ko parameter name ke front me add kiya jata hai.

def my_foo(**city):
    print(“The smallest city is “ + city[“city2”])

my_foo(city1 = “bhopal”, city2 = “goa”, city3 = “indore”)

List, Dictionary, Tuple, String, Set ko argument ki tarah pass karna

#Tuple as Argument 
def my_foo(country):
    for y in country:
        print(y)
countries = (“india”,”USA”,”UK”,”Russia”)        #TUPLE
my_foo(countries)

#Set as Argument 
def my_foo(country):
    for y in country:
        print(y)
countries = {“india”,”USA”,”UK”,”Russia”}           #SET
my_foo(countries)

#List as Argument 
def my_foo(country):
    for y in country:
        print(y)
countries = [“india”,”USA”,”UK”,”Russia”]           #LIST
my_foo(countries)

#Dictionary as Argument 
def my_foo(x):
    for key in x:
        print(“CITY: “,key,”STATE: “,x[key])
details={“Indore”:”MP”,”Varanasi”:”UP”,”Bangalore”:”KA”}
my_foo(details)

#String as Argument 
def my_foo(country_):
    for y in country_:
        print(y)
country = “INDIA”                                           #STRING
my_foo(country)

Python Function ka Return Value

Return value ka funda starting me confuse karta hai hai , lekin agar return value ka concept ache se dekha jaye to ek function tabhi value return karta hai jab uss function ko call karte samay uski value to kisi third variable me store karna hota hai ya fir kuch calculation karna hota hai.
Example se samajhte hain:

def cal(y):
    return 10 * y

a= cal(2)
b=cal(3)
c=cal(4)

d = a+b+c
print(d)

Python Function me Pass Statement

Agar aapne kisi function ko define kar diya aur chahte hain ki filhal aage ki coding ko continue karte hain uske baad function ki coding ko finally set karenge,  tab aapko chahiye ki function ke andar pass argument jarur pass kar de jiski wajah se program error free rahega.

def foo():
    pass

Python Function me Recursion ka concept

Recursion thoda confuse karne wala topic hai, lekin agar aane ise ek baar samajh liya to aap iske concept ko kabhi bhul nhi payenge. Recursion aisa concept hai jaha function apne aap ko condition ke according call karta hai.

Detail me samajhte hain: (CLICK HERE)