|
|
| Introduction | Variables | If-else |
| Strings | Functions | while-loop |
| For-Loop | List | Set |
| Dictionary | Tuple | Try..Except |
| Class/Object | Inheritance | Polymorphism |
| File Handling | ||
Functions are basically blocks of code designed to do a specific task. When you want to perform a particular task that you have defined in a function, you call the function responsible for it. If you need to perform
that task multiple times throughout your program, you don't need to type all the code for the same task again and again. Function can return certain data as well.
In Python, a function is defined by the def keyword, followed by a function name and parentheses and a colon. For example.
statements
Let's see some examples of Python functions and how they can be invoked.
Code Example 1:
#simple python function
def first_function():
print("This is my first function")
#invoke the function..
first_function()
#Output:
This is my first function
Parameters or arguments can be passed to a function. Arguments are specified inside the parentheses after the function's name. A function can have one or more parameters and each parameter is separated by a comma. Function can use those parameters to retrieve some data from the database or compute a calculation based on the input parameters.
Code Example 2: #function that takes 2 parameters def add_numbers(a,b): c=a+b return c #calling the function. result=add_numbers(5,6) Print print(result) #Output: 11 Code Example 3: #function that takes 3 parameters def multiply_numbers(a,b,c): d=a*b*c return d #invoke the function. result=multiply_numbers(5,6,2) #Print. print(result) #Output: 60
*args allows python functions to accept a variable and unspecified number of parameters that are non key-value pair. Suppose we have lots of non-keyword parameters to be passed to a function. using *args, these arguments are collected into a tuple, and we can we can loop through each one of them.
Code Example 4:
def multiply_numbers(*args):
result = 1
for x in args:
result = result * num.
return result
#invoke the function.
return_value=multiply_numbers(3, 4, 5,6)
#Output:
360
Code Example 5:
#It accepts 3 parameters
def Kids(*kids):
print("First Kid: " + kids[0])
print("Second Kid: " + kids[1])
print("Third Kid: " + kids[2])
#invoke the function
Kids("David", "John", "Mary")
#Output:
David
John
Mary
**kwargs allows python functions to accept a variable and unspecified number of parameters. Suppose we have lots of key-value parameters to be passed to a function, use the **kwargs. so, python dictionary can be passed and using **kwargs, you can access the key-value pair of the dictionary.
Code Example 6:
def Student(**param):
print("Name:", param["name"])
print("Age:", param["age"])
print("Major:", param["major"])
#invoke the function:
Student(name = "David", age = 19, major = "Finance")
#Output:
Name: David
Age: 19
Major: Finance
Both *args and **kwargs can be used in a function. Let's see an example where the student details, where keyword arguments like name, age, major are collected into dictionary and subjects are collected into tuple.
Code Example 7:
def StudentDetails(*args, **kwargs):
print("Subjects:", args) # Positional arguments
print("Student:", kwargs) # Keyword arguments
#invoke the function
StudentDetails("Physics", "Math", name="Mary", age=18)
#Output:
Subjects: ('Physics', 'Math')
Student: {'name': 'Mary', 'age': 18}