Learn JSX Expressions and Variables in React with Examples 2026
Functions are one of the most important concepts in Python
programming. They allow you to write reusable, organized, and efficient code
instead of repeating the same instructions multiple times.
In this tutorial, you will learn everything about Python
Functions, including:
A function is a reusable block of code designed to
perform a specific task. Instead of writing the same lines of code multiple
times, you can write them once inside a function and use that function whenever
needed.
Suppose you need to display:
Welcome to Web Designing Theory
100 times.
Without functions, you would have to write:
print("Welcome to Web Designing Theory")
print("Welcome to Web Designing Theory")
print("Welcome to Web Designing Theory")
...
This becomes lengthy and difficult to manage.
Instead, you can create one function.
def welcome():
print("Welcome to Web Designing Theory")
Now call it whenever needed.
welcome()
welcome()
welcome()
The same code executes multiple times without rewriting it.
This saves time and keeps your program organized.
Syntax
def function_name():
# Code
What is a String in Python? Complete Guide with Syntax, Examples
Start
│
▼
Define Function
│
▼
Call Function
│
▼
Execute Statements
│
▼
Return Result (Optional)
│
▼
End
Calling a function means executing the code inside a
function. Simply creating a function does not make it run. A function only
performs its task when you call it using its name followed by parentheses ().
Think of a function like a TV remote. Owning the remote
does not change the channel. You must press a button to make it work. In the
same way, defining a function only stores the instructions, while calling the
function tells Python to execute those instructions.
def greet():
print("Welcome to Web Designing Theory")
Output
Welcome to Web Designing Theory
In the above example:
You can call the same function as many times as you want without rewriting the code, making your programs shorter, cleaner, and easier to maintain.
Parameters are variables that receive values when a
function is called. They allow a function to work with different pieces of
information each time it runs. Instead of creating separate functions for
different values, you can use parameters to make one function flexible and
reusable.
def function(parameter):
print(parameter)
def welcome(name):
print("Welcome", name)
welcome("Priya")
Welcome Amit
Welcome Priya
Explanation
Parameters make functions more powerful because they let you write one function that can handle many different inputs.
Python while Loop Programs with Pattern Examples for Beginners 2026The return statement is used to send a value back from a
function to the place where the function was called. Unlike print(), which only
displays information on the screen, return gives the result back so it can be
stored in a variable, used in calculations, or passed to another function.
Think of a restaurant waiter. You place an order, the
kitchen prepares the food, and the waiter brings it back to your table.
Similarly, a function performs a task and the return statement brings the
result back to the caller.
def add(a,b):
return a+b
def add(a, b):
return a + b
print(result)
Output
30
Explanation
Using return makes functions more useful because the returned value can be reused elsewhere in your program.
The word anonymous means the function does not have a
traditional name like a normal function.
Lambda functions are useful for quick calculations and
are often used with functions such as map(), filter(), and sorted()
lambda arguments : expression
square = lambda x: x * x
Output
25
Explanation
Lambda functions make code shorter and are ideal for simple tasks. However, for larger or more complex operations, regular functions created with def are usually easier to read and maintain.
Recursion is a programming technique in which a function
calls itself to solve a problem. A recursive function continues calling itself
until it reaches a stopping condition called the base case.
Recursion is useful for solving problems that can be
divided into smaller versions of the same problem, such as calculating
factorials, searching directory structures, or working with tree-like data.
Imagine climbing down a staircase. You keep taking one
step at a time until you reach the ground floor. Each step is similar to the
previous one, and the process stops only when you reach the last step. This is
how recursion works.
Example
def countdown(number):
if number == 0:
print("Done")
else:
print(number)
countdown(number - 1)
Output
5
4
3
2
1
Done
Explanation
Without a base case, the function would keep calling
itself forever, eventually causing a Recursion Error because Python would run
out of memory allocated for recursive calls.
Variable Scope defines where a variable can be accessed
within a Python program. It determines whether a variable is available only
inside a function or throughout the entire program.
There are two main types of scope:
Local Scope
A local variable is created inside a function and can
only be used within that function.
def display():
message =
"Hello Python"
print(message)
Output
Hello Python
Explanation
The variable message exists only inside the display()
function. If you try to use it outside the function, Python will generate a
NameError because the variable is not available outside its local scope.
Global Scope
A global variable is created outside all functions and
can be accessed from anywhere in the program.
message = "Welcome"
print(message)
Output
Welcome
Explanation
Since message is defined outside the function, it has
global scope. The display() function can access and print it without any
problems.
Understanding variable scope helps prevent naming
conflicts, improves code organization, and makes programs easier to debug and
maintain. It is an essential concept for writing clean and efficient Python
applications.
1. What is a function in Python?
A function is a reusable block of code that performs a
specific task. It helps reduce code repetition and makes programs easier to
read, maintain, and debug.
2. What is calling a function in Python?
Calling a function means executing the code inside the
function by using its name followed by parentheses (). A function only runs
when it is called.
3. What are parameters in Python functions?
Parameters are variables defined in a function that receive values (arguments) when the function is called. They make functions flexible and reusable.
4. What is the difference between parameters and
arguments?
Parameters are the variables listed in the function
definition, while arguments are the actual values passed to the function during
a function call.
5. What is the return statement in Python?
The return statement sends a value back from a function
to the place where it was called. It allows the returned value to be stored in
a variable or used in other operations.
6. What is a Lambda function in Python?
A Lambda function is a small, anonymous function written
in a single line using the lambda keyword. It is commonly used for short and
simple operations.
7. What is recursion in Python?
Recursion is a programming technique where a function calls itself repeatedly until a stopping condition (base case) is reached.
8. What is variable scope in Python?
Variable scope determines where a variable can be
accessed in a program. The two main types are local scope and global scope.
9. What happens if a recursive function has no base case?
Without a base case, the function keeps calling itself
indefinitely until Python raises a RecursionError.
10. Why are Python functions important?
Python functions improve code reusability, reduce duplication, simplify debugging, and make programs more organized and maintainable.
1. What is a function in Python?
Answer:
A function is a named block of reusable code that performs a specific task.
Functions help organize code and avoid repetition.
2. Which keyword is used to define a function in Python?
Answer:
The def keyword is used to define a function.
Example:
def greet():
print("Hello")
Python Variables and Data Types Explained with Examples
3. How do you call a function in Python?
Answer:
A function is called by writing its name followed by parentheses.
Example:
greet()
4. What are parameters in Python functions?
Answer:
Parameters are variables that receive values when a function is called. They
allow the same function to work with different inputs.
5. What is the difference between print() and return?
Answer:
|
print() |
return |
|
Displays output on the screen |
Sends a value back to the caller |
|
Cannot be reused later |
Returned value can be stored and reused |
|
Mainly used for displaying results |
Mainly used for processing and calculations |
6. What is a Lambda function?
Answer:
A Lambda function is an anonymous function created using the lambda keyword. It
is used for simple, one-line operations.
7. What is recursion in Python?
Answer:
Recursion is a process where a function calls itself repeatedly until a
stopping condition (base case) is met.
8. What is a base case in recursion?
Answer:
A base case is the condition that stops the recursive function from calling
itself forever. Without a base case, Python raises a RecursionError.
9. What is variable scope?
Answer:
Variable scope defines where a variable can be accessed in a program. Variables
can have local scope (inside a function) or global scope (throughout the
program).
10. What is the difference between local and global
variables?
Answer:
|
Local Variable |
Global Variable |
|
Declared inside a function |
Declared outside a function |
|
Accessible only inside that function |
Accessible throughout the program |
|
Exists only during function execution |
Exists until the program ends |
Comments
Post a Comment