Funtion in Python

A functions is a named block of code that does something when you call it. Think of it like mini-program inside your main program. Why use functioning? To avoid repeating the same code To organize your code better To make your code reusable. Example 1: A simple function def say_hello(): print("Hello, friend!") To run this function: say_hello() Example 2: Function with input def greet(name): print("Hi", name) greet("Blackmare01wolf") # Output: Hi Blackmare01wolf Example 3: Function that gives back a result def add(a, b): return a + b total = add(5, 10) print(total) # Output: 15 Use def to define a function. Use return when you want to give back a value.

May 4, 2025 - 05:28
 0
Funtion in Python

A functions is a named block of code that does something when you call it. Think of it like mini-program inside your main program.

Why use functioning?

  • To avoid repeating the same code
  • To organize your code better
  • To make your code reusable.

Example 1: A simple function

def say_hello():
     print("Hello, friend!")

To run this function:

say_hello()

Example 2: Function with input

def greet(name):
     print("Hi", name)
greet("Blackmare01wolf") # Output: Hi Blackmare01wolf

Example 3: Function that gives back a result

def add(a, b):
     return a + b
total = add(5, 10)
print(total) # Output: 15

Use def to define a function. Use return when you want to give back a value.