By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,594 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,594 IT Pros & Developers. It's quick & easy.

Can a function be called within a function ?

P: n/a

Is it possible to write code and allow a function to be called within
another like I have shown below ?

Pete

def populatelist():
f=open(_globals.appath + "dxcluster.svr","r")
while true:
text = f.readline()
if text =="":
break
_list.append(string.strip(text))
f.close()
def serversettings():
servers.main(root)
if _globals.refresh=="yes":
populatelist() <----------------------- calling function above.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hiya,

That's certainly possible, it's standard practice.

(It might be cleaner to pass filenames using parameters though)

cheers,

--Tim

Jul 19 '05 #2

P: n/a
Peter Moscatt wrote:
Is it possible to write code and allow a function to be called within
another like I have shown below ?
Yes, of course. In fact you do it six times in the code below, to call open(), readline(), append(),
close(), main() and populatelist().

Kent

Pete

def populatelist():
f=open(_globals.appath + "dxcluster.svr","r")
while true:
text = f.readline()
if text =="":
break
_list.append(string.strip(text))
f.close()
def serversettings():
servers.main(root)
if _globals.refresh=="yes":
populatelist() <----------------------- calling function above.

Jul 19 '05 #3

P: n/a
Peter Moscatt wrote:
Is it possible to write code and allow a function to be called within
another like I have shown below ?

Pete

def populatelist():
f=open(_globals.appath + "dxcluster.svr","r")
while true:
text = f.readline()
if text =="":
break
_list.append(string.strip(text))
f.close()
def serversettings():
servers.main(root)
if _globals.refresh=="yes":
populatelist() <----------------------- calling function above.


Yes. It is perfectly normal. As programs get more complex, you
find methods calling methods calling methods. And as Kent pointed
out, you are already callin methods from withon methods by
calling the likes of open() and close().

Beware of creating loops - methods that call themselves, eitehr
directly or by calling other methods that end up calling the
original method. This can be done with care to make sure that at
some point a method is sure to return without calling onwards,
usually by having the problem simplified every time, but you
should avoid this kind of thing for now. It's called recursion.
An example would be in calculating x to the power of y. x^y can
be calculated by working out x * x^(y-1), but x^(y-1) is x *
x^(y-2) etc. But you canbe sure this ends when we finally come to
work out x^1 which is x, and this breaks the loop. If that makes
sense.

Steve
Jul 19 '05 #4

P: n/a
Thanks Guys,

I didn't think it was calling the function so I will put some test code and
print something to the screen just to ensure it's entering the function.

Thanks for all ya help.

Pete


Peter Moscatt wrote:

Is it possible to write code and allow a function to be called within
another like I have shown below ?

Pete

def populatelist():
f=open(_globals.appath + "dxcluster.svr","r")
while true:
text = f.readline()
if text =="":
break
_list.append(string.strip(text))
f.close()
def serversettings():
servers.main(root)
if _globals.refresh=="yes":
populatelist() <----------------------- calling function
above.


Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.