Showing posts with label Python Functions. Show all posts
Showing posts with label Python Functions. Show all posts

Creating a simple Function in python || python code for creating function

← Back to Home

๐Ÿ”น Functions in Python

A function is a block of code which only runs when it is called.

we can pass data, known as parameters, into a function.

A function can return data as a result.


# In Python a function is defined using the def keyword:

๐Ÿ”ธ Example : Creating a Function

def my_function():
  print("Hello function" )



๐Ÿ”ธ  Calling a Function

To call a function, we use the function name followed by parenthesis

 Example :


def my_function():
  print("Hello from function")

my_function()    # Here is Function call by its name


Python Functions Explained: A Beginner’s Guide with Code Examples

← Back to Home

๐Ÿ” Understanding Python Functions – A Beginner’s Guide

Functions are one of the most powerful and useful features in Python. They allow you to group code into reusable blocks, making your programs more organized, readable, and efficient.

Functions are the heart of clean, efficient Python programming. If you're a beginner, think of functions as named blocks of code that do something specific — like a recipe in a cookbook.


๐Ÿง  What is a Function?

A function is a reusable block of code that  that performs a specific task and runs only when called.  You define it once and call it whenever needed. 

You can pass data into it using parameters, and get a result back using the return keyword.

๐Ÿ“Œ Key Points:

  • A function is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a function.
  • A function can return data as a result.
  • In Python, a function is defined using the def keyword:

See example below:

def my_function():
  print("Hello from a function")

๐Ÿ”ง Basic Anatomy of a Function

def function_name(parameters):
    # code block
    return result

๐Ÿ‘️ Code Diagram

+---------------------------+
| def greet(name):         | <- Function Definition
|     print("Hello", name) | <- Function Body
+---------------------------+

        ⬇ Function Call

+--------------------+
| greet("Alice")     | <- Output: Hello Alice
+--------------------+

✅ Why Use Functions?

Functions help you:

๐Ÿงฉ Break complex tasks into manageable chunks
๐Ÿ” Reuse code instead of repeating
๐Ÿ“š Improve readability of your code
๐Ÿ› ️ Debug and maintain programs easily


✍️ Creating Your First Function

Let’s write a simple function:

def my_function():
    print("Hello from a function")

✍️ Defining and Calling a Function

Let’s write a above simple function again:

def my_function():
    print("Hello from a function!")

# Call the function
my_function()

๐Ÿ–ฅ️ Output:

Hello from a function!


๐Ÿงพ Function with Parameters

def greet(name):
    print(f"Hello, {name}!")
    
greet("Alice")

๐Ÿ–ฅ️ Output:

Hello, Alice!

๐Ÿ“Œ You can pass any data—strings, numbers, lists, etc.—as parameters.



๐Ÿ” Reusability at Work:

Want to greet different users? Use parameters.

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")
greet("Bob")

๐Ÿ–จ️ Output:

Hello, Alice!
Hello, Bob!

๐Ÿ”„ Returning Values from Functions

Some functions return a result:

def add(a, b):
    return a + b

sum = add(4, 6)
print(sum)

๐Ÿ–จ️ Output:

10


๐Ÿ”„ Function with Conditional Logic

def is_even(num):
    if num % 2 == 0:
        return True
    else:
        return False

print(is_even(4))  # True
print(is_even(5))  # False

๐Ÿ“Œ You can use functions to implement logic and decision-making.


๐Ÿงญ Flowchart: How a Function Works

[Start]
   ↓
Define Function → [Call Function?] — No → [End]
                          ↓ Yes
                    Execute Code Block
                          ↓
                     Return (Optional)
                          ↓
                        [End]

See the flowchart in picture below:




๐ŸŽฅ Watch and Learn

Want to see it in action?
๐Ÿ‘‰ Check out the video below: “What is a Function in Python” – ideal for visual learners!



๐ŸŽฏ Final Tip

Functions help you write clean, modular, and testable code. The more you use them, the better your Python skills will become!


๐Ÿš€ Your Turn

Now that you know the basics of Python functions, try creating your own! Whether it’s a calculator or a greeting app, functions will help you build smarter and cleaner code.


Featured Post

Extra Challenge: Using References Between Documents

  ๐ŸŽฏ ๐Ÿ’ก Extra Challenge: Using References Between Documents Here's an  Extra Challenge  designed to build on the original MongoDB mode...

Popular Posts