Function parameters and return values in python

Function parameters and return values in python

In Python function program declare parameter/argument variables as inputs to process and manipulate them, and user defined function values ​​can return variable values ​​as program output.

Function parameters and return values in python

So let’s go through the Python function program to understand how Python functions work, and how functions return argument values.

Python Function Parameters/Arguments.

Python function parameters/arguments are program variables listed inside brackets in the function definition. They are storage placeholders for the program values ​​that you provide when calling the declare program function. Here program function variables are parameters/arguments, you can declare them if you want or else the function can have zero or more parameter/argument variables declared.

python function parameter example.

def welcome(info):

“”use welcome function to display function information”””

print(f”Hi, {info}”)

# let call welcome function to display information

welcome(“vcanhelpsu”) # the result is – Hi, vcanhelpsu

In the above function example, info is a parameter/argument variable of welcome function. When we call welcome(“vcanhelpsu”), here “vcanhelpsu” is called as info parameter/argument.

Python Function Return Values.

Python function programs often return values ​​passed to the function or called functions using the user return function syntax. If a function in the current program does not explicitly return any value, then these functions return None function values ​​by default.

Python Function Return Example.

def sub_integer(p, q):

“”here the sub_integer function declare to subtract two integers”””

return p – q

output = sub_integer(10, 4)

print(output) # the reuslt is – 6

In the above subtract_integer function, the sub_integer function takes two program variable parameters/arguments p and q as input values. They are subtracted using the p – q parameters, and the result is displayed using p – q in the return value.

Python functions with multiple return values.

Python functions can return multiple values ​​as multiples or other iterable types, which can be unpacked when calling the function.

Python functions with multiple return examples.

def sub_integer(p, q):

    “””here the sub_integer function declare to subtract two integer”””

    return p – q

output = sub_integer(10, 4)

print(output)  # the reuslt is – 6

def add_integer(p, q):

    “””here the add_integer function declare to add two integer”””

    return p + q

output1 = add_integer(10, 4)

print(output1)  # the reuslt is – 14

def mul_integer(p, q):

    “””here the mul_integer function declare to multiply two integer”””

    return p * q

output2 = mul_integer(10, 4)

print(output2)  # the reuslt is – 40

In the multiply function return example above, sub_integer, Counts the p and q function parameter argument values ​​from add_integer, mul_integer function, and returns them as a single value. Prints the function argument values ​​by calling add, subtract, add, multiply, etc.

Function default parameter values.

In any Python function program, you can define default values ​​for parameters/arguments. If a default value is provided to a parameter in a function program, then while calling the existing function program value becomes optional.

Function default parameter values ​​​​example.

def welcome(info=”vcanhelpsu”):

“”here welcome function declare with info parameter text”””

print(f”Hi, {info}”)

# let call function with default parameter

welcome(“david”) # the result is – Hi, david

welcome(“mathew”) # the result is – Hi, mathew

welcome() # the result is – Hi, vcanhelpsu

In the above program example, the default value of the info parameter/argument variable is the “welcome” function. If no arguments are given while calling welcome(), it returns the default function values.

Python variable-length arguments.

Python function programs can accept argument variables in variable numbers using *args (for positional arguments) and **kwargs (for keyword arguments).

Python variable-length arguments example.

def addtion_num(*args):

“””here the addtion_num parameter used to add all number argument”””

result = 0

for p in args:

result += p

return result

output = addtion_num(9, 8, 16, 1, 11, 17, 19)

print(output) # the result is – 81

In the above function program, the addition_all function accepts any number of positional argument parameters (*args), and returns the total value of all those integer numbers.

Python Function Parameter Argument Information.

Understanding how to effectively define Python program function parameter/argument variables and function return values ​​allows you to write flexible and reusable function module source code in Python programming.