Create a function that accepts arguments #
Arguments are values that we specify when calling a function as its inputs. For example, we can call print("Hello World!"), and the message "Hello World!" is printed out in the output box. We can create functions that accept values by adding parameters in the function definition.
For example, we can create a function that takes 2 arguments, like so:
def my_function(param1, param2):
print(param1 + param2)
When we call the function my_function(10, 20), we pass 2 number arguments. Inside the function, variable param1 will be 10, and variable param2 will be 20, then the print() function will be called with those 2 numbers added together.
my_function(1, 1) # Output: 2
my_function(5, 5) # Output: 10
Exercise #
Create a new function called add_them_up that takes 2 numbers and prints the total of those 2 numbers by calling print().
Tests #
- Create a new function named
add_them_up - Function
add_them_upshould accept 2 arguments - Calling the function
add_them_up(1, 2)should callprint(3) - Calling the function
add_them_up(10, 10)should callprint(20)
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules