Python Fundamentals: Functions
Functions in Python allow you to organise and reuse code. They can take in parameters and return results, making code more modular and easier to manage.
a. Defining a Function: Use the 'def' keyword to define a function.
b. Function with Parameters: Functions can accept parameters to process.
c. Returning Values: Functions can return values using the 'return' statement.
a. Defining a Function: Use the 'def' keyword to define a function.
Code
def greet(): print("Hello, World!") greet()
Output
Hello, World!
b. Function with Parameters: Functions can accept parameters to process.
Code
def greet(name): print(f"Hello, {name}!") greet("Alice")
Output
Hello, Alice!
c. Returning Values: Functions can return values using the 'return' statement.
Code
def add(a, b): return a + b result = add(5, 3) print(result)
Output
8