DEV Community

Narasimma Radhakrishnan
Narasimma Radhakrishnan

Posted on

Python Functions??

Functions are one of the most important concepts in Python programming. They help developers write reusable, organized, readable, and maintainable code.

When developing applications, we often need to perform the same operation multiple times. Instead of writing the same code repeatedly, we can create a function and call it whenever required.

Example Without Function

print("Welcome to Python")
print("Welcome to Python")
print("Welcome to Python")
Enter fullscreen mode Exit fullscreen mode
Output:

Welcome to Python
Welcome to Python
Welcome to Python
Enter fullscreen mode Exit fullscreen mode

Here, we are repeating the same statement three times.

Example With Function

def welcome():
    print("Welcome to Python")

welcome()
welcome()
welcome()
Enter fullscreen mode Exit fullscreen mode
Output:

Welcome to Python
Welcome to Python
Welcome to Python
Enter fullscreen mode Exit fullscreen mode

Here, we created a function called welcome() and reused it three times.

Syntax of a Function

def function_name():
    # Statements
    # Function logic

function_name()
Enter fullscreen mode Exit fullscreen mode

def – A keyword used to define a function.

function_name – The name given to the function.

() – Parentheses used to pass parameters.

: – Indicates the beginning of the function body.

Indentation – Defines the statements inside the function.

Function call – Executes the function.

Top comments (0)