I am trying to get my function to take two arguments, and return their sum. Am I going about this the right way? This is what I have so far:
def my_sum(a, b):
sum = a + b
def main():
a = input(int("enter a number: ", a)
b = input(int("enter a number: ", b)
sum = a + b
return sum
print(" result: ", sum)
main()
So it looks good, but the main problem is that you aren’t actually calling your function 🙂 Once you get your two numbers, you can then make the call to your function (which you have properly set up):
Now, one other thing you’ll have to do is modify your function to return a value. You’re already almost there – just add a return (note that since you are just returning the sum of the two numbers, you don’t have to assign it to a variable):
This now means that when you run
s = my_sum(a, b), your function will return the sum of those two numbers and put them intos, which you can then print as you are doing.One other minor thing – when you use the setup you are (with
def main(), etc.), you usually want to call it like this:At this stage, don’t worry too much about what it means, but it is a good habit to get into once you start getting into fun stuff like modules, etc. 🙂