Home | Computer Science

  Function
In Python, a function is a block of code that performs a specific task and can be reused multiple times throughout your program. Functions help in modularizing code, making it more organized, readable,and maintainable. To define a function in Python, you use the def keyword, followed by function name and its parameters (if any). Here's the basic syntax of a Python function: def function_name(parameter1, parameter2, ...): # Function body (code block) # Statements and computations go here # You may use the parameters inside the function # Optional: Return a value using the 'return' statement return result

def say_hello(): return "Hello, World!" message = say_hello() print(message) # Output: Hello, World!

OUTPUT

  A simple function that adds two numbers and returns the result:

 

def add_numbers(a, b): return a + b result = add_numbers(5, 3) print(result) # Output: 8

OUTPUT