logo CBCE Skill INDIA

Welcome to CBCE Skill INDIA. An ISO 9001:2015 Certified Autonomous Body | Best Quality Computer and Skills Training Provider Organization. Established Under Indian Trust Act 1882, Govt. of India. Identity No. - IV-190200628, and registered under NITI Aayog Govt. of India. Identity No. - WB/2023/0344555. Also registered under Ministry of Micro, Small & Medium Enterprises - MSME (Govt. of India). Registration Number - UDYAM-WB-06-0031863

What is function in Python?


Function in Python
 

In Python, a function is a reusable block of code that performs a specific task or set of tasks. Functions help in organizing code into modular units, making it more readable, maintainable, and reusable. They play a crucial role in structuring programs and promoting the concept of code reusability.

 

Here is the basic syntax of a function in Python:

 

def function_name(parameters):
    # code block
    # ...
    return result  # optional
  • def: This keyword is used to define a function.
  • function_name: It is the identifier for the function, by which you can call and reference it.
  • parameters: These are variables that the function can accept as input (optional).
  • code block: This is the set of instructions or statements that the function executes when called.
  • return: This keyword is used to specify the value that the function should return (optional).

Here's a simple example of a function that adds two numbers:

def add_numbers(a, b):
    result = a + b
    return result

# Calling the function
sum_result = add_numbers(3, 4)
print(sum_result)  # Output: 7

In this example, add_numbers is the function name, and it takes two parameters (a and b). Inside the function, it calculates the sum of a and b and returns the result. The function is then called with arguments 3 and 4, and the result is printed.

 

Functions can have various use cases, and they are a fundamental building block in Python programming. They can be defined with or without parameters, and they can return values or perform tasks without returning anything.

 

Thank you.


Give us your feedback!

Your email address will not be published. Required fields are marked *
0 Comments Write Comment