CS1101 Discussion U2 [PDF]

  • Author / Uploaded
  • Ahmad
  • 0 0 0
  • Gefällt Ihnen dieses papier und der download? Sie können Ihre eigene PDF-Datei in wenigen Minuten kostenlos online veröffentlichen! Anmelden
Datei wird geladen, bitte warten...
Zitiervorschau

Example 1: Define a function that takes an argument. Call the function. Identify what code is the argument and what code is the parameter. >>> def message_first(name):          print("Dear " + name)  >>> message_first("Ahmad") Dear Ahmad Explanation:  The name of the function: message_first  The parameter: name The argument: Ahmad Example 2: Call your function from Example 1 three times with different kinds of arguments: a value, a variable, and an expression. Identify which kind of argument is which.  >>> message_first("John") Dear John Explanation:  The argument is a value: “John” ………………………………………………………….. >>> var = " Mr.Ali, Welcome to our course" >>> message_first(var) Dear Mr.Ali, Welcome to our course Explanation:  The argument is a variable: var = "Mr.Ali, Welcome to our course" ………………………………………………………….. >>> x = "Ali " >>> y = "Noah " >>> exp = x + y + "." * 5 >>> message_first(exp) Dear Ali Noah ..... Explanation: 

The argument is an expression: x + y + "." * 5 Example 3: Create a function with a local variable. Show what happens when you try to use that variable outside the function. Explain the results. >>> def message_first(name):               greeting = "Dear " + name               print(greeting) >>> message_first("Ali") Dear Ali >>> print(greeting) File "C:/Users/user/PycharmProjects/MyAssignment/U2.py", line 5, in     print(greeting) NameError: name 'greeting' is not defined Explanation: The local variable in this case is: greeting = "Dear " + name When I try to use that variable outside the function, I get this message: NameError: name 'greeting' is not defined. Because local variables exist inside the function, it can't be accessed outside the function. NameError shows that the variable needs to be defined outside the function. Example 4: Create a function that takes an argument. Give the function parameter a unique name. Show what happens when you try to use that parameter name outside the function. Explain the results. >>> def study_book(color):     

print("Take your " + color + " book")

>>> study_book("red") Take your red book >>> print(color) File "C:/Users/user/PycharmProjects/MyAssignment/U2.py", line 5, in     print(color) NameError: name 'color' is not defined Explanation: The parameter is: color

I get this message: NameError: name 'greeting' is not defined. Because parameters are local, they do not exist outside the function, and like local variables, they can't be accessed outside the function. Example 5: Show what happens when a variable defined outside a function has the same name as a local variable inside a function. Explain what happens to the value of each variable as the program runs. >>> def child_books(color1, color2):         color1 = "Red"         color2 = "Blue"         books = color1 + color2         print(books) >>> child_books(1, 2) RedBlue >>> color1="Red" >>> color2="Blue" >>> books = color1 + color2 >>> print(books) RedBlue Explanation: Even if the variables outside and inside the function have the same name, the variables inside child_books function do not affect outside similar variables. because these variables are independent of each other because the Python interpreter follows the flow of execution.