Python Tutorial : function
Python is one of the smartest programming language in modern programming world , probably Python contain the most advance libraries among the other programming language. Python is very popular for both backend , frontend and embedded software design and artificial intelligence software design. Python on does not contain only inbuild library , many third party provider several library to support Python for more advance work.
Today, in this tutorial we will learn about function in Python. First we have to learn what is function ?
A function is a block of code that have some specific task and objective , that means when we call a function ,the block code will be executed and it will return some value. The value must may be image, video , interger , string everything.
In other language , function starts and close with opening and closing braket( { }) ,but Python is very smart coding language , it handle opening and closing of the function in a smart way so that the function is compact, length is small.
Here is a smaple function
def HellowWorld():
print("Hello! Welcome to Python functions.")
def → defines the function
HellowWorld → function name
() → parentheses (for parameters)
How to Call function
Calling function in Python is very easy , just put the name with parameter.
HellowWorld()
Sigle Parameter Function
def HellowWorld(name):
print(f"Hello, {name}! Nice to meet you.")
Calling :HellowWorld("Ayan")
Output:Sum: Ayan ! Nice to meet you.
Multi Parameter Function
def add(a, b, c):
result = a + b + c
print("Sum:", result)
Calling:add(10, 20, 30)
Output:Sum: 60
Function Returns Multiple Values
Python can return multiple return values , this is simple and just like same method calling simple function.First you have to write all return variable and then call the functions .There is the simple example
def calculate(a, b):
add = a + b
sub = a - b
return add, sub
Calling :
x, y = calculate(10, 5)
print("Sum:", x)
print("Difference:", y)
Output:
Sum: 15
Difference: 5
Functions Can be called Together
Multiple function can be called together , first call the function which need to call first and then on.Here is the example of the same
def get_input():
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
return a, b
def add(a, b):
return a + b
def main():
x, y = get_input()
result = add(x, y)
print("Sum is:", result)
Calling :main()
Nested Function
Nested function meaning function under a function .Multiple nested function can be nested under a main function.We do not need to call inner function , we just need to call outer function, inner function will be called automatically.
def outer_function():
print("Outer function started.")
def inner_function():
print("Inner function executed.")
inner_function() # call inner function inside outer
Calling :outer_function()
Returning Inner Function
You can return the inner function from the outer function , this is called a closure.
def outer_function():
def inner_function():
print("Hello from inner function!")
return inner_function # returning the function, not calling it
Calling :
greet = outer_function()
greet()
Output:
Hello from inner function!
Prepared By: https://www.sonjukta.com/About.html
No comments:
Post a Comment