0% found this document useful (0 votes)
8 views1 page

Python Function

A Python function is a reusable block of code that performs a specific task, allowing for code efficiency by avoiding repetition. Functions can take parameters and return values, enabling dynamic input and output. Examples include a simple greeting function and an addition function that returns the sum of two numbers.

Uploaded by

abhishek sapra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Python Function

A Python function is a reusable block of code that performs a specific task, allowing for code efficiency by avoiding repetition. Functions can take parameters and return values, enabling dynamic input and output. Examples include a simple greeting function and an addition function that returns the sum of two numbers.

Uploaded by

abhishek sapra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

A Python function is like a mini-program inside your main program.

It's a reusable
block of code that does a specific task. Instead of writing the same code over and
over, you put it in a function and just call it whenever you need it.

Here's the basic idea:


python
Copy
Edit
def greet():
print("Hello!")
def means “define a function”

greet is the name of the function

The code inside (indented) runs when you call the function like this:

python
Copy
Edit
greet()
Functions can also take inputs (called parameters):
python
Copy
Edit
def greet(name):
print("Hello, " + name + "!")
Now you can do:

python
Copy
Edit
greet("Alice")
greet("Bob")
Functions can also return a value:
python
Copy
Edit
def add(a, b):
return a + b
You can use it like this:

python
Copy
Edit
result = add(3, 5)
print(result) # This prints 8

You might also like