What is a Function in Programming with Example: A Simple Guide

Logan Jul 13, 2026

In the realm of programming, a function is a fundamental concept that enables code reuse and enhances the modularity of an application. It's a block of organized, reusable code that performs a single, related action. Functions provide better modularity for your application and a high degree of code reusability.

Functions Explained for Beginners | Python Basics
Functions Explained for Beginners | Python Basics

They take inputs (parameters), perform specific tasks using those inputs, and return an output (optional). The beauty of functions lies in their ability to simplify complex tasks, making your code more readable and less repetitive. Now, let's delve deeper into the world of functions with some examples.

Function Pointers in C | Advanced Programming Made Easy
Function Pointers in C | Advanced Programming Made Easy

Defining and Invoking Functions

A function is defined using the 'def' keyword in Python. Let's define a simple function that greets a person:

Function Constructs
Function Constructs

<code> def greet(name): return f"Hello, {name}!" </code>

To invoke this function, we pass a name as an argument, like so: <code>print(greet("Alice"))</code>. This will output: "Hello, Alice!".

an image of a computer screen with the text c + + and other words on it
an image of a computer screen with the text c + + and other words on it

Arguments and Parameters

A function can accept inputs via arguments. Arguments are values we pass into a function when we call it, while parameters are variables that receiving values in a function definition.

Let's define a function that adds two numbers together:

How to Create a Function in JavaScript: Step-by-Step Guide 🚀
How to Create a Function in JavaScript: Step-by-Step Guide 🚀

<code> def add_two_numbers(a, b): return a + b </code>

Here, a and b are parameters. When we call this function with arguments, for instance: <code>add_two_numbers(5, 3)</code>, the output will be 8.

Default Arguments

What is Algorithm in Computer System? | IT System Notes Quick Guide and Cheat Sheet
What is Algorithm in Computer System? | IT System Notes Quick Guide and Cheat Sheet

Functions can also have default argument values. If no value is provided for the default argument when invoking the function, the default value is used.

Let's update our greet function to include a default greeting:

Essential Python Functions Every Developer Should Know 🐍
Essential Python Functions Every Developer Should Know 🐍
a poster with information about programming and how to use it in your home office or work space
a poster with information about programming and how to use it in your home office or work space
Functions in C++
Functions in C++
Input and Output Functions in C Programming – Complete Beginner's Guide
Input and Output Functions in C Programming – Complete Beginner's Guide
Master Python Functions – Write Smarter Code! 🐍✨
Master Python Functions – Write Smarter Code! 🐍✨
Functions in Python | Types, Example
Functions in Python | Types, Example
Functions in Python 🐍 Beginner friendly and easy to learn 🤩
Functions in Python 🐍 Beginner friendly and easy to learn 🤩
Function Overriding
Function Overriding
an open notebook with some type of function written in red on the top and bottom
an open notebook with some type of function written in red on the top and bottom
C Functions Cheat Sheet (Syntax, Types, Recursion & Pointers Explained)
C Functions Cheat Sheet (Syntax, Types, Recursion & Pointers Explained)
PHP Function (Guide With Examples)
PHP Function (Guide With Examples)
the differences between functional programming and functional programming are shown in this graphic above it is an example
the differences between functional programming and functional programming are shown in this graphic above it is an example
a table with words that are written in different languages, including the word's and numbers
a table with words that are written in different languages, including the word's and numbers
a poster with different types of programming instructions on it's back side, including the words
a poster with different types of programming instructions on it's back side, including the words
Operator Function in C++ Programming Language with Examples
Operator Function in C++ Programming Language with Examples
an image of the functions of c + + functions in web design and development, as well as text
an image of the functions of c + + functions in web design and development, as well as text
👉 C Recursion Explained | Easy Notes for Beginners 🔥
👉 C Recursion Explained | Easy Notes for Beginners 🔥
Computer Basics for Beginners | Meaning, Definition, Parts, Functions, Types & Uses
Computer Basics for Beginners | Meaning, Definition, Parts, Functions, Types & Uses
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
a diagram with the words programming basics on it
a diagram with the words programming basics on it

<code> def greet(name, greeting="Hello"): return f"{greeting}, {name}!" </code>

Now, if we call <code>greet("Bob")</code>, it returns "Hello, Bob!" and <code>greet("Charlie", "Hi")</code> returns "Hi, Charlie!".

Scopes of Variables

Variables defined inside a function have local scope, meaning they're only recognized within that function. variables defined outside any function have global scope.

Let's define a variable 'greeting' globally and use it within a function:

<code> greeting = "Howdy" def greet(name): print(f"{greeting}, {name}!") greet("Karen") # Outputs: "Howdy, Karen!" </code>

Modifying Global Variables

To modify a global variable inside a function, you need to declare it with the 'global' keyword.

For instance, let's increment the global greeting inside our greet function:

<code> greeting = "Howdy" def greet(name): global greeting greeting = "Hello" # Now, greeting is "Hello" print(f"{greeting}, {name}!") greet("Karen") # Outputs: "Hello, Karen!" </code>

In conclusion, functions are powerful tools that enable code reuse, improve modularity, and enhance the efficiency of any programming project. Mastering them is a must for every programmer. As you continue your programming journey, grasp the nuances of function behavior, and keep refining your function-building skills.