Matma
Matma
In Python, you can use the return keyword to exit a function so it goes back to
where it was called. That is, send something out of the function.
The return statement can contain an expression to execute once the function is
called.
The example below demonstrates how the return keyword works in Python:
def multiplyNum(num1):
return num1 * 8
result = multiplyNum(8)
print(result)
# Output: 64
What’s the code above doing?
If you find this article helpful, don’t hesitate to share it with your friends and
family.
Functions are integral parts of every programming language because they help make
your code more modular and reusable.
In this article, I will show you how to define a function in Python and call it, so
you can break down the code of your Python applications into smaller chunks.
I will also show you how arguments and the return keyword works in Python
functions.
The next thing you have to do is make sure you indent with a tab or 4 spaces, and
then specify what you want the function to do for you.
def functionName():
# What to make the function do
Basic Examples of a Function in Python
Following the basic syntax above, an example of a basic Python function printing
“Hello World” to the terminal looks like this:
def myfunction():
print("Hello World")
To call this function, write the name of the function followed by parentheses:
myfunction()
Next, run your code in the terminal by typing python filename.py to show what you
want the function to do:
sss-1
def subtractNum():
print(34 - 4)
subtractNum()
# Output: 30
Arguments in Python Functions
While defining a function in Python, you can pass argument(s) into the function by
putting them inside the parenthesis.
functionName(valueForArg1, valueForArg2)
Here's an example of arguments in a Python function:
# Output: 6
In the example above: