My Function Wont Return Values

+1 vote
asked Nov 8 by Python CPP Guy (130 points)
So,

My Code Is More Complex Than As Follows But The Issues Is Explained

a = 1

def myCode(a):
    a = a + 1
    return(a)

print(a)

and it would return 1, not 2 when the return funtion should make it 2.

so have i coded it wrong or is there a line of code im missing or is there something else wrong.

1 Answer

0 votes
answered Nov 12 by LiOS (5,280 points)

Based on what's been provided, you've not called the function. So you can either call the function and print 2 but variable a will still remain 1 afterwards (method 1) or call the function to update variable a to hold a value of 2 and will remain 2 for future use (method 2):

Method 1:

a = 1

def myCode(a):
    a = a + 1
    return(a)
    
print(myCode(a))

Method 2:

a = 1

def myCode(a):
    a = a + 1
    return(a)
    
a = myCode(a)

print(a)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...