i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this: -
-
def display(parameter1, parameter2):
-
-
{code of display}
-
-
solve(parameter3, parameter4):
-
-
-
{code of solve and it gives me its output in the form of a number}
-
-
#now i want to return control to display
-
-
{remaining code of display continues...}
-
how do i pass control from 1 function 2 another... like in this example do i need 2 say return(in the solve function) or sumthing like tht?
4 4227
i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this: -
-
def display(parameter1, parameter2):
-
-
{code of display}
-
-
solve(parameter3, parameter4):
-
-
-
{code of solve and it gives me its output in the form of a number}
-
-
#now i want to return control to display
-
-
{remaining code of display continues...}
-
how do i pass control from 1 function 2 another... like in this example do i need 2 say return(in the solve function) or sumthing like tht?
You define ALL functions separately from one another before calling them with the parameters: -
def display(parameter1, parameter2):
-
-
{code of display}
-
-
A = solve(parameters)
-
-
{rest of code of display}
-
-
def solve(parameter3, parameter4):
-
-
{code of solve}
-
-
return number
-
-
display(x, y)
-
When solve() is called inside display() it runs with the given parameters, and then returns the value of the number you want to display, which assigns it to A. If it doesn't return anything and just runs - you just call it without assigning.
i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this: -
-
def display(parameter1, parameter2):
-
-
{code of display}
-
-
solve(parameter3, parameter4):
-
-
-
{code of solve and it gives me its output in the form of a number}
-
-
#now i want to return control to display
-
-
{remaining code of display continues...}
-
how do i pass control from 1 function 2 another... like in this example do i need 2 say return(in the solve function) or sumthing like tht?
I skipped over this the first time because I was in a hurry and because I hate seeing nested function.
The answer lie in the "scope rules". Since function2() is def(inede) inside function1() on, it call be called ONLY from within function1() - def func1(*args, **kwarg):
-
def func2(arg1, arg2):
-
print arg1, arg2
-
return results
-
# do some work to make some args
-
intermediateResuts = func2(val1, val2)
-
return results
You define ALL functions separately from one another before calling them with the parameters: -
def display(parameter1, parameter2):
-
-
{code of display}
-
-
A = solve(parameters)
-
-
{rest of code of display}
-
-
def solve(parameter3, parameter4):
-
-
{code of solve}
-
-
return number
-
-
display(x, y)
-
When solve() is called inside display() it runs with the given parameters, and then returns the value of the number you want to display, which assigns it to A. If it doesn't return anything and just runs - you just call it without assigning.
This is, of course the cleaner thing to do, although python's syntax does not require it.
bvdet 2,851
Expert Mod 2GB
i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this: -
-
def display(parameter1, parameter2):
-
-
{code of display}
-
-
solve(parameter3, parameter4):
-
-
-
{code of solve and it gives me its output in the form of a number}
-
-
#now i want to return control to display
-
-
{remaining code of display continues...}
-
how do i pass control from 1 function 2 another... like in this example do i need 2 say return(in the solve function) or sumthing like tht?
I prefer to keep my nested functions before the code of the encapsulating function: - def funcMain(a, b):
-
def funcNested(c, d):
-
# perform some calculations
-
result = c + d
-
return result
-
-
# body of funcMain()
-
.............................
-
var1 = funcNested(k, m)
-
var2 = funcNested(q, r)
-
............................
-
return answer
Python built-in 'return' returns control back to the calling function, and in this case passes the result of a calculation.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Joel Forrest Moxley |
last post by:
Greetings python-list!
The good news is that I've been having a blast with Python since early
Spring. I've had great success in both learning the language from online
/ usegroup resources and...
|
by: Joel |
last post by:
python-list@python.org at Sun, 24 Aug 2003 13:31:58 but
didn't seem to show up here]
Greetings python-list!
The good news is that I've been having a blast with Python since early
Spring. ...
|
by: Nils Grimsmo |
last post by:
hi,
i'm having some trouble nesting functions. consider the following:
def h():
x = 1
def g():
print x # ok, x is taken from h
g()
|
by: Andy Baker |
last post by:
Hi there,
I'm learning Python at the moment and trying to grok the thinking behind
it's scoping and nesting rules.
I was googling for nested functions and found this Guido quote:...
|
by: Xah Lee |
last post by:
Here's the belated Java solution.
import java.util.List;
import java.util.ArrayList;
import java.lang.Math;
class math {
public static List range(double n) {
return range(1,n,1);
}
|
by: Josiah Manson |
last post by:
I found that I was repeating the same couple of lines over and over in
a function and decided to split those lines into a nested function
after copying one too many minor changes all over. The only...
|
by: Wolfgang Draxinger |
last post by:
If you know languages like Python or D you know, that nested
functions can be really handy.
Though some compilers (looking at GCC) provide the extension of
nested functions, I wonder, how one...
|
by: Maric Michaud |
last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit :
I was not aware of any "nested classes are unsupported" before and didn't
consider nested classes as bad practice till...
|
by: Fredrik Lundh |
last post by:
Patrol Sun wrote:
so why exactly are you trying to nest 20 or 100 for-in loops?
</F>
|
by: Gabriel Rossetti |
last post by:
Hello,
I can't get getattr() to return nested functions, I tried this :
.... def titi():
.... pass
.... f = getattr(toto, "titi")
.... print str(f)
....
Traceback...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |