Reputation: 31
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.
Upvotes: 0
Views: 5969
Reputation: 736
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:
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...
tutorialspoint.com - Python Functions
Upvotes: 3
Reputation: 129
You cant executing your function because
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()
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.
Upvotes: 0
Reputation: 827
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
Upvotes: 0