Functions
A function takes an input, performs a calculation, and produces an output. Functions are one of the basic building blocks of mathematics and are used everywhere in AI.
A function takes an input and transforms it into an output.
Think of a function like a machine. You give it an input, the function performs a calculation, and you get an output.
What Is a Function?
A function is a rule that tells us how to convert an input into an output.
A simple way to think about it is:
Input ↓ Function ↓ Output
For example, imagine a function that doubles a number.
Input = 5 Function = multiply by 2 Output = 10
So the function takes 5 and produces 10.
A Simple Mathematical Function
We can write the same idea mathematically.
f(x) = 2x
Here:
- f represents the function.
- x represents the input.
- 2x tells us what calculation to perform.
If the input is 5:
f(5) = 2 × 5
= 10
Therefore, the output is 10.
Think of a Function Like a Machine
One of the easiest ways to understand a function is to imagine a machine.
FUNCTION MACHINE Input ──────→ [ × 2 ] ──────→ Output 5 10
The machine has one job: multiply the input by 2.
So:
Input Calculation Output 2 2 × 2 4 5 5 × 2 10 10 10 × 2 20
The same function can be used with different inputs.
Functions in Python
Python also allows us to create functions.
def double(x):
return x * 2
print(double(5))
Output:
10
Here:
- double is the function name.
- x is the input.
- x * 2 is the calculation.
- return gives us the output.
Another Simple Example
Let's create a function that calculates the square of a number.
def square(x):
return x * x
print(square(3))
print(square(5))
print(square(10))
Output:
9 25 100
Mathematically, this function can be written as:
f(x) = x²
For example:
f(5) = 5² = 25
Function Input and Output
A function can have different inputs and produce different outputs.
Function: f(x) = x + 10
If the input is 5:
f(5) = 5 + 10
= 15
If the input is 20:
f(20) = 20 + 10
= 30
The rule stays the same. Only the input changes.
Why Are Functions Important in AI?
AI models receive information as input and perform mathematical operations to produce an output.
Input Data
↓
Mathematical Function
↓
Calculation
↓
Output / Prediction
For example, suppose an AI model receives information about a house:
House size = 1500 sq ft Bedrooms = 3 Location = 8
These values can be given to mathematical functions inside the model.
House Data
↓
Functions
↓
Mathematical Calculations
↓
Prediction
Predicted Price = ₹75,00,000
A real AI model is much more complicated than this example, but the basic idea is the same: inputs are transformed into outputs through mathematical operations.
Functions Can Be Combined
AI models usually don't use only one function. Multiple calculations can be performed one after another.
Input ↓ Function 1 ↓ Function 2 ↓ Function 3 ↓ Output
For example:
Input ↓ Multiply by 2 ↓ Add 5 ↓ Output
If the input is 10:
10 × 2 = 20 20 + 5 = 25 Final Output = 25
Neural networks perform many transformations like this, although the actual calculations involve vectors, matrices, weights, activation functions, and other mathematical operations.
Functions and Machine Learning
A machine learning model can be viewed as a mathematical function that learns useful parameters from data.
Input Data
↓
Machine Learning Model
↓
Prediction
For example:
Study Hours
↓
ML Model
↓
Predicted Exam Score
The model learns how the input values relate to the output values.
This is why understanding functions is important before learning derivatives, gradients, and gradient descent.
Function → Derivative → Gradient Descent
Functions are the starting point for the calculus concepts that come next.
Function ↓ How does the output change? ↓ Derivative ↓ How do many parameters affect the output? ↓ Gradient ↓ Which direction should we move? ↓ Gradient Descent ↓ Improve the AI model
So functions are not an isolated mathematical topic. They provide the foundation for understanding how AI models calculate and learn.
A function takes an input, performs a rule, and produces an output.
In AI, functions are used to transform input data into useful outputs and predictions. Understanding this idea makes derivatives, gradients, and gradient descent much easier to understand later.