-1

How do I assign a global variable to a function's return? The only way I seem to be able to get it to work properly is to assign the variable to the function itself. When I run the code, the variable assignment performs the function on that line, which is not what I want. I only want an assignment so I can use the variable later.

def random_function():   
    x = 3.14    
    return x

This is what I'd like to happen but does not work:

pi = x

This works but runs the function in the console on that line which is not something I want:

pi = random_function() 
print(pi)
martineau
  • 119,623
  • 25
  • 170
  • 301
JReinhal
  • 1
  • 2
  • `pi = random_function() ` is the way to go. Search for the keywords: "python by pass-by-reference" for more info – Nir Alfasi Apr 08 '19 at 21:16
  • 1
    `x` does not exist outside of the function. `pi = random_function()` is exactly how you would call the function and assign its return value to a variable. – jasonharper Apr 08 '19 at 21:17
  • 1
    Don't use global variables. Pass data in as arguments, and return data as return values. – juanpa.arrivillaga Apr 08 '19 at 21:21
  • @juanpa.arrivillaga, your comment gets to my point. I'd like to use one function's return value as an argument in another. Is this only possible through assigning the first function to a global variable? – JReinhal Apr 08 '19 at 21:28
  • @JReinhal what? No, not at all. `a = func1(x, y); func2(a)` Or even `func2(func1(x, y))` – juanpa.arrivillaga Apr 08 '19 at 21:30

3 Answers3

0

If you're inside a function but want to assign a new value to a global, you have to declare the global first. Otherwise the identifier will be assumed to be a local variable. What I mean is this:

pi = -1   # declare your global

def random_function():
    global pi
    x = 3.14
    pi = x  # now works, after the `global` line above
    return x
adrtam
  • 6,991
  • 2
  • 12
  • 27
0

If I understand it right, you want to assign the return value of your function to a variable?

x = random_function()

I think that's how you can write it. You can use whatever variable you want.

Len
  • 156
  • 1
  • 5
0

You've answered your question already. To assign a global variable to a function's return value, simply assign the variable to the function's call;

def random_function():   
    x = 3.14    
    return x

pi = random_function()

You can print pi in the console if you want to see it's value. Not required.

Ozichukwu
  • 396
  • 3
  • 12
  • Thank you for your response. The issue I'm getting with this format is that if the return of the function is a print statement, then that statement is printed in the console just by declaring the global assignment to it. Is there any way around that? – JReinhal Apr 08 '19 at 21:31
  • @JReinhal You can't return statements you can only return expressions. Please update your question with an example function that emphasizes your point for clarification. – a_guest Apr 08 '19 at 21:33
  • @JReinhal If i get you correctly, your "random_function" contains a print statement. You then assign pi to this function call and all it does is print only. I'd say you replace the print statement with "return x" if I'm correct. My code should work just fine. – Ozichukwu Apr 08 '19 at 21:37