DEV Community

bhuvaneswari nandhagopal
bhuvaneswari nandhagopal

Posted on

PYTHON FUNCTION

What is function in python?
In Python, a function is a reusable block of code that performs a specific task. Functions help make programs more organized, readable, and easier to maintain.

Defining a Function
You create a function using the def keyword

def greet():
    print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

Calling a Function
greet()

Output:
Hello, World!

a) Built-in Functions:

Predefined in Python.
Examples: print(), len(), type(), max(), min(), sum().

print(len("Python"))
Enter fullscreen mode Exit fullscreen mode

output:
6

b) User-defined Functions:
Created by the programmer using the def keyword.

def greet(name):
    return f"Hello, {name}!"

print(greet("Bhuvana"))
Enter fullscreen mode Exit fullscreen mode

output
Hello, Bhuvana

Functions without Parameters:

def hello():
    print("hello")

hello()
Enter fullscreen mode Exit fullscreen mode

output
hello

Functions with Parameters:

def add(x,y):
    total = x+y
    print(total)
x=10
y=20
add(x,y)

Enter fullscreen mode Exit fullscreen mode

output
30

Positional Arguments: [TBD]

Arguments are matched by position.

def power(base, exp):
    return base ** exp
print(power(2, 3)) 
Enter fullscreen mode Exit fullscreen mode

output
8

Keyword Arguments:

Arguments are matched by name.

print(power(exp=3, base=2))  
Enter fullscreen mode Exit fullscreen mode

output
8

Default Arguments:

Parameters have default values.

def greet(name="Guest"):
    print(f"Hello, {name}!")
greet()
Enter fullscreen mode Exit fullscreen mode

output
Hello, Guest!

Variable-length Arguments:

*args → Non-keyword variable arguments (tuple)
**kwargs → Keyword variable arguments (dict)

def show_args(*args, **kwargs):
    print(args)
    print(kwargs)

show_args(1, 2, 3, name="Alice", age=25)
Enter fullscreen mode Exit fullscreen mode

output
(1, 2, 3)
{'name': 'Alice', 'age': 25}

Functions that Return a Value

def square(x):
    return x * x
Enter fullscreen mode Exit fullscreen mode

Explanation

def → used to create a function
square → function name
x → input value
x * x → multiply the number by itself
return → gives back the result

Functions that Do Not Return a Value

def display(msg):
    print(msg)
display("hello")
Enter fullscreen mode Exit fullscreen mode

output
hello

Lambda Functions (Anonymous functions)

square = lambda x: x * x
print(square(5))
Enter fullscreen mode Exit fullscreen mode

output
25

Recursive Functions

A function that calls itself.

def factorial(n):
    return 1 if n == 0 else n * factorial(n - 1)
print(factorial(5))
Enter fullscreen mode Exit fullscreen mode

This program finds the factorial of 5.

What is factorial?

multiply numbers from 5 to 1

5 × 4 × 3 × 2 × 1 = 120

output
120

Reference:

https://www.bing.com/search?q=types+of+functions+in+python&gs_lcrp=EgRlZGdlKgcIBBBFGMIDMgcIABBFGLABMgcIARBFGMIDMgcIAhBFGMIDMgcIAxBFGMIDMgcIBBBFGMIDMgcIBRBFGMIDMgcIBhBFGMIDMgcIBxBFGMID0gEKNTIwNjI1ajBqN6gCCLACAQ&FORM=ANNTA1&PC=U531

Top comments (0)