Question on Python Function

J

joy99

Dear Group,

I have a small question on function.

If I write two functions like the following:

IDLE 2.6.5 element1=5
element2=6
add=element1+element2
print "PRINT THE ADDITION",add

element3=7
element4=22
mult=element3*element4
print "PRINT THE MULTIPLICATION",mult

Can I now write a third function where the above two functions can be
passed as argument or parameter?

Best Regards,
Subhabrata.

NB: As I copied the code from IDLE to MS-Word befor posting here,
codes may have slight indentation errors.
 
A

Alister

Dear Group,

I have a small question on function.

If I write two functions like the following:

IDLE 2.6.5
element1=5
element2=6
add=element1+element2
print "PRINT THE ADDITION",add


element3=7
element4=22
mult=element3*element4
print "PRINT THE MULTIPLICATION",mult

Can I now write a third function where the above two functions can be
passed as argument or parameter?

Best Regards,
Subhabrata.

NB: As I copied the code from IDLE to MS-Word befor posting here, codes
may have slight indentation errors.

I don't quite see the point of your functions as they do not use their
parameters in any way, but you need to set a return value

IDLE 2.6.5 element1=5
element2=6
add=element1+element2
return add
element3=7
element4=22
mult=element3*element4
return mult
print "add= %s mult=%s" % (x , y)


--
Your digestive system is your body's Fun House, whereby food goes on a
long,
dark, scary ride, taking all kinds of unexpected twists and turns, being
attacked by vicious secretions along the way, and not knowing until the
last
minute whether it will be turned into a useful body part or ejected into
the
Dark Hole by Mister Sphincter. We Americans live in a nation where the
medical-care system is second to none in the world, unless you count maybe
25 or 30 little scuzzball countries like Scotland that we could vaporize
in
seconds if we felt like it.
-- Dave Barry, "Stay Fit & Healthy Until You're Dead"
 
V

Vlastimil Brom

2010/5/24 joy99 said:
Dear Group,

I have a small question on function.

If I write two functions like the following:

IDLE 2.6.5
       element1=5
       element2=6
       add=element1+element2
       print "PRINT THE ADDITION",add


       element3=7
       element4=22
       mult=element3*element4
       print "PRINT THE MULTIPLICATION",mult

Can I now write a third function where the above two functions can be
passed as argument or parameter?

Best Regards,
Subhabrata.

NB: As I copied the code from IDLE to MS-Word befor posting here,
codes may have slight indentation errors.

Hi,
while it is quite unclear to me, what you are trying to achieve (what
are the passed n arguments supposed to do?), you can well pass an
already defined function as an argument to another function; if you
want to select a function for the needed operation, if can be e.g.:

def compute(arg1, arg2, fn):
fn(arg1, arg2)

- supposing you don't want to "return" the result but just print it as
your functions do;
is it what you were after or did I miss something more complex?

hth
vbr
 
A

Alister

Hi,
while it is quite unclear to me, what you are trying to achieve (what
are the passed n arguments supposed to do?), you can well pass an
already defined function as an argument to another function; if you want
to select a function for the needed operation, if can be e.g.:

def compute(arg1, arg2, fn):
fn(arg1, arg2)

- supposing you don't want to "return" the result but just print it as
your functions do;
is it what you were after or did I miss something more complex?

hth
vbr
I did not realise you could pass a function like this, I am sure it could
lead to some interesting programming.

I am still new to OOP & the light is only just starting to switch on.

analysing your examples I now realise that all function parameters are
Objects & EVERYTHING(well almost anyway) is an object.

It just goes to show that even when you know 1 answer to the original
question You can still learn by looking at others







--
"And, of course, you have the commercials where savvy businesspeople Get
Ahead
by using their MacIntosh computers to create the ultimate American
business
product: a really sharp-looking report."
-- Dave Barry
 
J

joy99

2010/5/24 joy99 <[email protected]>:














Hi,
while it is quite unclear to me, what you are trying to achieve (what
are the passed n arguments supposed to do?), you can well pass an
already defined function as an argument to another function; if you
want to select a function for the needed operation, if can be e.g.:

def compute(arg1, arg2, fn):
    fn(arg1, arg2)

- supposing you don't want to "return" the result but just print it as
your functions do;
is it what you were after or did I miss something more complex?

hth
  vbr- Hide quoted text -

- Show quoted text -

Dear Vlastimil,

You are right. Your approach will do to me. I was trying Python Doc
either I do not know where to check, or I could not find.
I am trying to work out. Numbers I can pass, I was checking the last
example in Python Docs with "def cheeseshop(kind, *arguments,
**keywords):" if that gives me any clue.

If you can kindly try.

Best Regards,
Subhabrata.
 
J

joy99

2010/5/24 joy99 <[email protected]>:














Hi,
while it is quite unclear to me, what you are trying to achieve (what
are the passed n arguments supposed to do?), you can well pass an
already defined function as an argument to another function; if you
want to select a function for the needed operation, if can be e.g.:

def compute(arg1, arg2, fn):
    fn(arg1, arg2)

- supposing you don't want to "return" the result but just print it as
your functions do;
is it what you were after or did I miss something more complex?

hth
  vbr- Hide quoted text -

- Show quoted text -

Dear Vlastimir,

As pointed out by Alister, I can print the values of function1 and
function2 with the help of another function3, but my target is to call
the "add" value of function1 and "mult" value of function2 in a third
function or the values and parameters of the third function in fourth
function. While calling, I am looking not only to print, but to use
them or manipulate them.

I hope I am bit clear now.

Best Regards,
Subhabrata.
 
K

Kushal Kumaran

<snip>

Dear Vlastimir,

As pointed out by Alister, I can print the values of function1 and
function2 with the help of another function3, but my target is to call
the "add" value of function1 and "mult" value of function2 in a third
function or the values and parameters of the third function in fourth
function. While calling, I am looking not only to print, but to use
them or manipulate them.

I hope I am bit clear now.

If you need to use the values in another function, you need a way to
let that function get its hands on the values. Your function1 and
function2 should return the values they compute instead of printing
them out.
 
P

Peter Otten

Kushal said:
If you need to use the values in another function, you need a way to
let that function get its hands on the values. Your function1 and
function2 should return the values they compute instead of printing
them out.

For example:
.... return x + y
........ return x * y
........ return add(mul(x, x), mul(y, y))
....25

OP: If that addresses your question I suggest that you work through some
introductory text about python. The python wiki has a few suggestions, see

http://wiki.python.org/moin/BeginnersGuide

Peter
 
J

joy99

For example:

 >>> def add(x, y):
...     return x + y
...>>> def mul(x, y):

...     return x * y
...>>> def sum_of_squares(x, y):

...     return add(mul(x, x), mul(y, y))
...>>> sum_of_squares(3, 4)

25

OP: If that addresses your question I suggest that you work through some
introductory text about python. The python wiki has a few suggestions, see

http://wiki.python.org/moin/BeginnersGuide

Peter

Hi Peter and Other Kind Contributors of the Group,

I got lot of insights from the discussion in the group. Though I did
not get the exact answer but from Peter's link I could work them out.
Thank you, Peter, for kindly referring me the link. I like to thank
others of the group also for kindly spending their valuable time. I
wanted to define one function and call the value of that function to
be used by another function. If I misquoted the problem, I am sorry.

Wishing You all a Great Day Ahead,
Best Regards,
Subhabrata.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top