Functions in Python are blocks of organized, reusable code designed to perform a specific task. They help modularize code, improve readability, and make it easier to manage and reuse code. Here are some examples of defining functions and calling functions from other functions:
1. Simple function:
A basic function that takes two parameters and returns their sum.
```python
def add_numbers(a, b):
result = a + b
return result
# Calling the function
sum_result = add_numbers(5, 3)
print(sum_result) # Output: 8
```
2. Function with default parameter:
A function with a default parameter value. If the second parameter is not provided, it will use the default value.
```python
def greet(name, greeting="Hello"):
message = f"{greeting}, {name}!"
return message
# Calling the function without the second parameter
greeting_message = greet("John")
print(greeting_message) # Output: Hello, John!
# Calling the function with the second parameter
greeting_message = greet("Alice", "Hi")
print(greeting_message) # Output: Hi, Alice!
```
3. Function with multiple return values:
A function that returns multiple values using tuple packing and unpacking.
```python
def get_name_and_age():
name = "John"
age = 30
return name, age
# Calling the function and unpacking the returned values
person_name, person_age = get_name_and_age()
print(person_name) # Output: John
print(person_age) # Output: 30
```
4. Function calling another function:
A function can call another function within its block.
```python
def multiply_numbers(a, b):
return a * b
def perform_calculation(x, y):
result = add_numbers(x, y)
result *= multiply_numbers(x, y)
return result
# Calling the perform_calculation function
calc_result = perform_calculation(3, 4)
print(calc_result) # Output: 84 (3 + 4) * (3 * 4)
```
5. Recursive function:
A function that calls itself to perform a repetitive task.
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Calling the factorial function
result = factorial(5)
print(result) # Output: 120 (5! = 5 * 4 * 3 * 2 * 1)
```
6. Function as an argument to another function:
Functions can also be passed as arguments to other functions.
```python
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def calculate(operation, x, y):
return operation(x, y)
# Calling the calculate function with add and subtract functions as arguments
result1 = calculate(add, 5, 3)
result2 = calculate(subtract, 5, 3)
print(result1) # Output: 8 (addition)
print(result2) # Output: 2 (subtraction)
```
These examples demonstrate various aspects of functions in Python, including defining functions, using default parameters, returning values, calling functions from other functions, recursion, and using functions as arguments to other functions. Functions are a powerful feature in Python that helps to structure and organize code for efficient and reusable programming.
This blog is mainly helpful to those who are preparing for government exam and for those who get admission in B.com or M.com. Information of all government jobs, daily news papers, competitive exam materials, study videos, government GR, syllabus of B.Com & M.Com, paper bank of Kutch university, paper bank and assignments of IGNOU & BAOU, Gujarati vyakran tutorial, computer shortcut keys, scholarships details.
Comments
Post a Comment