What are functions?
A function is a block of organized and reusable code which performs an action depending on what it receives as input. In mathematics, this is also a function. Mathematical functions create an output depending on what they receive as an input, just like how in Python functions function and create outputs depending on their input as well. Functions have to be defined using the def method.
For example:
def add(x,y):
return x + y
# x and y are the parameters and they are the data needed for the function to operate.
In order to get a value back from a function, we have to return something from that function. In this case, we are getting back x +y, so if I call this function (if I use it), I will get the value of x + y. To call a function, so to use the function, we need to simply write it and give it the necessary input, just like f(x) in mathematics.
add(5, 10)
Output:
15
Please note that returning something is not the same as printing something! Print will just show the value to the screen and will not store the value found. Return, on the other hand, will store the value of whatever is executed in the function.
Let's create a function: reversing text.
def reverse(text):
return text[::-1]
# a function which will reverse whatever iterable is entered
For example:
reverse("hello")
Output:
olleh
What is the difference between arguments and parameters?
Let's look at this with an example by creating a function called about.
def about(name, age, likes):
sentence = f"Meet {name}! They are {age} years old and they like {likes}."
return sentence
Let's call the function and give some inputs which will shape the result of our code (just like in mathematics).
about("Jack", 19, "TV Shows"):
Output:
"Meet Jack! They are 19 years old and they like TV Shows.
In this case, the parameters are what we use while defining the function. So "name", "age" and "likes" are our parameters. Arguments are what we pass into the function, which are "Jack", 19 and "TV Shows".
Keyword arguments
If we combine our arguments with the parameters, we get keyword arguments and we don't have to write our arguments in the order defined.
about(age = 15, likes = "Tennis", name = "Emma")
In cases where we have to follow a particular order like in the first example, those would be positional arguments.
An additional thing to note is that we can have automatically set parameters.
def about(name, age, likes="Soccer"):
sentence = f"Meet {name}! They are {age} years old and they like {likes}."
return sentence
Make a program which will execute the functions of a basic calculator, as in it can complete addition, subtraction, multiplication, division and exponentiation. Make sure these operations are completed via functions that have two parameters. The first parameter for exponentiation is the base and the second is the exponent. The first parameter for division is the dividend and the second is the divisor.
Step 1: Define and create functions for addition, subtraction, multiplication, division and exponentiation.
Step 2: Receive data as to which operation the user wants to complete.
Step 3: Receive the numbers needed for the operation and complete the operation according to the answer given in step 2.
Step 4: Print the result of the operation to the screen.
Step 5: Tell the user to re-run the code if they want to complete another operation.
This is only a suggested answer
def addition(x, y):
return x + y
def subtraction(x, y):
return x - y
def multiplication(x, y):
return x * y
def division(x, y):
return x / y
def exponentiation(x, y):
return x ** y
operation = input("What operation would you like to do? ").lower()
if operation == "addition":
print(addition(int(input("Enter the first number: ")), int(input("Enter the second number: "))))
elif operation == "subtraction":
print(subtraction(int(input("Enter the first number: ")), int(input("Enter the second number: "))))
elif operation == "multiplication":
print(multiplication(int(input("Enter the first number: ")), int(input("Enter the second number: "))))
elif operation == "division":
print(division(int(input("Enter the dividend: ")), int(input("Enter the divisor: "))))
elif operation == "exponentiation":
print(exponentiation(int(input("Enter the base number: ")), int(input("Enter the exponent: "))))
print("Run again for other operations.")
As of February 2025, MyPyFriend is only up to this point and you have reached the final topic. Although more is to come, this is the end of the website for now. If you haven't already, you may read the Math and Python section.