Skip to main content

Functions in Python with multiple examples and call function from multiple function

 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.

Comments

Popular posts from this blog

Gujarati Keyboard layout (terafont-varun), Computer Short cut key, Tally short cut key

Word , Excel , Power Point Shortcut Key in Gujarati

Terafont-Varun (Gujarati Typing) Keyboard Layout by "Sama Soyab"

  For Gujarati Typing : Required : Terafont-Varun Font  After Successfully Installed Terafont Varun Open Any Text Editor or any program. Select Font Terafont-Varun -> Ok For more detail please watch below video. Search Topics : Learn terafont varun, Learn terafont chandan, Learn terafont gujarati to english translation, Learn terafont varun keyboard, Learn terafont converter, Learn terafont varun zip, Learn terafont keyboard, Learn terafont kinnari, Learn terafont akash, Learn terafont aakash, Learn terafont akash ttf, Learn terafont aakash gujarati download, Learn terafont akash keyboard, Learn terafont akash download for windows 10, Learn terafont akash font download, Learn terafont arun, Learn terafont border, Learn terafont chandan keyboard, Learn terafont-chandan font, Learn tera font chandana, Learn convert terafont to shruti, Learn convert terafont varun to shruti, Learn terafont varun chart, Learn terafont download, Learn terafont download for windows 10, Learn tera...