Hi all, I am a newb, got a simple question, anyone can give any idea about it will be great,thanks in advance.

a=2
b=3
def functionA(a,b):
c=a+b
return c
def functionB(c)
answer=c+1
return c

but this code doesnt work....what I want is just assign functionA as one of the argument of functionB...how can I do that?

btw,whats the different between print and return?thanks

Recommended Answers

All 3 Replies

Member Avatar for masterofpuppets

hi
here's an example on how to use return using your code:

def functionA( n, m ):
    # here m and n have nothing to do with a and b
    # when this function is called like functionA( a, b ) n gets assigned to the value of a and m gets assigned to the value of b 
    r = m + n
    return r

def functionB( r ):
    # same here: function( c ) - r gets assigned to the value of c
    answer = r + 1
    return answer

>>>a = 2
>>>b = 3
>>>c = functionA( a, b )
>>>print c
5
>>>d = functionB( c )
>>>print d 
6
>>>

Only functions can return values and that value gets assigned to a variable, i.e.
c = functionA( a, b ) means compute a + b in the function and return the result in the variable c. Sorry if this doesn't make much sense :)
If the function used print instead of return, here's what you would get:

def functionA( a, b ):
    r = a + b
    print r

>>>c = functionA( 2, 3 ):
>>>c
5
None

Hope I gave you some idea what's going on :)

P.S. next time pls use code tags :)

Hi,thanks for your replay,and it does make perfect sence,however,what I want is how can I just call functionB and it automatically gives me answer,the value of a,b are in the code initially.thanks

In the future, please use code tags when posting code. It will allow the forum members here to read your post easily and will give you answers quicker (and of better quality).

Now I will repost your code with code tags so you can see the difference:

a=2
b=3
def functionA(a,b):
    c=a+b
    return c
def functionB(c)
    answer=c+1
    return c

how can I just call functionB

You need to call your functions. All you've done is defined them. To call a function you use the function identifier, followed by parenthesis. If the function has required parameters (which both of yours do), you need to provide those inside the parenthesis. If a function returns values (which both of yours do), you need to "catch" the return into some type of container.

And one final thing: in functionB you are storing c+1 into the object called answer; however you fail to return your "answer" and are just returning the original value of c.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.