Student Summer Sprint
  • Welcome to Hackathons for Schools Student Summer Sprint!
  • Event Information
  • What is the Student Summer Sprint?
  • Useful Event Information and Links
  • Schedule
  • Our Organisers, Panelists and Mentors
    • Organisers
    • Panelists and Speakers
    • Mentors
  • Project Details
    • Themes
    • Judging Criteria and Winning Teams
    • Presentation Advice
    • Submission Checklist and FAQs
  • Coding Platform and GitLab
  • Technical Workshops
    • How to Use Git
      • Git and the Terminal
      • How to use Git - The Basics
      • Branching, Merging and Other Useful Commands
      • How to use Git - Further Resources
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • Web Development - Resources
    • Introduction to Python
      • Variables
      • Data Types and String Formatting
      • Input and Output
      • Conditional Statements
      • Functions
      • Libraries
      • CHALLENGES
        • Solutions
      • Learning Python - Resources
    • Discord Bots in Python
      • Discord Bots - Resources
    • Web Scraping in Python
  • Careers Advice and Opportunities
    • University Advice
      • University Advice - Further Resources
    • CV, Applications and Interviews
      • Creating a Great CV
      • UCAS Personal Statement Advice
      • Interview Hints and Tips
      • Ultimate LinkedIn Guide
      • Applications - Further Resources
    • Different routes into a tech career
      • Different Routes into Tech - Further Resources
    • What Now?
      • More Opportunities!
Powered by GitBook
On this page

Was this helpful?

  1. Technical Workshops
  2. Introduction to Python

Functions

Introduction to functions in Python.

A function is a block of organised, reusable code that is used to perform a single, related action.

Very similar to the functions you came across in maths, they take an input and return an output.

However, not every function has to take an input. Similarly, not every function has to return something. Some functions can even take multiple inputs!

An example you've already seen is the print() function.

print("Hello World!")

Functions improve code readability and code re-usability! They're super important to be familiar with as you become a better programmer.

Instead of:

x = 2
y = 6
z = 1

numberSum = x + y + z

You can do:

x = 2
y = 6
z = 1

def sumOfThree(a, b, c):
    numberSum = a+b+c
    print(numberSum)

sumOfThree(x, y, z)
sumOfThree(5, 10, 20)

The output of this program will be 9 and 35.

But let's say you don't want to print the result, you just want to return it as data from the function. That's fine too! Here, you can use the return statement in the function to return the data:

x = 2
y = 6
z = 1

def sumOfThree(a, b, c):
    numberSum = a+b+c
    return numberSum

result = sumOfThree(x, y, z)
print(result)

This program does the same as before, outputting 9, except using the return statement instead of outputting from inside the function.

PreviousConditional StatementsNextLibraries

Last updated 4 years ago

Was this helpful?