Slide 1: Introduction to Functions
- Functions are blocks of code designed to perform a specific task.
- They help in code reusability and better organization.
Slide 2: Syntax of a Function
- Use the 'def' keyword to define a function.
- Syntax: def function_name(parameters):
- # block of code
- return result (optional)
Slide 3: Types of Functions
- 1. Built-in Functions (e.g., print(), len(), type())
- 2. User-defined Functions (defined using def)
Slide 4: Function Example
- def greet(name):
- print('Hello,', name)
- greet('Alice') # Output: Hello, Alice
Slide 5: Arguments and Parameters
- Arguments: Values passed to a function.
- Parameters: Variables in function definition.
- Types: Positional, Keyword, Default, Variable-length (*args, **kwargs)
Slide 6: Return Statement
- Functions can return values using the 'return' keyword.
- Example:
- def add(a, b):
- return a + b
Slide 7: Recursion
- A function calling itself is called recursion.
- Example:
- def factorial(n):
- if n == 1: return 1
- return n * factorial(n-1)