Home > Software engineering >  Use value to another function in any function
Use value to another function in any function

Time:12-13

def anshu():
    
  a=1 2
  print(a)


anshu()

def sanju():
    b=2 3
    print(b)

sanju()

def bala():
    
     
    c=a b
    print(c)

can you explain? I gave many value in one or more function i want use these value in any function in python

CodePudding user response:

To access a variable from one function in another function, you can either return the variable from the first function and pass it as an argument to the second function, or you can make the variable global so that it can be accessed from any function. Here's an example using the first approach:

def anshu():
    a = 1   2
    return a

def sanju():
    b = 2   3
    return b

def bala(a, b):
    c = a   b
    print(c)

anshu_result = anshu()
sanju_result = sanju()
bala(anshu_result, sanju_result)

Here's an example using the second approach:

def anshu():
    global a
    a = 1   2

def sanju():
    global b
    b = 2   3

def bala():
    c = a   b
    print(c)

anshu()
sanju()
bala()

Note that using global variables is generally not considered good practice, because it can make your code difficult to maintain and debug. It's usually better to use the first approach of passing variables as arguments to functions.

  • Related