4

I'm starting Comp Sci courses in Uni this coming fall (starting with zero programming knowledge), so I'm just starting to play around programming. I'm following a book and tried copy-pasting some code - but it doesn't work. Here's what I tried:

>>> def function(x):
    return x+2
function(2)
SyntaxError: invalid syntax

The word "function" was highlighted. I'm confused because the very same example is used in the book and it appears to work but then I get that error on my end. What's going on here?

2
  • 1
    Good luck with your efforts! Knowing where to ask for help is a good start for anything, and Stack Overflow has lots of help available. I recommend reading existing questions to see how other people are using the language and to get a sense for how Python code looks. Commented Aug 10, 2012 at 1:12
  • I'll recommend here using mostly the internet to teach yourself programming. Go for docs.python.org and work through the tutorial there. It's how I learned, and I suspect many others who frequent this site. Commented Oct 4, 2012 at 2:27

4 Answers 4

6

You need to separate the function definition from its execution. Also, Python is sensitive to whitespace at the beginning of lines. Try this (exactly):

def function(x):
    return x+2
function(2)

or, in one line (which you should not do; see the style guidelines):

def function(x): return x+2; function(2)

or, in the Python shell:

>>> def function(x):
    return x+2

>>> function(2)
4

Note the blank line between the function definition and its use. After you define the function, hit enter once to get the prompt back.

Sign up to request clarification or add additional context in comments.

Comments

3

I'm assuming you meant to put Python in the title. Python has interesting syntax rules in that it actually counts white space as meaningful when parsing the program. What I mean is that having extra space, newlines, or tabs, etc. actually changes the meaning of the program. Double check the book example and make sure you have the exact same (tabs, new lines, and all) syntax written. It may look closer to this:

def f(x):
    return x + 2

note the new line and tab. To call this function, on a separate line say:

f(5)

or replace 5 with whatever parameter you want.

edit:

so the full script should be:

def f(x):
    return x + 2

f(2)

1 Comment

yes exactly like that but it highlights "f" as syntax error. i don't indent "f(5)" but it looks like that's how its supposed to be, all other spaces are properly placed as well,.. gahh this is crazy lol
2

Try this:

def function(x):
    return x+2
function(5)

In python, indentations are important. They are the {} of the python world.

You actually do not need to add extra whitespace before function(5) because python knows not to include it in the function definition because it is not indented. It is still good practice to add the extra blank line, but it is not strictly necessary.

1 Comment

You are right in the case of a python script, but the >>> in the question indicates that the asker is executing statements in a REPL. In this case, a blank line is necessary to end the previous statement and begin a new statement before using the function.
0

This is for the users using Python 2.6.6 and IDLE 2.6.6.

Since Python interpreter is very much sensitive to whitespace and indentations, we need to separate the function declaration from the execution.

What you must be doing:

>>> def square(input):
    output=input*input
    return output
print square(5)

Output: SyntaxError: invalid syntax

Correct way to do it:

>>> def square(input):
    output=input*input
    return output

>>> print square(3)
9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.