Function and Pass Statement

Python tutorial · PySpark.in

Functions in Python

Functions are one of the most important building blocks in Python programming.They help you organize your code, make it reusable, and improve readability.A function is a block of code that performs a specific task.

Why Use Functions?

1.Avoid repetition of code
2.Make programs easier to read and debug
3.Divide large problems into smaller parts
4.Reuse code efficiently

Defining a Function in Python

You can define a function using the def keyword.

PLAINTEXT
1def greet():
2 print("Hello, welcome to Pyspark!")
▶ Output will appear here.

Here:

Calling a Function

To execute the function, just call it by its name followed by parentheses:

PLAINTEXT
1greet()
▶ Output will appear here.
PYTHON
1def greet():
2 print("Hello, welcome to Pyspark!")
3greet()
▶ Output will appear here.

Function with Parameter

You can pass values (called arguments) to functions.

PYTHON
1def greet_user(name):
2 print("Hello", name, "!")
3greet_user("Rohan")
▶ Output will appear here.

Function with Return Value

A function can return a value using the return keyword.

PYTHON
1def add_numbers(a, b):
2 return a + b
3result = add_numbers(10, 5)
4print("Sum:", result)
▶ Output will appear here.

Default Arguments

You can assign a default value to parameters.

PYTHON
1def greet_user(name="Guest"):
2 print("Hello", name)
3greet_user()
4greet_user("Riya")
▶ Output will appear here.

Keyword Arguments

You can call functions using parameter names (order doesn’t matter).

PYTHON
1def student_info(name, course):
2 print("Name:", name)
3 print("Course:", course)
4student_info(course="MCA", name="Rohan")
▶ Output will appear here.

Variable-Length Arguments

When you don’t know how many arguments will be passed, use:

PYTHON
1def show_marks(*marks):
2 print("Marks:", marks)
3show_marks(90, 85, 78)
4def show_details(**info):
5 print("Student Info:", info)
6show_details(name="Megha", course="MCA")
▶ Output will appear here.

Nested Functions

A function can also be defined inside another function.

PYTHON
1def outer():
2 def inner():
3 print("Inside Inner Function")
4 inner()
5 print("Inside Outer Function")
6outer()
▶ Output will appear here.

The Pass Statement in Python

Sometimes, while writing your code, you may want to define a function but not write its body immediately.However, Python does not allow empty function definitions — it will show an error if you leave the body blank.That’s where the pass statement comes in!

What is the pass Statement?

The pass statement is a null operation — it means “do nothing.”
It acts as a placeholder for future code so that your program runs without errors.

Syntax

PLAINTEXT
1def function_name():
2 pass
▶ Output will appear here.

This code will not produce any error, even though the function body is empty.

Why Use pass?

Example

PYTHON
1def login_user():
2 pass # TODO: Add login logic later
3def register_user():
4 pass # TODO: Add registration logic later
▶ Output will appear here.

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges