Assignment 02 Yoheshwaran K
Assignment 02 Yoheshwaran K
Batch No :191
Name :Yoheshwaran K
E-mail :[email protected]
Ph No :6374577055
Gender :Male
College Name :Pavendar Bharathidasan College
of Engineering and Technology
1. What is function?
A function is a named sequence of statements that can be executed
as a single unit. It takes inputs, processes them, and produces an output.
The input to a function is known as the "arguments" or "parameters," and
the output is referred to as the "return value." Functions are defined with
a unique name, and when you want to execute that function, you "call" it
by using its name along with the necessary arguments.
sum_result = add_numbers(5, 3)
print(sum_result) # Output: 8
2. Difference between Built-in function and user define function
Built-in Functions.
Built-in functions are pre-defined functions that come as part of the
programming language's standard library or core. They are already
available for use and don't require additional coding to access them.These
functions are typically designed to perform common tasks or operations
that are frequently needed in programming.
Examples of built-in functions in Python include ‘print()’, ‘len()’,
‘str()’, ‘int()’, ‘max()’, ‘min()’, etc.
User-Defined Functions:
User-defined functions are functions that programmers create and
define themselves to perform specific tasks or operations that are not
available as built-in functions.These functions are defined by the
programmer to meet their unique requirements and are not part of the
language's standard library.User-defined functions help break down
complex tasks into smaller, manageable parts and promote code
reusability and readability.Programmers can create functions with
meaningful names that reflect the purpose of the functionality they are
implementing.
Here's an example of a built-in function ‘(len())’ and a user-defined
function in Python:
# Built-in function (len())
sample_list = [1, 2, 3, 4, 5]
length = len(sample_list)
print(length) # Output: 5
# User-defined function
def square(x):
return x * x
result = square(3)
print(result) # Output: 9
print(len(my_list)) # Output: 5
print(len(my_string)) # Output: 13
print(len(my_dict)) # Output: 3
```