Making a Function in Python that converts centimeters into inches


Functions in Python are very convenient.

The tasks that are to be performed multiple times in Python programming can become very tedious. Fort this reason 'functions' are employed. 

We simply put some repetitive task into a function and after that, we have just to recall the defined function and the result will be thrown at us in an instant. That is the beauty of python functions.

Below we have defined a function for converting centimeters into inches. Now we don't need to write the formula again and again in our code to convert centimeters into inches. We just have to call the function and put our value in centimeters and the same function will convert it into inches. That's all!

Syntax of Functions:

def ----------> define

inches--------> function name

()--------------> parameter that takes an argument

return ---------> keyword in python that returns the result

formula---------> the body of function


Centimeters into Inches Function Definition

def inches(cm):
    return 1/2.54 * cm

a = inches(7)
print(a)


Now, you can just enter the value in the called function and it will return the result in inches. In the above example, 7 centimeters is being converted into inches and printed on the terminal.

That's it for now!

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes