SoFunction
Updated on 2025-03-02

Summary of 9 major methods for function calls in Python

In Python, functions are a very important programming concept. They make the code modular, reusable, and can improve the readability of the code. This article will explore in-depth nine methods of Python function calls, including ordinary functions, anonymous functions, recursive functions, advanced functions, etc., as well as their application examples.

Method 1: Normal Function

Ordinary functions are the most basic function types in Python. They are defined by the def keyword and can take parameters and return values.

def add(a, b):
    return a + b

result = add(3, 4)
print(result)  # Output 7

Method 2: Anonymous function (Lambda function)

Anonymous functions, also known as Lambda functions, are short, temporary functions that are often used for simple operations. Lambda functions are defined using the lambda keyword and have no function names. They can be used for some operations in functional programming.

multiply = lambda x, y: x * y
result = multiply(3, 4)
print(result)  # Output 12

Method 3: Recursive Function

A recursive function is a function that calls itself. Recursion is often used in solving problems, such as calculating Fibonacci sequences.

def fibonacci(n):
    if n <= 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)

result = fibonacci(5)
print(result)  # Output 5

Method 4: Advanced Functions

A higher-order function refers to a function that can accept a function as a parameter or return a function as a result. They can be used for combinations, transformations, and abstractions of functions.

def apply(func, x):
    return func(x)

square = lambda x: x**2
result = apply(square, 5)
print(result)  # Output 25

Method 5: Closure

A closure is a function object that contains its own code and environment. This allows it to access variables of external functions, even if the external function has been executed.

def outer_function(x):
    def inner_function(y):
        return x + y
    return inner_function

closure = outer_function(5)
result = closure(3)
print(result)  # Output 8

Method 6: Generator Function

A generator function is a special function that uses the yield keyword to generate an iterator that can produce values ​​one by one, rather than generating all values ​​at once.

def countdown(n):
    while n > 0:
        yield n
        n -= 1

for i in countdown(5):
    print(i)  # Output by one 5, 4, 3, 2, 1

Method 7: Decorator Function

A decorator function is a function used to modify the behavior of other functions. They are often used to add additional features such as performance analysis, logging, permission checking, etc.

def logger(func):
    def wrapper(*args, **kwargs):
        print(f"Calling function {func.__name__}")
        result = func(*args, **kwargs)
        print(f"{func.__name__} returned {result}")
        return result
    return wrapper

@logger
def add(a, b):
    return a + b

result = add(3, 4)

Method 8: Inline Functions

An inline function is a method that reduces the overhead of function calling by inserting the contents of a function into the call. In Python, the way in which inline functions can be used is to use inline caching or use JIT (instant compilation) techniques.

from numba import jit

@jit
def add(a, b):
    return a + b

result = add(3, 4)

Method 9: Some functions application

Partial function application is a method of fixing part of the function's parameters and returning a new function. This is very useful in functional programming.

from functools import partial

def power(base, exponent):
    return base**exponent

square = partial(power, exponent=2)
cube = partial(power, exponent=3)

result1 = square(4)
result2 = cube(4)

These 9 methods cover all aspects of function calls in Python, from basic function definitions to advanced functional programming techniques. Understanding and mastering these methods can help everyone better organize and optimize code and improve development efficiency. Both beginners and experienced developers should be familiar with these methods in order to choose the right way to define and call functions under different circumstances.

This is the end of this article about the summary of 9 major methods of function calls in Python. For more related content of Python function calls, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!