To create a function in Python, you can use the def
keyword followed by the function name and parentheses ()
that may include input parameters. The code block within the function should be indented.
Here is an example of a simple function that takes in two variables and returns their sum:
main.py68 chars4 lines
In this example, the def
keyword is used to define the add_numbers()
function. It accepts two input parameters num1
and num2
, calculates their sum, and returns the result.
You can call this function by passing in desired values as input parameters:
main.py53 chars3 lines
In this example, the add_numbers()
function is called with values 2
and 3
and the returned result is stored in the result
variable. The output of the function is printed to the console, which will display 5
.
gistlibby LogSnag