coding180 icon
Coding180
Introduction to Python Functions, What is a function and why do we use them?, The syntax for definin

Introduction to Python Functions

Home > Tutorials > Python for beginners > Functions


Programming languages are all about making life easier, faster, and more efficient. As you learn more about programming, you will notice that there are certain tasks that you need to do over and over again.
Think of printing a message to the screen or calculating the area of a rectangle. These tasks can be repetitive and tedious, and they take away from the really interesting part of programming.

This is where functions come in handy. A function is a block of code that you can reuse over and over again, without having to write it every time.

In other words, a function is like a mini-program within a larger program. It has a specific purpose, it takes some input (in the form of arguments), and it produces some output (in the form of a return value).

Why Use Functions?

Using functions has several advantages:

  • Reuse - You can reuse code instead of rewriting it every time.
  • Organization - Breaking down a large program into smaller functions makes it easier to read and understand.
  • Testing - Functions make it easy to test small pieces of code before combining them into a larger program.
  • Abstraction - Functions hide implementation details, allowing you to focus on what the function does, rather than how it does it.

For example, let's say you need to calculate the area of a rectangle multiple times in your program. Instead of writing the same code over and over again, you can define a function that calculates the area for you. This saves you time and makes your code more readable.

How to Define a Function

To define a function in Python, you use the def keyword followed by the name of the function, parentheses containing any arguments (separated by commas), and a colon. Then you write the code that makes up the function, indented under the function definition.

Here is an example of a function that takes two arguments (name and age) and prints them to the screen:

def greet(name, age):
    print(f"Hello {name}, you are {age} years old!")

To call this function, you simply write its name followed by parentheses containing the arguments:

greet("John", 25)
# Output: Hello John, you are 25 years old!

How to Call a Function

To call a function in Python, you simply write the name of the function followed by parentheses containing any arguments (if necessary). For example, let's say we have a function that adds two numbers together:

def add_numbers(x, y):
    return x + y

We can call this function like this:

result = add_numbers(2, 3)
print(result)
# Output: 5

Exercises

  1. Write a function that takes a string as an argument and prints each character on a new line.
  2. Write a function that calculates the area of a circle with a given radius (area = pi * r ** 2). Use math.pi for the value of pi.
  3. Write a function that takes a list of numbers and returns the sum of all the even numbers in the list.

In conclusion, functions are an essential part of programming in Python. They allow you to reuse code, organize your program, and abstract away implementation details. With practice, you will become comfortable defining and calling your own functions.


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel