-1

I new at python programming and I am on a project about math, and in a moment I have faced a problem I need to do some operations about two values ​​inside the function and according to their sizes or etc. there will be a output of this function. This is what i'm trying to do:

def my_func(x,y):
  if x > y:
    my_func() = x**y + y**x
  else:
   my_func() = y*x

and i will use this in an if statement.

if my_func(x,y) and my_func(x,z) == True:

  #do something

i know it will not work cuz this is not the way the functions work. But if there is a way to do this i would be pleased to know. If this question looks a little bit weird, sorry i don't know very much about python programming but i'm trying to improve myself.

  • 6
    I think you are looking for a [return statement](https://docs.python.org/3/reference/simple_stmts.html#the-return-statement). – khelwood Oct 08 '21 at 18:48
  • 1
    You need to read a tutorial on Python functions, it will show how to do this. – Barmar Oct 08 '21 at 18:50
  • Use `return` instead of `my_func() =`. – Samwise Oct 08 '21 at 18:51
  • What you are asking doesn't make sense. Can you please elaborate on what you are trying to accomplish? Create a simple example with an actual function – juanpa.arrivillaga Oct 08 '21 at 18:51
  • Why do you think you need functions in the if statement in this case instead use return – Papis Sahine Oct 08 '21 at 18:54
  • Welcome to Stack Overflow. Please read [ask] and note that you [are expected to do some research first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), including [learning the fundamentals of the language](https://docs.python.org/3/tutorial/index.html). – Karl Knechtel Oct 08 '21 at 19:13
  • I can literally [copy and paste your question title into a search engine](https://duckduckgo.com/?q=How+to+set+a+value+to+function+in+python) and get multiple relevant results ([1](https://www.w3schools.com/python/gloss_python_function_return_value.asp), [2](https://stackoverflow.com/questions/20309581/how-to-set-the-return-of-a-function-to-a-variable-in-python), [3](https://www.datacamp.com/community/tutorials/functions-python-tutorial)), *even though it didn't use the right words for the concept*. You could try many other queries as well, like `python get data from function`. – Karl Knechtel Oct 08 '21 at 19:13
  • Your final if statement is poorly constructed. – RufusVS Oct 08 '21 at 19:43

2 Answers2

3

is this what you want to do ??

def my_func(x,y):
   if x > y:
       return x**y + y**x
   else:
       return  y*x

result = my_func(4,5)
sbabti zied
  • 716
  • 4
  • 15
0

You have to return the ouput from the function when you have performed the necessary actions according to your inputs parameters. You can do it like below.

 def my_func(x, y):
    if x > y:
        output = x ** y + y ** x
    else:
        output = y * x
    return output


print(my_func(2, 3))
print(my_func(3, 2))