Python

Using Arguments

Calling functions in Python

Purpose

Remember when we said that we’ll cover the purpose of the parentheses? Those parentheses are space for any input values you want the function to take. What do I mean by input value? Well, sometimes, we don’t want the function to be restricted to doing one exact thing (like saying "Hi"). Maybe, we want slight changes between each run (like saying "I moved 6 feet" and then "I moved 4 feet"). This can be accomplished by passing input values into the function.

Syntax

In Python, if we want to give inputs to a function, we must first decide how many and what to call them. This is done when we "define" the function. Here is how we do it…

def function_name(input1, input2, input3, …):
	# the function's code goes here...

We give names to the inputs (replace input1, input2, etc), and then we can use those inputs (input1, input2, etc) as if they were variables, inside the function. We do this because when we "define" the function, we don’t know what the input values will be, so we treat them as variables. We actually give the values to the variables when we "call" the function. You can have any number of inputs. Just put a comma between each one’s name when "defining" the function. Here’s an example where we add two numbers…

def add_two_numbers(number1, number2):
	sum = number1 + number2
	print("The sum is " + str(sum))

NOTE: Of course, just like last time, this won’t show us anything until we actually call the function. Also, if you just want one argument, just do "def function_name(input1): instead of using the commas.

Calling Functions with Arguments

To call a function with arguments, it’s a similar process. The only difference is you need to give the actual input values to the function when you call it. So…

function_name(value_1, value_2)

Here, value_1 and value_2 are replaced by the actual input values you want to give the function when it runs this time. In our example with addition, it would look like this…

def add_two_numbers(number1, number2):
	sum = number1 + number2
	print("The sum is " + str(sum))

add_two_numbers(4,7)

Output:

The sum is 11

And, now we can actually use this function repetitively with different values so it is actually kind of useful…

def add_two_numbers(number1, number2):
	sum = number1 + number2
	print("The sum is " + str(sum))

add_two_numbers(4,7)
add_two_numbers(5,9)
add_two_numbers(100, 3500)

Output:

The sum is 11
The sum is 14
The sum is 3600

The str() is used to change data from an integer or float type to a string data type. This basically converts the data to text that we can use in strings, print statements, etc.

Remember the int() and print() functions? Those are functions that take in an input value, and give you something back. The print function outputs something to the console. The int() function, though, gives a value back to the code. How did it do that? It used the ‘return’ statement. The ‘return’ statement ends the execution of the function call (i.e. the line that ‘calls’ the function) and "returns" the result as a value. Basically, the function call is given a value in the code (i.e. the value of the expression/variable following the return keyword). Here’s an example…

def add_two(number1, number2):
	return number1 + number2

result = add_two(5,7)
# Here, the function call ‘add_two(5,7)’ returns a value to the code ‘12.’ That value is stored in the variable ‘result’

print("The result is " + str(result))

Output:

The result is 12

NOTE: If a function has the ‘return’ keyword in it, the value of the function call must always be assigned to some variable, or it must be used somewhere. If not, an error will occur because Python expects you to use the ‘returned’ value and you did not.

And that’s all we have to cover in terms of content for functions. I know this seems like an unnecessary hassle right now, but when you do our mini project for this section, you’ll truly see how useful the concept of functions is.

chevron_up