Python Function

Python Function

A function is a set of statements that take inputs, do some specific task and produces output. The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function. The main purpose of function is code reusability.

एक फंक्शन स्टेटमेंट है जो इनपुट्स लेता है, कुछ विशिष्ट कार्य करता है और आउटपुट उत्पन्न करता है। विचार यह है कि कुछ सामान्य या बार-बार किए गए कार्य को एक साथ रखा जाए और एक फंक्शन बनाया जाए, ताकि विभिन्न इनपुट्स के लिए बार-बार एक ही कोड लिखने के बजाय, हम फंक्शन को कॉल कर सकें। फंक्शन का मुख्य उद्देश्य कोड पुनः प्रयोज्य है।

Note: In other languages functions are known as methods, procedures, subroutines etc.

अन्य भाषाओं में कार्यों को विधियों, प्रक्रियाओं, उप-रेखाओं आदि के रूप में जाना जाता है,

Advantages of Python functions
पायथन फंक्शन के लाभ

  • By using functions, we can avoid rewriting same code again and again in a program

फंक्शंस का उपयोग करके, हम एक प्रोग्राम में एक ही कोड को बार-बार लिखने से बच सकते हैं।

  • We can call functions any number of times in a program and from any place in a program.

हम किसी प्रोग्राम में किसी भी जगह और किसी भी जगह पर कई बार फंक्शन कॉल कर सकते हैं।

  • We can track a large python program easily when it is divided into multiple functions.

हम एक बड़े पायथन प्रोग्राम को आसानी से ट्रैक कर सकते हैं जब इसे कई फंक्शंस में विभाजित किया जाता है।

  • Reusability is the main achievement of functions.

पुनः प्रयोज्य फंक्शंस की मुख्य उपलब्धि है।

  • However, Function calling is always overhead in a program.

हालाँकि, किसी प्रोग्राम में फंक्शन कॉलिंग हमेशा ओवरहेड होती है।

Python supports two types of functions:
पायथन दो प्रकार के कार्यों का समर्थन करता है:

1. Built-in functions: The functions which are coming with Python software automatically are called built-in functions.

बिल्ट-इन फंक्शनः पायथन सॉफ्टवेयर के साथ स्वचालित रूप आने वाले कार्यों को बिल्ट-इन फंक्शन कहा जाता है।

Examples: id(), type(), input(), eval() etc.,

2. User defined functions: the functions which are developed by programmers explicitly according to business requirement are called user defined functions.

यूजर डिफाईन फंक्शन: वे कार्य जो प्रोग्रामर द्वारा व्यावसायिक आवश्यकता के अनुसार स्पष्ट रूप से विकसित किए जाते हैं, यूजर डिफाईन फंक्शन कहलाते हैं।

Note: while creating functions we can use two keywords i.e., def (mandatory) and return (optional)

फंक्शंस बनाते समय हम दो कीवर्ड का उपयोग कर सकते हैं अर्थात्, def (अनिवार्य) और return (वैकल्पिक)

Creating a function
एक फंक्शन बनाना

These are the basic steps in creating user-defined functions in Python:

पायथन में यूजर-डिफाईन फंक्शन बनाने में ये मूल स्टेप हैं:

Step 1: Declare the function with the keyword def followed by the function name.

फंक्शन नाम के बाद कीवर्ड def के साथ फंक्शन को डिक्लेयर करें।

Step 2: Write the arguments inside the opening and closing parentheses of the function, and end the declaration with a colon.

फंक्शन के ओपनिंग और क्लोजिंग कोष्ठक के अंदर तर्क लिखें, और एक कोलन के साथ डिक्लरेशन को समाप्त करें।

Step 3: Add the program statements to be executed.

निष्पादित करने के लिए प्रोग्राम स्टेटमेंट जोडें।

Step 4: End the function with/without return statement.

फंक्शन को समाप्ति विवरण के साथ/बिना समाप्त करें।

The function block is started with the colon (.) and all the same level block statements remain at the same indentation.

फंक्शन ब्लॉक को कोलन() के साथ शुरू किया जाता है और सभी समान स्तर के ब्लॉक स्टेटमेंट एक ही इंडेंटेशन पर बने रहते हैं।

Calling a function ()
कॉलिंग ए फंक्शन()

In python, a function must be defined before the function calling otherwise the python interpreter gives an error. Once the function is defined, we can call it from another function or the python prompt. To call the function, use the function name followed by the parentheses.

पायथन में, फंक्शन को कालिंग करने से पहले परिभाषित किया जाना चाहिए अन्यथा पायथन इंटरप्रेटर एक त्रुटि देता है। एक बार फंक्शन परिभाषित होने के बाद, हम इसे किसी अन्य फंक्शन या पायथन प्रॉम्प्ट से कॉल करसकते हैं । फंक्शन को कॉल करने के लिए कोष्ठक के बाद फंक्शन नाम को उपयोग करें

Example 1:

def wishing ():
    print (" ................................")
    print (" Welcome to Function")
    print (" ................................")
wishing ()

Output: A simple function that prints the message is given below.
एक साधारण फंक्शन जो संदेश प्रिंट करता है, नीचे दिया गया है।
. . . . . . . . . .
Welcome to Function
. . . . . . . . . .
Example 2: A simple Python function to check whether x is even or odd.
एक साधारण पायथन फंक्शन यह जाँचने के लिए है कि क्या x सम या विषम है।

def is_even ():
    if x% 2==0: 
        print("Is even number") 
    else:
        print("Is not even number")
x=int (input ("Enter a number ")) 
is_even ()

Output:
Enter a number 3
Is not even number
Enter a number 2
Is even number

Function parameters
फंक्शन पैरामीटर्स

The information into the functions can be passed as the parameters. Parameters are inputs to the functions. The parameters are specified in the parentheses. We can give any number of parameters, but we have to separate them with a comma (,).

फंक्शन्स में जानकारी को पैरामीटर के रूप में पारित किया जा सकता है। पैरामीटर फंक्शन्स के लिए इनपुट हैं। पैरामीटर कोष्ठक ) में निर्दिष्ट हैं। हम कितने भी पैरामीटर दे सकते हैं, लेकिन हमें उन्हें कामा () के साथ अलग करना होगा।

Note: नोट

1. A function can accept any number of parameters that must be the same in the definition and function calling.

एक फंक्शन किसी भी संख्या के पैरामीटर को स्वीकार कर सकता है जो परिभाषा और फंक्शन कॉलिंग में समान होना चाहिए।

2. If a function contains parameters, then at the time of calling, compulsory we should provide values otherwise we will get an error message.

सकते हैं। फंक्शन को कॉल करने के लिए, कोष्ठकों के बाद फंक्शन नाम का उपयोग करें।

यदि किसी फंक्शन में पैरामीटर हैं, तो कॉलिंग के समय, अनिवार्य हमें वैल्यू प्रदान करना चाहिए अन्यथा हमें एक एरर मैसेज मिलेगा।

3. If the parameters passed to a function are less than that it is specified to accept, then an error will be returned.

यदि किसी फंक्शन को दिए गए पैरामीटर इससे कम हैं कि वह स्वीकार करने के लिए निर्दिष्ट है, तो एक एरर वापस आ जाएगी।

Example 1: A function that accepts a string as the parameter and prints it.
एक फंक्शन जो पैरामीटर के रूप में एक स्ट्रिंग को स्वीकार करता है और इसे प्रिंट करता है।

def fun (name):
    print("Hello", name)
fun (input ("Enter your name: "))

Output:
Enter your name : Gyancs
Hello Gyancs

Example 2: Findout passing argument is an odd number or even number
फाइंडआउट पासिंग तर्क एक विषम संख्या या सम संख्या है

def funEO (x) :
    if (x% 2 == 0):
        print (x, " is even number") 
    else:
        print (x, " is odd number ")
# calling function 
funEO (1) 
funEO (2) 
funEO (3)

Output:
1 is odd number
2 is even number
3 is odd number

Example 3: If the parameters passed to a function are less than that it is specified to accept, and then an error will be returned.
यदि किसी फंक्शन को दिए गए पैरामीटर उस से कम हैं, जिसे स्वीकार करना निर्दिष्ट है, और फिर एक एरर वापस आ जाएगी।

def fun(a, b):
    print (" a value is :", a) 
    print (" b value is :", b)
    print (" End function ") 
#calling fun () 
    fun(10) 
# TypeError: fun () missing 1 required positional argument: 'b'
fun(10, 20) 
fun(10, 20, 30)
# TypeError: fun() take's 2 positional arguments but 3 were given 

Output:
a value is : 10
b value is : 20
End function

Example 4: If the passing parameters type mismatch, and then an error will be returned.
यदि पासिंग पैरामीटर मिसमैच टाइप करता है, और फिर एक एरर वापस आ जाएगी।

def fun (name, eng, hin, sci):
    print (" Name of the student :", name) 
    print(" English Marks :", eng)
    print(" Hindi Marks :", hin) 
    print(" Science Marks :", sci) 
    print(" Total Marks :", eng+hin+sci)
#calling function () 
fun ("Gyancs", 89, 90, "fifty")

Output:
Name of the student : Gyancs
English Marks : 89
Hindi Marks : 90
Science Marks : fifty
Traceback (most recent call last):
  File “C:/Users/HP/Desktop/1.py”, line 8, in <module>
    fun (“Gyancs”, 89, 90, “fifty”)
  File “C:/Users/HP/Desktop/1.py”, line 6, in fun
    print(” Total Marks :”, eng+hin+sci)
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

Example 5:Using functions design a small calculator program (addition, subtraction, multiplication and division).
फंक्शंस का उपयोग करके एक छोटा कैलकुलेटर प्रोग्राम डिजाइन किया जाता है (जोड़, घटाव, गुणा और भाग)

def add (a, b):
    print (" Addition of two values : ", a+b)
def sub (a, b): 
    print (" Subtraction of two values : ", a-b)
def mul (a, b) :
    print (" Multiplication of two values : ", a*b) 
def div(a, b):
    print (" Division of two values" , a/b)
x = int (input ("Enter x value : ")) 
y = int(input ("Enter y value : ")) 
add (x, y) 
sub (x, y)
mul(x, y) 
div (x, y)

Output:
Enter x value : 9
Enter y value : 7
 Addition of two values :  16
 Subtraction of two values :  2
 Multiplication of two values :  63
 Division of two values 1.2857142857142858

Chapter wise Model Paper Link

About Me