Menu
×
   ❮   
     ❯   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING

Functions

Functions are used to structure your code in a better way, so that your code becomes easier to read and to use.

Functions makes it possible to re-use the same code many times, which is a huge benefit.

What is a Function?

A function holds a piece of code that does a specific task.

A function takes some data as input, the code inside the function does something with the data, and then the result is returned.

Click the "Run" button below to see the function converting a temperature from Fahrenheit to Celsius.

functioncode57°Finput°Creturn

Below is how the Python code looks like for the convertToCelsius function:

defDefines thefunctionconvertToCelsius(Functionnamefahrenheit):Inputcelsius = (fahrenheit - 32) * 5 / 9returnCode insidefunctioncelsiusReturnvalue

The function above takes a temperature in Fahrenheit as input, converts it into Celsius, and returns the Celsius value as output.

Note: Functions can have different shapes and forms. Input and return are optional for example, but functions as explained here are how they usually appear, and how we normally think of them.


When Should I Use a Function?

If a part of your program does a specific task, you should create a function for it.

It is especially useful to create a function if you need to run that code more than once, and from different parts of your program.


Creating a Function

Before using a function, you need to create it.

Recipe for creating a function:

  1. Name the function.
  2. Define the input.
  3. Write the code inside the function, what you want the function to do.
  4. Define the return value.

Creating our convertToCelsius function looks like this:

def convertToCelsius(fahrenheit):
  celsius = (fahrenheit - 32) * 5 / 9 
  return celsius

Our function is named convertToCelsius. It takes fahrenheit as input, and returns celsius.

But to make the function run, we need to call it.


Calling a Function

To call a function you write its name together with the input, and that makes the function run.

After creating the convertToCelsius function, we can call it, converting 100°F into Celsius like this:

def convertToCelsius(fahrenheit):
  celsius = (fahrenheit - 32) * 5 / 9
  return celsius

print(convertToCelsius(100))
Run Example »

Running the example above, you can see we use print statements to see the result of the function call (the return value).

Imagine we have many temperature measurements we need to convert from Fahrenheit to Celsius.

Now that we have created the convertToCelsius function, we can just call that same function over and over again for each temperature in Fahrenheit we want to convert.

def convertToCelsius(fahrenheit):
  celsius = (fahrenheit - 32) * 5 / 9 
  return celsius 

print('Fahrenheit values 0, 20, 40, 60, 80, 100')
print('converted to Celsius:\n')

print(convertToCelsius(0))
print(convertToCelsius(20))
print(convertToCelsius(40))
print(convertToCelsius(60))
print(convertToCelsius(80))
print(convertToCelsius(100))
Run Example »

As you can see, functions can be called many times, and after making a function, we can use it knowing what it does, without having to understand how it does it.


The Benefits of Using Functions

The more programming you do, and the longer your programs get, the benefits from using functions become more and more obvious.

The benefits we get from wrapping code that does a specific task into a function are many.

Reusability: Write the code once, and reuse it as many times as you like, from different parts of your program. This saves time and effort, and you avoid repetition.

Simpler programs: Functions make it easier to break down complex problems into smaller, more manageable pieces. This way of solving a problem is called divide and conquer.

Readability: Creating functions for tasks, with names describing what the functions do, makes it easier to understand the code by reading it.

It is easier to understand what this line of code does:

convertToCelsius(60)

than this:

(60 - 32) * 5 / 9

Fixing errors: If there is something wrong with the code inside the function, we only need to change the code in one place, so the code becomes easier to maintain. Alternatively, without using a function, the code with the error in it would perhaps be repeated many times in many places, making the error harder to fix.

Collaboration: People can work together more easily when splitting the problem into functions that can be written separately. Functions create clear boundaries between parts of the program.

Testing: Functions can be tested independently to ensure they work correctly.

Scalability: Functions make it easier to expand and add new features to your programs.

Abstraction: Allows you to hide complex details and focus on what the function does instead of how it works.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.