-1

I am trying to make a simple function that accepts 2 parameters, and adds them together using "+".

def do_plus (a,b):
    a=3
    b=7
    result = a + b
    print (result)

However, I get no value returned, the function is executed but no output is shown.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
apronedsamurai
  • 73
  • 1
  • 11
  • 2
    `return result` and you also need to call the function e.g. `my_calculation = do_plus(3, 7)` outside of the function. There's no point giving hard-coded values to `a` and `b` inside the function, you want to pass them as _arguments_. Also, everything that's supposed to be inside the function should be indented. – roganjosh Dec 09 '16 at 13:45
  • `from operator import add as do_plus` – Chris_Rands Dec 09 '16 at 13:58

3 Answers3

2

You're missing the indentation.

a=3
b=7
def do_plus (a,b):
    result =a+b
    print (result)
# and you have to call the function:
do_plus(a,b)

You probably want to separate logic from input/output, as so:

def do_plus(a, b):
    result = a + b
    return result

res = do_plus(3, 7)
print(res)
Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
2

try this:

def do_plus (a,b):
    print=a+b
do_plus(3, 7)

you can call your function "do_plus" passing parameters and print wath the function return

Attention to the "spaces" before result is important in python the identation of script

MadCat
  • 113
  • 1
  • 1
  • 13
1

It's hard to tell from your code because the indentation is off, but a simple addition function can be something like:

def addition(a, b):
  return a + b

You are accepting parameters a and b, but then assigning them values 7 and 3, so that no matter what, it will return 10.

Will
  • 4,299
  • 5
  • 32
  • 50