Discussion Unit 2 CS 1101.docx - Create your own Python code examples that demonstrate each of the following Do not copy examples from the book or any

Discussion Unit 2 CS 1101.docx - Create your own Python...

This preview shows page 1 - 4 out of 16 pages.

Create your own Python code examples that demonstrate each of the following. Donot copy examples from the book or any other source. Try to be creative with your examples to demonstrate that you invented them yourself.Example 1: Define a function that takes an argument. Call the function. Identify what code is the argument and what code is the parameter.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. 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.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.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.184 wordsPermalink| ReplyRe: Discussion Forum Unit 2by Dain Anderson- Friday, 13 September 2019, 9:53 AMGreetings,The following is my Discussion Forum submission for Unit 2. Thank you for taking the time to read it, share yours comment and rate it.
Background image
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 print_number(number_to_print):print(number_to_print)>>> print_number(1)1>>> The argument is 1, or the value passed to the parameter. The parameter is number_to_print which is used to reference the argument value, inside the function.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. >>> unlucky_number = 13>>> print_number(55)55>>> print_number(unlucky_number)13>>> print_number(unlucky_number+1)14>>> When passed to the print_number(), 55 represents a value, unlucky_number represents a variable and unlucky_number+1 represents an expression.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 increment_number(number_to_increment):new_number = number_to_increment + 1print(new_number)>>> print(new_number)Traceback (most recent call last):
Background image
File "<pyshell#10>", line 1, in <module>print(new_number)NameError: name 'new_number' is not defined>>> When you reference a variable outside a function, an error occurs, indicating the variable is not defined. This is because the variable new_number is locally scoped within the increment_number() function, and cannot be accessed outside of the function definition.Example 4: Create a function that takes an argument. Give the function parameter a unique name. Show what happens when youtry to use that parameter name outside the function. Explain the results.
Background image
Image of page 4

You've reached the end of your free preview.

Want to read all 16 pages?

  • Spring '17

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors