Python - Functions
Why should I learn to solve Python: Functions technical interview questions?
Learn and practise solving Python: Functions technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.
Where can I get technical Python: Functions technical interview questions and answers with explanations?
IndiaBIX provides you with lots of fully solved Python: Functions technical interview questions and answers with a short answer description. You can download Python: Functions technical interview questions and answers as PDF files or e-books.
How do I answer Python: Functions technical interview questions from various companies?
You can answer all kinds of Python: Functions technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Functions technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.
In Python, a function is a reusable block of code that performs a specific task. Functions are defined using the def
keyword, and they allow you to modularize your code by breaking it into smaller, more manageable pieces. Functions take input, called parameters, and may return output.
Example of a Simple Function:
def greet(name):
"""This function greets the person passed in as a parameter."""
print(f"Hello, {name}!")
# Calling the function
greet("Alice")
Output:
Hello, Alice!
In the example above:
- The function
greet
is defined with one parameter (name
). - Inside the function, a greeting message is printed using the
print
statement. - The function is then called with the argument
"Alice"
, and it prints the greeting for Alice.
Function with Return Value:
def add_numbers(a, b):
"""This function adds two numbers and returns the result."""
result = a + b
return result
# Calling the function and storing the result
sum_result = add_numbers(3, 7)
print("Sum:", sum_result)
Output:
Sum: 10
In this example, the function add_numbers
takes two parameters (a
and b
), adds them, and returns the result. The returned value is then printed.
Functions in Python promote code reuse, improve readability, and make your code more modular and organized.
The difference between a function definition and a function call lies in their roles and when they are executed in the code.
Function Definition:
def greet(name):
"""This function greets the person passed in as a parameter."""
print(f"Hello, {name}!")
In the example above, greet
is a function definition. It defines a function named greet
that takes a parameter name
and prints a greeting message.
Function Call:
# Calling the function
greet("Alice")
Here, greet("Alice")
is a function call. It executes the code inside the greet
function with the argument "Alice"
. This results in printing the greeting message for Alice.
Output:
Hello, Alice!
To summarize, a function definition is where you define the code that makes up the function, including its name, parameters, and the actions it performs. On the other hand, a function call is where you execute the function with specific arguments, causing the defined code to run.
In Python, you can define a function using the def
keyword. Below is an example of how to define a simple function:
def greet(name):
"""This function greets the person passed in as a parameter."""
print(f"Hello, {name}!")
# Call the function
greet("Alice")
In this example, greet
is the function name, and name
is a parameter that the function takes. The function prints a greeting message using the print
statement.
Output:
Hello, Alice!
You can also have functions without parameters or without a return
statement. Functions can perform various tasks and encapsulate reusable pieces of code.
The def
keyword in Python is used to define a function. It indicates the start of a function definition, followed by the function name and its parameters. Let's discuss the significance of the def
keyword with an example:
def greet(name):
"""This function greets the person passed in as a parameter."""
print(f"Hello, {name}!")
# Call the function
greet("Alice")
In this example, def
is the keyword used to define the function greet
. It is followed by the function name (greet
) and the parameter name
. The colon (:
) indicates the start of the function body.
The def
keyword is crucial for declaring functions in Python. It allows you to encapsulate a block of code, give it a name, and reuse it throughout your program. Functions are essential for code organization, readability, and reusability.
Let's consider another example where we define a function to calculate the square of a number:
def square(number):
"""This function calculates the square of a given number."""
return number ** 2
# Call the function
result = square(5)
print(f"The square of 5 is: {result}")
Output:
The square of 5 is: 25
Here, the def
keyword is used to define the function square
. It takes one parameter (number
) and returns the square of that number. The function is then called with the argument 5
, and the result is printed.
The def
keyword, in this case, is essential for creating a reusable block of code that can be invoked with different values.