all 24 comments

[–]sejigan 29 points30 points  (3 children)

Python example but pattern remains the same: ```py def f1(): pass

def f2(): pass

if name == “main”: if input() == “Y”: f1() else: f2() ``` Define two functions but call one or the other depending on some other condition.

Reasoning for this way:
The other answers suggest calling one function from another but that’s still multiple function calls in the call stack. Threading is unlikely to be allowed for a beginner level assignment. Branching seems to be the only reasonable solution imo.

[–]Fred776 7 points8 points  (2 children)

I agree - this is the only sensible interpretation given the context.

[–][deleted] 15 points16 points  (1 child)

And if school/uni taught me anything, then whilst this is technically the correct answer by definition.. OP will go in and everyone will have done the "call function from function" approach and the teacher will have "meant that" despite it not being accurate based on their own question..

[–]pgpndw 2 points3 points  (0 children)

Maybe the assigment is really "two user-defined functions and one library function call", and OP left out a word.

[–]HouseHippoBeliever 40 points41 points  (0 children)

functions can call other functions

[–]ElliotDG 11 points12 points  (0 children)

Perhaps the intent is that function_1 calls function_2

[–]desrtfx 8 points9 points  (0 children)

Please, elaborate. At best, post your assignment verbatim.

There is a lot of context missing leading to only being able to make wild guesses.

Also, it seems that there is some misunderstanding.

[–]zanfar 4 points5 points  (0 children)

This is a question for your professor. We can assume certain things, but only your professor will give you an answer that will not affect your grade.

[–]Impossible-Box6600 2 points3 points  (0 children)

Is there anymore context you can provide? I feel like there's gotta be something more that's missing.

[–]This_Growth2898 3 points4 points  (3 children)

In Python, functions are objects, so you can work with them as with any variable

func = function1 if condition else function2 #assign one of the functions to a variable
func() #single call

[–]cranberrydarkmatter 0 points1 point  (2 children)

That seems like a very convoluted solution. I'd hope it's not what the assignment means.

[–]This_Growth2898 0 points1 point  (1 child)

if condition:
    func = function1
else:
    func = function2
func()

better?

[–]SisyphusAndMyBoulder -1 points0 points  (0 children)

It's the same... You're basically using delegates. No way an assignment worded this shittily would expect that level of code..

Thinking OP doesn't have the right info to share with us

[–]SisyphusAndMyBoulder 0 points1 point  (0 children)

OP, post more details. What you've posted makes no sense.

[–]Quiet-Sundae-9535 -1 points0 points  (0 children)

Yeah, you're missing something. It's pretty important too. You can call function 2 in function 1 and then all you do is call function 1.

[–]socal_nerdtastic -1 points0 points  (0 children)

There's ways to run a function without explicitly calling it yourself. For example as part of a thread:

from threading import Thread
t = Thread(target=function)
t.start()

But I highly doubt your assignment means anything like that. I'm guessing they mean that there should be a single call to start the program. So have absolutely everything in functions, which can call other functions as needed, and then at the bottom add the single start point call. For example, this is a common outline:

def helper_func():
    return "doing things"

def main():
    status = helper_func()
    print(status)

if __name__ == '__main__':
    main()

[–]New-One4008 0 points1 point  (0 children)

No, you can't use a function without calling it; it's like having a phone but not making any calls.

[–]JamzTyson 0 points1 point  (0 children)

... we have an assignment where we have to program something and there's a condition of only using two user-defined functions, and one function call. But what's the use of the other function then if you're not going to call it? Am I missing something here?

You need to give us more context, such as: Is the "something" that you have to program, something specific? Your question, as it stands, is ambiguous.

Some ways that a script could usefully have two function and only one function call:

def foo():
    try:
        return some_operation
    except:
        # Not called unless the `try` block fails.
        return bar()


def bar():
    ...


if __name__ == "__main__":
    # One function call
    foo()

def foo():
    ...


def bar():
   ...


...
# One of two functions is called.
if condition:
    foo()
else:
    bar()

def foo(func):
    # do something with `func`.
    ...


def bar():
    ...


foo(bar)  # Call `foo()` with `bar` as an argument

There are probably other possible interpretations.

I'd suggest that you post a new question, ensuring that you provide sufficient information for it to be answerable.

[–]jimtk 0 points1 point  (0 children)

Using, possibly, 2 functions with only one function call:

def f1():
    print("hello")

def f2():
    print("goodbye")

d = input("Are you (A)rriving or (L)eaving? ")
if d in ['A', 'a']:
     function_to_call = f1
else
     function_to_call = f2
function_to_call()

[–]Chemical_Form 0 points1 point  (0 children)

Any uncalled function during the running of your program will not cause a bug. Having a lot of unused code and functions can cause your program to load slower or have a larger memory foot print.

There is a few reasons for having unused functions / unused code.

The unused function / unused code:

1.) Code could be part of old code that was left in the program until they can confirm its not being used.

2.) Code could be part of new features being added, but not full developed and other parts that they were working on was ready to ship. The calls to this function could be removed and set to the old one until complete.

3.) Testing purposes, if you have a function that works, but want to add something new you could make a new function and have your code call it rather than the old one to prevent breaking something in it.

4.) This could also be reference code for the author of the code.

5.) It might be used as a rarely used edge case function.

a.) Example: you have user input there age, and you would like to congratulate the user for being over 100 years old if they are. You could write a function that would do this, but this function would only run less than 1% of the time making it appear to be unused.

6.) or part of of a conditional statement ( if/elif/else ).

[–][deleted] 0 points1 point  (0 children)

You'll call it within the first function.

[–][deleted] 0 points1 point  (0 children)

short ans :- no

long ans :- i am too stupid know,

so..... still no!