OneBite.Dev - Coding blog in a bite size

declare a function with return value in python

Code snippet on how to declare a function with return value in python

  def my_function(arg1, arg2):
    # Add both the parameters and return them."
    total = arg1 + arg2
    
    # Returning the total value
    return total

This code is declaring a function named “my_function”, which takes two arguments, “arg1” and “arg2”. The function adds both arguments together and returns the result as “total”. Finally, the function returns the value of “total”. To call the function, you would simply specify the arguments and use the return keyword to store the value of the function in a variable. For example: “my_total = my_function(1,2)”. This code would assign the value of “3” to the variable “my_total”.

python