Functions are created using the def keyword followed by the function name followed by the open-closed brackets followed by the double comma (:). So, in the first line the function keyword def, function name, set of brackets, and double comma are required to define a function but without an indented function body, the function does nothing. This is also called the function header. The indentation of the function body is the four spaces. The function body is defined as all the indented code lines after the function header. The general definition of the function can be written in the following form.
Example 1 - define a function that will print two sentences.
Solution: - we will create a function using def keyword followed by the function name (function name in this case is PrintString()) followed by the double comma (:). The function body will be indented in which we will use two print built-in functions to show as output two strings and these are "this is the first sentence." and "This is the second sentence.". After the function is defined and the function body created outside of the function we will create a new variable test and assign it to that variable function name with empty brackets (). The brackets have to be followed after the function name even if the function does not require any arguments.
Output:
Solution The function is created using the def keyword followed by the function name which is in this case called Calculator followed by the set of brackets followed by the colon (:). The function body will start by asking the user to type two numbers that will be converted to floating point numbers.
def FunctionName(argument1,...):The function can be created without requiring any arguments so for this function put open/close brackets without arguments.
#function body
test = FunctionName(argument1, argument2,...)
Example 1 - define a function that will print two sentences.
Solution: - we will create a function using def keyword followed by the function name (function name in this case is PrintString()) followed by the double comma (:). The function body will be indented in which we will use two print built-in functions to show as output two strings and these are "this is the first sentence." and "This is the second sentence.". After the function is defined and the function body created outside of the function we will create a new variable test and assign it to that variable function name with empty brackets (). The brackets have to be followed after the function name even if the function does not require any arguments.
def PrintString():The value of PrintString is a function object, which has the type 'function'. In the last line of code, we have called a function and assigned it to a variable test. We could also type "PrintString()" and the Python would show the output of the function since the entire function body of the function PrintString contains two print built-in functions.
print("This is the first sentence.")
print("This is the second sentence.")
test = PrintString()
Output:
This is the first sentence.Example 2 Create a function that will ask a user to type two numbers and show as output the result of their addition. Expand the function to ask a user to choose one of four basic mathematical operations (addition, subtraction, multiplication, and division).
This is the second sentence.
Solution The function is created using the def keyword followed by the function name which is in this case called Calculator followed by the set of brackets followed by the colon (:). The function body will start by asking the user to type two numbers that will be converted to floating point numbers.
USER_INPUT1 = float(input("Enter first number:> "))The next step is to create a while loop in which the user is asked to type in the mathematical operation that will be applied to those two numbers (addition, subtraction, multiplication, or division). After a user types in the mathematical operation, this operation will be performed and the function will end.
USER_INPUT2 = float(input("Enter second number:> "))
while True:The entire code for this example is given below.
print("Choose math operation") print("For Addition type add")
print("For Subtraction type sub")
print("For Multiplication type mul")
print("For Division type div")
MATH_OP = input("Type the math operation:> ")
if MATH_OP == "add":
print("{} + {} = {}".format(USER_INPUT1,
USER_INPUT2,
USER_INPUT1 + USER_INPUT2))
break
elif MATH_OP == "sub":
print("{} - {} = {}".format(USER_INPUT1,
USER_INPUT2,
USER_INPUT1 - USER_INPUT2))
break
elif MATH_OP == "mul":
print("{} * {} = {}".format(USER_INPUT1,
USER_INPUT2,
USER_INPUT1*USER_INPUT2))
break
else:
print("{}/{} = {}".format(USER_INPUT1,
USER_INPUT2,
USER_INPUT1/USER_INPUT2))
break
def Calculator():Output: Test - addition
USER_INPUT1 = float(input("Enter first number:> "))
USER_INPUT2 = float(input("Enter second number:> "))
while True:
print("Choose math operation")
print("For Addition type add")
print("For Subtraction type sub")
print("For Multiplication type mul")
print("For Division type div")
MATH_OP = input("Type the math operation:> ")
if MATH_OP == "add":
print("{} + {} = {}".format(USER_INPUT1,
USER_INPUT2,
USER_INPUT1 + USER_INPUT2))
break
elif MATH_OP == "sub":
print("{} - {} = {}".format(USER_INPUT1,
USER_INPUT2,
USER_INPUT1 - USER_INPUT2))
break
elif MATH_OP == "mul":
print("{} * {} = {}".format(USER_INPUT1,
USER_INPUT2,
USER_INPUT1*USER_INPUT2))
break
else:
print("{}/{} = {}".format(USER_INPUT1,
USER_INPUT2,
USER_INPUT1/USER_INPUT2))
break
test = Calculator()
Enter first number:> 5Output: Test - subtraction
Enter second number:> 10
Choose math operation
For Addition type add
For Subtraction type sub
For Multiplication type mul
For Division type div
Type the math operation:> add
5.0 + 10.0 = 15.0
Enter first number:> 5Output: Test - multiplication
Enter second number:> 10
Choose math operation
For Addition type add
For Subtraction type sub
For Multiplication type mul
For Division type div
Type the math operation:> sub
5.0 - 10.0 = -5.0
Enter first number:> 5Output: Test - division
Enter second number:> 10
Choose math operation
For Addition type add
For Subtraction type sub
For Multiplication type mul
For Division type div
Type the math operation:> mul
5.0 * 10.0 = 50.0
Enter first number:> 5The problem with these functions is that doesn't require any input argument and they do not return any value. In all these examples the called function was assigned to the variable "test" however, these functions did not return any value so nothing was assigned to that variable after the code is executed. To assign the result of the function to the variable we have to use return variable which will be explained in the next post.
Enter second number:> 10
Choose math operation
For Addition type add
For Subtraction type sub
For Multiplication type mul
For Division type div
Type the math operation:> div
5.0/10.0 = 0.5