Function Examples
Function without arguments:
def greet():
print("Hello, welcome!")
greet()
Output:
Hello, welcome!
Function with arguments:
def greet(name):
print("Hello,", name, "welcome!")
greet("Alice")
greet("Bob")
Output:
Hello, Alice welcome!
Hello, Bob welcome!
Function with arguments:
def add_numbers(a, b):
return a + b
result = add_numbers(500, 300)
print("The sum", result)
Output:
The sum 800
Function with default argument values:
def greet(name="guest"):
print("Hello,", name, "welcome!")
greet()
greet("Alice")
Output:
Hello, guest welcome!
Hello, Alice welcome!
Function with arguments:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
result = factorial(10)
print("The factorial is", result)
Output:
The factorial is 3628800
Basic Topics
- Introduction to Python
- Basic Syntax and Data Types
- Variables and Operators
- Input
- Conditional statements
- Loops
- Functions
- List
- Tuples
- Sets
- Dictionary
- Modules
- Packages
- Exception Handling
- Read/Write Files