Functions (parameters)

Two of the keywords in the definition of the function we gave last lesson were modular and reusable, Let's understand these two concepts better by looking at a more expressive function in the following code editor.

Here is what we updated in this function from the previous:

  • function_with_parameter: We gave the function a new name.
  • input: This is called a parameter, this is used to pass data into the function.
  • str: This is the datatype the parameter should be, including this helps ensure the function will run as expected.
  • "enjoy your " + input: This is the string that will be returned from the function.

The major new addition here is adding the parameter in. We brought the parameter in by adding it within the parenthesis after the function name. We further specified the datatype the parameter should be. Including this helps ensure the function runs as expected. For example, here we set the datatype as str. If when we call the function we pass in anything not a string, such as an int, then the interpreter will throw an error.

Parameters and arguments

A parameter in a function is like a placeholder that you can fill with data when you use the function. It's a way to pass information to the function so it knows what to do with that data.
An argument is the term for a variable passed in to a function. In the example above, the argument is "day", which is passed in to the parameter input.

Including a parameter in a function makes it more modular, This means we can apply it to a variety of situations. In the code editor, we passed in the string "day" as an argument, which will return the string "enjoy your day". We could also pass in the string "coffee" as an argument, which will return the string "enjoy your coffee". Allowing us to use the same function for different situations is the modularity of the function.

To make sure we have a grasp on this before moving on. Let's experiment with some things. In line 4 of the above code editor we passed in the string "day". First, try putting in the string "coffee". Second, try passing in the number 2 and see what happens. Finally, try running the function without any argument.

We can make functions with multiple parameters. When we do so, the order matters. The first argument is associated with the first parameter, the second argument is associated with the second parameter and so on.

Up till now the parameters have been required arguments. There are also optional parameters. In this case, the parameter will have a default value assigned. When this function is called, we can optionally pass in a value. When we do not pass in an argument, the parameter will use its default value. On the other hand, when we do pass in an argument, it will use this instead. With optional parameters the order does not matter, as long as they are passed after all required parameters. In the following code editor, inputA and inputB are both required parameters, while inputC is an optional parameter with the default value "New Year".

Reusability

Programmers use functions to carry out specific tasks consistently, and they may use them many times—possibly hundreds, thousands, or even millions of times. Writing reliable code becomes challenging because it needs to work correctly under various circumstances.

In the previous two code examples, we ran the functions twice, each time with different values given as arguments. This demonstrates a concept called reusability. It means that we can employ the same function repeatedly, changing the arguments to produce different outcomes.

Practice Question

Using the code below, what is the closest choice when the function is called as function_with_parameter("meal")?

score: 0%