Python Forum
Beginner question - storing values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner question - storing values
#2
Functions are a way of abstracting some code away, so from the outside of the function you don't have to care about how it does the job, you might just have to hand it some data and get an answer back. From inside the function you don't have to care about when the function is called, just how to get the data from the caller and how to pass it back.

In your function1, it doesn't need any information from the caller, it calls input() directly. But it does produce output (via return). But in your main code you're not doing anything with the value so it disappears.

3 + 4             # valid but useless python.  It would compute an answer, but would forget it after
ans = 3 + 4       # here the value is stored in a variable for later use
function1()       # the function may be returning data, but it's not kept.
ans = function1() # now we can do something with the info from the function.
Same thing when we want to hand data to the function. That's done through the argument list. You've defined function2 to take one argument (which will be seen as answer inside the function).

answer2()             # Error.  The function expects one piece of data to arrive.  Sending none is not allowed
answer2(ans)          # We send in the data we got earlier
answer2("bogus")      # Send in some made-up data
answer2("one", "two") # Also an error.  We can only send in one piece of data to this function, not less, not more
Talbot9 likes this post
Reply


Messages In This Thread
Beginner question - storing values - by cybertron2 - Mar-09-2021, 12:03 AM
RE: Beginner question - storing values - by bowlofred - Mar-09-2021, 12:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 271 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,813 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,417 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,580 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  beginner question about lists and functions sudonym3 5 2,837 Oct-17-2020, 12:31 AM
Last Post: perfringo
  beginner question ___ 1 1,777 Jul-12-2020, 08:12 AM
Last Post: Gribouillis
  Beginner question: lxml's findall in an xml namespace aecklers 0 2,962 Jan-22-2020, 10:53 AM
Last Post: aecklers
  Super easy beginner question AkulaLA 3 3,350 Nov-07-2019, 03:42 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 2,859 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Beginner Question - Esaping the Escape Character correctly? Bramen 4 2,769 Aug-27-2019, 02:38 PM
Last Post: Bramen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020