hrishila guha
hrishila guha

Reputation: 19

Combining two pre defined functions

I was asked to create a function to give a square of a value. Here is the defined function:

 def square(x):
    print(x**2)

secondly I was asked to define another function to check weather the number is odd or even:

def oddeve(x):
    if x % 2 == 0:
     print ("{} is even".format(x))
    else:
     print ("{} is odd".format(x))

However now they want me to use both the function together to check whether the square is odd or even. Can anyone help me with how to combine two pre defined functions?

Upvotes: 0

Views: 82

Answers (1)

Aniketh Malyala
Aniketh Malyala

Reputation: 2660

You can simply call one function inside of the other function. However, this requires that you don't print out the value, but rather return it:

 def square(x):
    return(x**2)

Now you can simply call:

oddeve(square(x))

For example, if x = 4, square(4) returns 16, and oddeve(16) prints out that it is even.

Upvotes: 3

Related Questions