0

How do I avoid having a long list with function calls in the end of the program like down below? Below is just an example but when I code larger programs it can easily become a "list" of over 20 function calls.

def ask_age():
    age = input("age: ")
    return age

def calculate_age_minus_ten(age):
    age_minus_ten = int(age) - 10
    return age_minus_ten

def print_phrase(age,age_minus_ten):
    print("I am " + str(age) + " years old. 10 years ago I was " + str(age_minus_ten) + " 
    years old")

age = ask_age()
age_minus_ten = calculate_age_minus_ten(age)
print_phrase(age, age_minus_ten)

I would like to make it look something like this:

def ask_age():
    age = input("age: ")
    return age

def calculate_age_minus_ten():
    age_minus_ten = int(ask_age()) - 10
    return age_minus_ten

def print_phrase():
    print("I am " + ask_age() + " years old. 10 years ago I was " + str(calculate_age_minus_ten()) + " years old")

print_phrase()

It has worked to code according to example 1 in school for a couple of weeks now, but when I have to do larger programs, it becomes difficult to collect all function calls at the bottom. So what I want to do is to continuously call functions in the code so that I only need to call one function at the bottom.

3
  • Do you need separated functions ? I would recommend making a single function with everything you need, instead of having multiple functions that do simple things such as taking an single input. Commented Nov 30, 2021 at 22:25
  • You need to call each function at least once either way. It doesn't really matter if you do this 'spread out', or all together. Depending on the code, it may be clearer to call all functions together. Commented Nov 30, 2021 at 22:27
  • If you want to keep the functions separate like that, you can always define yet another function which simple calls each of the other three one-after-the-other. This is something done fairly often. Commented Nov 30, 2021 at 23:31

1 Answer 1

1

My suggestion: don't use multiple functions, do everything you need in a single one and if you still want those values you can return them:

def foo():
    age = int(input("age: "))
    age_minus_ten = age - 10
    print(f"I am {age} years old. 10 years ago I was {age_minus_ten}"
    return age, age_minus_ten

age, age_minus_ten = foo() # If you want the return values
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.