Parameters

Functions have parameters, which are used to generalize what the function does. You can also think of the parameters as inputs to the function.

For example, I could have a function called scoop_ice_cream(flavor), which has a parameter called flavor. Parameters, as you can see, are placed inside the parentheses after the name of the function.

When I call or use the method, I pass in an argument for flavor (the actual value that goes into the parameter), such as ‘vanilla’ or ‘chocolate’. To call the function, I would type scoop_ice_cream('vanilla') or scoop_ice_cream('chocolate').

This is useful because I don’t have to retype a bunch of code that’s associated with scooping ice cream, I can use the same code with different flavors!

#defining the function
def scoop_ice_cream(flavor):
	if flavor == "chocolate":
		print("That will be $2!")
	elif flavor == "vanilla":
		print("That will be $1!")
	else:
		print("That will be $3!")

#calling the function
scoop_ice_cream("chocolate")

Output:

That will be $2!

Multiple Parameters

You can have as many parameters as you want. You can also have no parameters. For example, print() can take no parameters, and it will simply output an empty line.

However, if you do decide to have multiple parameters, remember that the order of how you input your parameters matters. In the example below, since 5 is the first argument given, it gets assigned to the first parameter, which is a.

def func(a, b, c):
	print("a is " + a)
	print("b is " + b)
	print("c is " + c)

func(5, 6, 7)

Output:

a is 5
b is 6
c is 7

Keyword and Default Parameters

Using keyword parameters is a way to inputting values into a function without having to worry about the order. Here is an example of its use:

def func(p1, p2, p3):
	print(p1)
  print(p2)
  print(p3)

func(p3=1, p1=2, p2=3)

Output: