0

I'm not understanding where I have to use a function and where I don't, for example, I tried to write this for the area of a rectangle and after hours of trying to figure out why I couldn't get it to execute properly I could just get rid of the very first line of code and it worked fine.

def area_rectangle(width,height):

    width=int(input("Enter the width of rectangle: "))
    height=int(input("Enter the height of rectangle: "))
    area=width*height
    print area

Thought I had to start it out like I did but it just wasn't working until I deleted the first line.

3
  • You are defining a function without anything inside. To get it working, you can add pass inside the function or indent your code to be inside of the function. Commented Aug 12, 2018 at 16:51
  • you may want to correct indentation. Commented Aug 12, 2018 at 16:52
  • 2
    First you need to understand how functions work... it makes no sense to have a width and a height parameter in that function. Commented Aug 12, 2018 at 16:53

3 Answers 3

3

Functions are a way of compartmentalizing code such that it makes it both easier to read and easier to manage. In this case, there are a few concepts you must understand before implementing functions to solve your problem.

Functions follow the format:

def functionName(): #this defines the function
    print("This is inside the function.") #this is code inside the function

functionName() #this calls the function

A few things to note:

  • code that belongs to the function is indented
  • in order for the function to execute, it first has to be invoked (i.e. called)

So your function aims to calculate the area of a rectangle using width and height variables. In order for your function to work you would first need to invoke the function itself and then remove the unneeded parameters as you are asking for them as input anyway. This would give you:

def area_rectangle():

    width=int(input("Enter the width of rectangle: "))
    height=int(input("Enter the height of rectangle: "))
    area=width*height
    print (area)

area_rectangle()

Another way to go about this would be to make use of parameters. Parameters are values passed to a function by the line of code that invokes them, and they are given within the parentheses:

def functionName (my_param):
    print (my_param)

fucntionName (my_param)

Using parameters to solve your problem would look something like this:

def area_rectangle(width, height):
    area=width*height
    print (area)


width=int(input("Enter the width of rectangle: "))
height=int(input("Enter the height of rectangle: "))

area_rectangle(width, height)

Another side note is on return values. Rather than printing the result of a function within the function itself, you can return it to the line that invoked it and then make use of it outside the function:

def area_rectangle(width, height):
    area=width*height
    return area


width=int(input("Enter the width of rectangle: "))
height=int(input("Enter the height of rectangle: "))

area = area_rectangle(width, height)

print ("The area is {}".format(area))

Functions are an essential part of Python, and I suggest reading some tutorials on them, as there is many more things you can do with them. Some good ones...

learnpython.org - Functions

tutorialspoint.com - Python Functions

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

Comments

0

First,

You should indent your code

Second,

Now to get your code working you should call the function area_rectangle()

Corrected Code

def area_rectangle():

   width=int(input("Enter the width of rectangle: "))
   height=int(input("Enter the height of rectangle: "))
   area=width*height
   print area
area_rectangle()

Indentation is the key for Python (there are no {} just indentation)

Refer python documentation

3 Comments

This doesn't answer the question.
Thanks. As I understand it, the question is when not how to use a function. Sure, the OP has some syntax/logic issues, but I think they are mainly interested in understanding what/why functions. Why did you remove the parameters from OP's code, for example? Why should OP bother going through all of the trouble to put their code in a function?
Then it would be wise to direct OP to Python language basics.right?
0

You cant executing your function because

  1. You are not invoking (calling) it at the bottom.

    def area_rectangle(width,height):
        width=int(input("Enter the width of rectangle: "))
        height=int(input("Enter the height of rectangle: "))
        area=width*height
        print area
    
    area_rectangle()
    
  2. You are passing the required argument "width and height" to the function "area_rectangle" which is meaningless because you are accepting them from the user within the function. just call the function to work.

    Functions are a group of statements which gives you the answer for your problem statement. In your case if you are writing it as a function then you can resuse this value "area_rectangle" anywhere you want. you don't need to write those lines again.

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.