-7

I'm new to python, and I'm making a program for a friend. I was wondering if it's possible to call a function in another function, for example like this:

def hundredsCheck():
    if till.hundreds > 0:
        till.hundreds -= 1
        total -= 100
        deposit.hundreds += 1
        deposit.total += 100
        return
    else:
            fiftiesCheck()
            return

In this case, fiftiesCheck() is a function that is defined just below hundredsCheck(), and follows a much similar format. I have two classes as well, till and deposit, and they just contain values like the ones used in the example I listed.

I'm doing this to save space in a tiered if statement that takes up a lot of room, so this would be far simpler. If this is a viable method, then please say so, along with any other advice you may have.

huu
  • 7,032
  • 2
  • 34
  • 49
Forcsees
  • 11
  • 5
  • 10
    Why just not *try* this? Calling other functions in your functions is a very natural thing to do, why *wouldn't* this work? If it didn't, how would you ever get anything done? – Martijn Pieters May 16 '14 at 16:53
  • 2
    `Python` is an excellent language in part because of how easy it is to just pop this code into a file and run it. [This](http://www.pythoncentral.io/execute-python-script-file-shell/) resource goes over many different ways to do this in different environments. – huu May 16 '14 at 16:56
  • I am new to python, I have never used it or even downloaded any form of ide or runtime for python. I'm making this program in a notepad file. Just a quick thing I'm doing for my friend. I have never ever used this language before, so I have no idea if this was a legal statement or not for Python. I'm accustomed to C++ and Java, not anything like Python or Lua. – Forcsees May 16 '14 at 16:56
  • 2
    Tell your friend to have someone who actually knows Python do their homework for them, then. – Wooble May 16 '14 at 17:11
  • `Just a quick thing I'm doing for my friend.` ...a quick thing you're asking *StackOverflow* to do for your friend. – admdrew May 16 '14 at 17:16
  • This is only but a sample of a larger project. I did everything myself, I asked for confirmation of proper syntax, that is all. I'm not doing my homework for a friend, it's merely something for fun, and he needed some help. He doesn't know anyone else that knows python that he could ask for help. No need to be rude or mean, it's unnecessary. I had a question, all you had to to was answer it or ignore it. Thank you all for your time, have a good day. – Forcsees May 16 '14 at 19:22

3 Answers3

1

You absolutely may call a function within another function in Python, so long as the callee is in the scope of the caller.

Check out this question and answer: Short Description of the Scoping Rules?

The short of it: inside a function, you can call functions in the global scope, the function scope, or, if you're inside a class, the class scope.

Community
  • 1
  • 1
calpeyser
  • 99
  • 5
0

sure you can even do stuff like

def func1():
    print "OK"

def func2():
    print "OK"
    f1 = func1 #call a function as something else
    f1()

def func3():
    print "OK"
    def f1(): #define a function inside another function
        func2()
    f1()

func3()
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

Why don't you try this? Just try putting placeholder functions in a piece of code, and paste it into your python interpreter.

But for the sake of answering this question, here is your answer.

Yes. You can call functions from other functions, and you can even define other functions inside functions:

>>> def foo():
...     print 'foo'
... 
>>> def oop():
...     def boo():
...             foo()
...             print 'boo'
...     boo()
...     print 'oop'
... 
>>> def loo():
...     oop()
... 
>>> loo()
foo
boo
oop
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76