3 – Default Arguments

Let’s revisit the cylinder volume function, but with one modification. This function includes a default argument. If radius is not specified, then the variable radius will default to five when used in the function body. Calling the function like this would be the same thing as calling it like this, because radius is set to five if not specified as another value. Default arguments allow functions to use default values when those arguments are omitted. This is helpful because it can make your code more concise in scenarios where there is a common value you can use for a variable, although you still want it to be customizable. In this example, the default radius is five but we can still change this. This time when we call the function, we are overriding the default value of five and calculating the cylinder volume for a cylinder with a radius of seven instead. Also notice here, we’re passing values to our arguments by position. It’s possible to pass values in two ways, by position and by name. Each of these function is evaluated in the same way. This one is simply taking the arguments by position while this function is accepting arguments by name. There are some nuances for when you can perform the passing of values in each of these ways. You’ll see more applications of this in the following quiz section.

Dr. Serendipity에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

Continue reading