3

I know there are several questions about this, I read but I could not understand. I'm trying to use the result of the return of one function in another:

def multiplication(a):
    c = a*a
    return c

def subtraction(c):
    d = c - 2
    return d

print(subtraction(c))

Output:

NameError: name 'c' is not defined

I know that there is a possibility of using global variables, but I have seen that this is not a good idea since the variables can change their value.

EDIT:

These two functions are just idiotic examples. I have two functions with words and I need to use the return of the first function in the second function. In case of this my idiotic example, I need the result of the first function (c) in the second function.

marin
  • 923
  • 2
  • 18
  • 26

5 Answers5

3

You are not calling your functions properly.

def multiplication(a):
    c = a*a
    return c

def subtraction(c):
    d = c - 2
    return d

# first store some value in a variable
a = 2

# then pass this variable to your multiplication function
# and store the return value
c = multiplication(a)

# then pass the returned value to your second function and print it
print(subtraction(c))
BcK
  • 2,548
  • 1
  • 13
  • 27
2

Does this makes things clearer?

def multiplication(a):
    c = a*a
    return c

def subtraction(c):
    d = c - 2
    return d

print(multiplication(5))               # 25
print(subtraction(5))                  #  3
print(multiplication(subtraction(5)))  #  9
print(subtraction(multiplication(5)))  # 23

I think you're trying to do what's happing in the last print statement: first call the multiplication function, and then call the subtraction function on the result.


Note that the variable c in your multiplication function is an entirely different variable from the c in your subtraction function. So much so, that it may make things more clear to rename your variables, perhaps something like this:

def multiplication(a):
    product = a * a
    return product

def subtraction(a):
    difference = a - 2
    return difference
WhiteHotLoveTiger
  • 2,088
  • 3
  • 30
  • 41
1

So why not use return value?

print(subtraction(multiplication(24)))

?

bipll
  • 11,747
  • 1
  • 18
  • 32
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/20347703) – Ivan Jul 19 '18 at 14:39
  • Oh really? Sp, my reply does not say anything about how "to use the result of the return of one function in another", does it? – bipll Jul 20 '18 at 05:35
1

'c' is not declared outside the 'subtraction' function. You need to give need to declare 'c' before printing. Let's say you want 'c' to be 5, then:

c = 5
print(subtraction(c))
guygrinberger
  • 327
  • 2
  • 14
1

You have defined two functions which both return a number.

If you call subtraction(c) you will get the error you see, because there is no c.

If you define a c in scope of the print statmenet

c = 42
print(subtraction(c))

it will be ok.

Try thinking of it like this: each function takes a variable does things to it and returns a number.

e.g.

>>> multiplication(101)
10201

That this happened to be called c isnide the function isn't known outside the function (i.e scope).

You can save the number to a variable

>>> x = multiplication(101)

Then x remembers that value. Or

>>> c = multiplication(101)

This is not the same c as you have inside the functions.


(And after the question edit):

Decide what value you want to call the first function with, for example 101:

>>> c = multiplication(101)

then use that return to call the next function:

>>>> subtraction(c)

Or just chain them togther:

subtraction( multiplication(101) )

To start the chain you will need to use a string, int or defined variable. Otherwise you get name not defined errors. Once a variable is used in a function it goes out of scope when the function ends.

doctorlove
  • 18,872
  • 2
  • 46
  • 62