Introduction to Functions section 5. Parameters


#1

I must be the noob of noobs. I do not understand section 5. Parameters in Introduction to Functions. The first step is to run the script and nothing happens. What is it supposed to do?

def mult_two_add_three():
number = 5
print(number*2 + 3)

Next it talks about “special_item”. Why add a special_item? Why not just edit the original function to update the special of the day? it looks like more work for to add a special_item then to edit the function. I must be missing something. Everything made sense until this section. Does anyone have any suggestions? If this exercise is an oversimplification of this function, please help me understand the practical use for it in a different way or show me how I am missing the point. Maybe the exercise might make sense if the first step actually printed something to the console.

Thank you much for any help you can give.

Frank

Section 5 instructions below.

INTRODUCTION TO FUNCTIONS

Parameters

Let’s return to Engrossing Grocer’s. The special of the day will not always be mandarin oranges, it will change every day. What if we wanted to call these three print statements again, except with a variable special? We can use parameters , which are variables that you can pass into the function when you call it.

def greet_customer(special_item):
    print("Welcome to Engrossing Grocer's.")
    print("Our special is " + special_item + ".")
    print("Have fun shopping!")

In the definition heading for greet_customer() , the special_item is referred to as a formal parameter . This variable name is a placeholder for the name of the item that is the grocery’s special today. Now, when we call greet_customer , we have to provide a special_item :

greet_customer("peanut butter")

That item will get printed out in the second print statement:

Welcome to Engrossing Grocer's.
Our special is peanut butter.
Have fun shopping!

The value between the parentheses when we call the function (in this case, "peanut butter" ) is referred to as an argument of the function call. The argument is the information that is to be used in the execution of the function. When we then call the function, Python assigns the formal parameter name special_item with the actual parameter data, "peanut_butter" . In other words, it is as if this line was included at the top of the function:

special_item = "peanut butter"

Every time we call greet_customer() with a different value between the parentheses, special_item is assigned to hold that value.

1.

The function mult_two_add_three() prints a number multiplied by 2 and added to 3 . As it is written right now, the number that it operates on is always 5 .

Call the function and see what it prints to the console.

Stuck? Get a hint

2.

Now, modify the function definition so that it has a parameter called number . Then delete the number = 5 assignment on the first line of the function.

Pass the number 1 into your function call.

3.

Call the function with the value 5 as the argument.

4.

Call the function with the value -1 as the argument.

5.

Call the function with the value 0 as the argument.


#2

you currently only defined the function. A function only execute when called, so you will need to add a function call

parameters are very useful, to go with your example:

def greet_customer(special_item):
    print("Welcome to Engrossing Grocer's.")
    print("Our special is " + special_item + ".")
    print("Have fun shopping!")

greet_customer("peanut butter")
greet_customer("jam")
greet_customer("cheese")
greet_customer("salad")

which is a lot shorter then:

print("Welcome to Engrossing Grocer's.")
print("Our special is peanut butter.")
print("Have fun shopping!")

print("Welcome to Engrossing Grocer's.")
print("Our special is jam.")
print("Have fun shopping!")

print("Welcome to Engrossing Grocer's.")
print("Our special is cheese.")
print("Have fun shopping!")

print("Welcome to Engrossing Grocer's.")
print("Our special is salad.")
print("Have fun shopping!")

and sometimes, the lesson is just teaching a concept, which you will see more in action later


#3

Stetim94,

Thank you for responding.

The first part of your response “What is it supposed to do?” lets me know that the lesson may not be clear in that part. I pasted the whole section of the lesson from the Parameter header down.

In response to your example:

Because you added both the long way and the short way of doing it, that helps me more than the lesson did. If I didn’t see the redundant entries in the second example, I wouldn’t have understood how special_item pulls information. I see now that when using special_item, you also need to add the items when calling the function. I would have thought that everytime you call the function with a different item, it would change it rather than add another entry.

greet_customer(“peanut butter”)
greet_customer(“jam”)
greet_customer(“cheese”)
greet_customer(“salad”)

I didn’t understand that part. It looked like special_item was pulling information from mid-air. This shows how little I know about programing.

With that said, if all i was doing is change out one item, I still think it would have been easier to edit the function like this.

def greet_customer ():
print(“Welcome to Engrossing Grocer’s.”)
print(“Our special is peanut butter.”)
print(“Have fun shopping!”)

greet_customer ()

Rather than like the statement below

def greet_customer(special_item):
print(“Welcome to Engrossing Grocer’s.”)
print('Our special is " + special_item + “.”)
print(“Have fun shopping!”)

greet_customer(“peanut butter”)

The lesson only dealt with one item to change out and didn’t add multiple items. This is one of the reasons it made it confusing to me. Not having an example of other ways to do it was the other thing that kept me from understanding it. But now I understand how to use special_item thanks to you.

I appreciate the help you gave me Stetim94


#4

The lesson leaves it to you to piece together of parameters and arguments and what advantage this offers.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.