0

I seem not to be able to understand something fundamental. I have a function A which calls function B and proceeds with the results from it. Function B on the other hand does some iterative calculations and gets called by other functions repeatedly until the iteration is satisfied. When the iteration is satisfied, what I want is one result for function A (from function B), but I seem to get as many results as function B is iteratively called and my code just begins to act silly. It generally gives None result.

Here is what I mean script-wise:

def func_A():
    x = 1
    y = 1

    result_A = func_B(x, y)

    print(result_A)

def func_B(x, y):
    result_B = x + y

    if result_B < 10:
        func_C(x,y)
    else:
        return result_B

def func_C(x, y):
    x = x + 1
    y = y + 1

    func_B(x,y)

func_A()

What I want is func_A call to print 16 when x and y reach 4, however it returns None. I have some complicated nest of functions so I need to solve this problem with this function structure. If anyone could help I would appreciate it very much.

Ismail Durmaz
  • 2,521
  • 1
  • 6
  • 19
  • 1
    func_A is not returning anything. You just print it. Maybe replace `print(result_A)` with `return result_A`? Also I would replace call of `func_C` from `func_B` with call `func_B(x+1, y+1)` but that is just minor refactoring... – Minarth Jan 09 '21 at 13:44

2 Answers2

1

Please see following code. I added return when call func_C and func_B.

def func_A():
    x = 1
    y = 1
    result_A = func_B(x, y)
    print(result_A)


def func_B(x, y):
    result_B = x + y
    if result_B < 10:
        return func_C(x, y)
    return result_B


def func_C(x, y):
    x = x + 1
    y = y + 1
    return func_B(x, y)

func_A()
Canopus
  • 565
  • 3
  • 17
0

I agree with @Super Star but..

def func_B(x, y):

    result_B = x * y     #if you want 16 as an answer modify + to *
   
Aurora087
  • 86
  • 5