Artificial Intelligence

Module 2.12 : Functions in Python

Introduction

In programming, developers often need to use the same block of code multiple times. Writing the same code repeatedly increases complexity, reduces readability, and makes programs difficult to maintain.

Python solves this problem using Functions.

Functions allow programmers to organize code into reusable blocks that perform specific tasks.

Functions are widely used in Artificial Intelligence, Machine Learning, Data Science, Web Development, Automation, and Software Development.

In this tutorial, you will learn Functions in Python, their syntax, parameters, arguments, return statements, lambda functions, and practical applications.


Learning Objectives

  • Understand functions in Python.
  • Learn built-in functions.
  • Create user-defined functions.
  • Use parameters and arguments.
  • Understand return statements.
  • Learn lambda functions.
  • Apply functions in real-world programs.

What are Functions in Python?

A function is a reusable block of code designed to perform a specific task.

Instead of writing the same code repeatedly, programmers can place code inside a function and call it whenever required.

Example without function:

print("Welcome")
print("Welcome")
print("Welcome")

Using a function:

def message():
    print("Welcome")

message()
message()
message()

Output:

Welcome
Welcome
Welcome

Why Functions are Important

Functions are important because they improve code organization, reusability, and readability.

Functions help programmers:

  • Reduce code repetition.
  • Organize large programs.
  • Reuse logic easily.
  • Improve debugging.
  • Increase maintainability.

Functions are heavily used in AI applications, machine learning models, and automation systems.


Built-in Functions in Python

Python provides many pre-defined functions called built-in functions.

Examples:

  • print()
  • input()
  • len()
  • type()
  • sum()
  • max()
  • min()

Example

numbers = [10,20,30]

print(len(numbers))
print(sum(numbers))

Output:

3
60

User-Defined Functions

Functions created by programmers are called user-defined functions.

Python uses the def keyword to define functions.

Syntax

def function_name():
    statement

Example

def greet():
    print("Hello Python")

greet()

Output:

Hello Python

Function Parameters

Parameters allow functions to accept input values.

Example

def greet(name):

    print("Hello", name)

greet("John")

Output:

Hello John

Here, name is the parameter.


Function Arguments

Arguments are actual values passed into a function.

Example

def square(number):

    print(number * number)

square(5)

Output:

25

Here, 5 is the argument.


Default Parameters

Python allows default parameter values.

Example

def country(name="India"):

    print(name)

country()
country("USA")

Output:

India
USA

Return Statement

The return statement sends a value back from a function.

Example

def add(a,b):

    return a+b

result = add(10,20)

print(result)

Output:

30

Return statements are extremely important in Artificial Intelligence and Data Science functions.


Functions Returning Multiple Values

Python functions can return multiple values.

Example

def calculation(a,b):

    return a+b,a-b

x,y = calculation(10,5)

print(x)
print(y)

Output:

15
5

Lambda Functions

Lambda functions are small anonymous functions.

They are useful for short operations.

Syntax

lambda arguments: expression

Example

square = lambda x:x*x

print(square(4))

Output:

16

Recursive Functions

A recursive function calls itself repeatedly until a condition is met.

Example

def countdown(n):

    if n==0:
        return

    print(n)

    countdown(n-1)

countdown(5)

Output:

5
4
3
2
1

Functions in Artificial Intelligence

Functions are heavily used in Artificial Intelligence systems.

Examples:

  • Feature Extraction Functions
  • Model Training Functions
  • Prediction Functions
  • Data Cleaning Functions
  • Evaluation Functions

Machine Learning libraries such as NumPy, Pandas, TensorFlow, and Scikit-Learn rely heavily on functions.


Real-World Examples

Salary Calculator

def salary(hours,rate):

    return hours*rate

print(salary(8,500))

Output:

4000

Student Result Example

def result(marks):

    if marks>=40:
        return "Pass"

    return "Fail"

print(result(65))

Python Example

def multiply(a,b):

    return a*b

answer = multiply(5,4)

print(answer)

Output:

20

Interview Questions

1. What is a function in Python?

A function is a reusable block of code used to perform a specific task.

2. Which keyword creates functions?

The def keyword.

3. What is a parameter?

A parameter is a variable defined inside a function declaration.

4. What is a return statement?

The return statement sends values back from a function.


Assignment

  1. Create a function to add two numbers.
  2. Create a function that prints student information.
  3. Use default parameters in a program.
  4. Create a lambda function for multiplication.
  5. Create a recursive countdown function.

Quiz

Q1. Which keyword defines a function?

  • A. func
  • B. return
  • C. def
  • D. lambda

Answer: C. def

Q2. Which statement sends values back from a function?

  • A. print
  • B. return
  • C. break
  • D. import

Answer: B. return

Q3. What is a lambda function?

A lambda function is a small anonymous function.


Summary

In this tutorial, you learned Functions in Python. You explored built-in functions, user-defined functions, parameters, arguments, return statements, lambda functions, and recursion.

Functions are extremely important in Artificial Intelligence, Machine Learning, Data Science, and software development because they improve code organization and reusability.

Next Tutorial

Tutorial 18: Exception Handling

“`

Leave a Reply

Your email address will not be published. Required fields are marked *