[Tutor] function question

Steven D'Aprano steve at pearwood.info
Sat Jan 5 18:46:13 EST 2019


Hello David, and welcome!

On Sat, Jan 05, 2019 at 11:18:04AM -0500, David Lynch wrote:

[...]
> From what I've read about functions I should be able to define a function
> with 2 variables? And then I can add all of my code into that function by
> indenting it. 

So far so good! Here's an example of such a function:


def add(x, y):
    return x + y


Of course in real programs we wouldn't bother with such a trivial 
function, since it is easier to read and write "a + b" rather than 
"add(a, b)". But it illustrates the concept.


> And then shouldn't I be able to
> function(float(input("enter input: ")))
> function(float(input("enter input 2: ")))
> return(function)
> ??

No, nothing like this. You are calling the function twice, but each time 
only supplying a single argument when it needs two. And the "return" 
keyword is only legal inside functions.

I *strongly* recommend that you make use of one of Python's best and 
most powerful features: the interactive interpreter. There you can try 
out small snippets of code with ease, see the result of running code 
live, use Python as a calculator, etc. That's an excellent way of 
learning to use the language, and you can always ask for help here if 
you have questions.

Do you know how to start the interactive interpreter? That will depend 
on your operating system (Windows, Mac, Linux) but generally you will 
open a "terminal" or "console", which gives you a command-line. That 
will have a % or $ prompt where you type commands.

Type "python" and hit the ENTER key. You will then see something similar 
to this:

  Python 3.5.2 (default, Oct 12 2016, 10:47:40)
  [GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux
  Type "help", "copyright", "credits" or "license" for more information.

and then be presented with >>> the default Python prompt, where you type 
Python code. In my examples below, I use the custom prompt "py>" because 
I think it looks nicer :-)

If you can get to the point of seeing the Python prompt, you're ready to 
start running live Python code. At the prompt, type:

    12*23

and hit ENTER, and you should see the answer 276:

py> 12*23
276


Now type in your function. Notice that you need to indent the body of 
the function, and that the prompt changes to ... inside the body.


py> def add(x, y):
...     return x + y
...
py>
    

(Remember, don't type the "py>" or "..." prompts.)

Now you should be ready to try calling the function:


py> add(99, 7)
106


If you get to that point, then you're ready to go to the next stage: 
intentionally doing something wrong so you can see what errors to 
expect.

What happens if you enter:

    add(99)

at the prompt? What error do you get?

Once you've understood that, they you can start writing more serious, 
useful functions.

Good luck, and don't hesitate to ask if you have any further questions!



-- 
Steve


More information about the Tutor mailing list