Functions
A function is a reusable block of code that performs a specific task. Instead of writing the same code multiple times, you define a function once and call it whenever needed.
Functions help organize code, avoid repetition, and make debugging easier.
Defining and Calling a Function
def greet(name):
return f"Hello, {name}!"
message = greet("AI Learner")
print(message) # Hello, AI Learner!Parameters and Return Values
- Parameters: Inputs to the function (inside parentheses).
- Return value: The output of the function (using
return).
def add(a, b):
return a + b
result = add(5, 3) # result = 8Why Functions Matter in AI
- Modularity: Break a large AI pipeline into small, testable functions (e.g., load_data, preprocess, train, evaluate).
- Reuse: Use the same data cleaning function across projects.
- Clarity: Give meaningful names to complex operations.
Example: A Simple Activation Function (ReLU)
def relu(x):
if x > 0:
return x
else:
return 0
print(relu(-5)) # 0
print(relu(3)) # 3Two Minute Drill
- Define a function with
def name(parameters): - Use
returnto send back a result. - Functions promote code reuse and readability.
- In AI, you’ll write functions for data processing, model training, and evaluation.
Two Minute Drill
- Define a function with
def name(parameters): - Use
returnto send back a result. - Functions promote code reuse and readability.
- In AI, you’ll write functions for data processing, model training, and evaluation.
Need more clarification?
Drop us an email at career@quipoinfotech.com
