Python Functions Tutorial

Welcome to this comprehensive tutorial on Python function, brought to you by myitschools.com. In this tutorial, we will explore various aspects of function in Python, Like their definition, usage, and practical examples. By the end of this tutorial, you will understand, how to use functions effectively in your Python programs.

Introduction to Function

Functions in Python are reusable blocks of code designed to perform a specific task. They promote modularity, reduce redundancy, and make code more readable and maintainable. Python provides built-in functions (e.g., print (), len (), etc.), and also allows users to define their own functions using the def keyword.

Why Function are Important

1. Code Reusability: Functions allow you to write a block of code once and reuse it multiple times.
2. Modularity: Functions break down complex programs into smaller, manageable units.
3. Improves Code Readability and Organization: Well-structured functions improve the readability of the code.
4. Avoiding Repetition: Without functions, repetitive tasks in a program might require copying and pasting similar code blocks.
5. Easier Debugging and Testing: Functions provide a way to isolate and test specific parts of your code.
6. Abstraction: Functions allow abstraction by hiding the complexity of operations and exposing only the necessary interface.
7. Parameterization: Functions can take parameters to make them flexible and dynamic.

Defining a Function

A function is defined using the def keyword, followed by the function name, parentheses, and a colon. The code block within the function is indented.

Syntax

def function_name(parameters):
    # block of code
    return expression
  1. Function Name: Descriptive name for the function, following Python naming conventions.
  2. Parameters: Inputs to the function (optional). Functions can take zero or more parameters.
  3. Return Statement: Specifies the value the function returns. A function without a return statement returns None by default.

Calling a function

Function Call: Once defined, a function can be called by writing its name followed by parentheses. If the function takes parameters, the arguments must be passed in the parentheses.

        <code>function_name(arguments)</code>
    Write a basic python program to call a function:-
    # In Python a function is defined using the def keyword:
    
    def my_function():
      print("Welcome to python programming language")
    
    # Calling a Function
                     # To call a function, use the function name followed by parenthesis:
    my_function()
    Write a program to find the total of the given number using function:
    #Here is first below example show that without using function:-
    # a = int(input("Enter your number: "))
    # b = int(input("Enter your number: "))
    # c = int(input("Enter your number: "))
     
    # Total= (a + b + c)
    # print(Total)
    
    #------By using python Function---------------------------------------------
    # Function Definition
    def avg():
        a = int(input("Enter your number: "))
        b = int(input("Enter your number: "))
        c = int(input("Enter your number: "))
        
        average = (a + b + c)/3
        print(average)
    
    avg() # Function Call
    print("Thank you!")
    Write a program to find the Average of the all the subject entered by user:
    def avg():
        name=input("Write your  name here :")
        maths=int(input("What is your maths marks: "))
        Eng=int(input("What is your Eng marks: "))
        Hindi=int(input("What is your Hindi marks: "))
        
        Average=(maths+Eng+Hindi)/3
        print(f"{name} your average score is {round(Average,2)}")
    
     #Function call here too indentation is too much important 
    avg()
    
    

    Scroll to Top