Python Forum
Beginner question - storing values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner question - storing values
#1
Hi everyone. I'm having trouble understanding the fundamentals of Python - namely taking one value and passing it into another function. I've been stuck on this for a few days and feel like I'm missing something incredibly basic. Any help would be greatly appreciated

def function1():
answer = int(input("type here"))
return answer

def function2(answer):
print(answer)

function1()
function2()

Put simply - it doesn't work, I don't know why it doesn't work, and I'm flailing around unsuccessfully trying to only work with the input value rather than accidentally re-launching function 1 again.
Many thanks,
Daniel
Reply
#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
#3
Sorry to be a pain but I'm still struggling. Now my code looks like this

def function1():
ans = 4
## I tried with adding return ans and just no line here and neither worked

def function2(ans):
print(ans)

function1()
function2()
Reply
#4
Maybe this will help

def func1():
    return 4+4

ans = func1()
print(f'This is the return from func1 -> {ans}')

def func2(ans):
    print(f'This is the return from func1 inside of func2 -> {ans}')

func2(ans)
Output:
This is the return from func1 -> 8 This is the return from func1 inside of func2 -> 8
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
The primary way to communicate with functions is arguments and return. Function arguments is how you pass information into a function. return is how a function passes information back.

To paraphrase some of the examples provide so far.
def func1(a, b):
    return a + b

print(func1(1, 2)
Line 1 is a function declaration. func1 has arguments a and b. They local variable created by Python when the function is called. They are assigned values that were passed by the caller of the function. In this example the func1() function is called in line 4. The value 1 is passed as the value for the argument a, and the value 2 is passed as the value for the argument b.

Line 2 computes the sum of a and b and returns this value. All functions return a value. If the function does not contain a "return" statement, the function returns None. In this example the function returns 3, the sum of the argument values 1 and 2.

There are other ways to get information into and out of a function, but arguments and return values are the most common, and the most preferred. Your program should be written like this:
def add_values(a, b):
    return a + b

def print_value(value):
    print(value)

print_value(add_values(1, 3))
This will print the number "4". If you change the arguments padded to add_values it will print a different number. This is what functions do. They perform an operation on input data. Change the input data an the function produces different results. That is why I did not use your function1. A function that always returns the same value should not be a function.

I also renamed your functions. Functions should not have names like function1 and function2. Functions should be named after what they do. My add_values adds two values and returns the sum. My print_value function print a value.

Another way to use the functions it to save the result of add_values in a variable and pass the variable to the print_value function.
def add_values(a, b):
    return a + b

def print_value(value):
    print(value)

a_plus_b = add_values(1, 3)
print_value(a_plus_b)
You can also pass variables as arguments to a function.
def add_values(a, b):
    return a + b

def print_value(value):
    print(value)

a = 1
b = 3
a_plus_b = add_values(a, b)
print_value(a_plus_b)
Combining function calls with variables lets you perform complex, multi-part calculations.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 169 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,700 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,296 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,444 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  beginner question about lists and functions sudonym3 5 2,726 Oct-17-2020, 12:31 AM
Last Post: perfringo
  beginner question ___ 1 1,730 Jul-12-2020, 08:12 AM
Last Post: Gribouillis
  Beginner question: lxml's findall in an xml namespace aecklers 0 2,906 Jan-22-2020, 10:53 AM
Last Post: aecklers
  Super easy beginner question AkulaLA 3 3,248 Nov-07-2019, 03:42 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 2,761 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Beginner Question - Esaping the Escape Character correctly? Bramen 4 2,696 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