# Why Do We Need Functions?

*By Waqas Hussain (GlitchWH) Computer System Engineering student specializing in Python and Cyber Security*

### Introduction

we were good without functions so far, but as the programs grow we just cant keep going on same logic to code again and again each time, for this purpose we use functions, functions saves our time, funtions are good for reusability. As programs grow, the same logic is needed in many places. Copy-pasting it works at first, then becomes messy and error-prone. A **function** is a named, reusable block of code that you write once and run whenever needed. In this article, we'll see why functions exist and trace exactly what Python does when it defines and calls one.

### Program Without function(s)

![](https://cdn.hashnode.com/uploads/covers/6a045b1b5418a55a522a6d57/f9cb7d3e-b6df-4ef0-821d-ee4229499312.png align="center")

too much mess right, each time i will have to repeat same logic again and again, even though the program is very small, but when we are writing program for complete systems, modules etc then repeating same logic will take about 20% extra of the program part.

*   we rewrite same logic again and again
    
*   it wastes our time
    
*   we are most probably open to make mistakes
    

we can implement same logic alot easier using functions

### Program with function(s)

![](https://cdn.hashnode.com/uploads/covers/6a045b1b5418a55a522a6d57/36782ba1-b0ba-42ed-9f45-718d75fa895d.png align="center")

![](https://cdn.hashnode.com/uploads/covers/6a045b1b5418a55a522a6d57/332491eb-f5a2-4d38-b4db-9c00015ecbb9.png align="center")

###   
Simple Breakdown

**The function definition** (def find\_area\_of\_rect(l, w)) Python creates a function object and attaches the name find\_area\_of\_rect to it. The body (return l \* w) does not run here. It is only stored for later.

**Taking the input** (input() and int()) input() shows the prompt and waits for you to type. It always gives back text, so "4" is a string, not a number. int() converts that text into a real integer, and the name (lenght) now points to the number instead.

**Calling the function** (find\_area\_of\_rect(lenght, width)) Python finds the function by name and passes your two values in as l and w. It runs l \* w in a temporary workspace that exists only during the call, then sends the result back. The result is stored in area1rect, and the temporary workspace and its l and w are deleted.

**Printing** (print(f"... {area1rect}")) Python builds the f-string first by inserting the value into the text. Then print displays the finished text.

**The last three lines** (area2rect = print(...)) Python works from the inside out: the function runs first, and displays the output for the passed inputs respectively

**Summary:** define the function, which is only stored. Take input and convert it to numbers. Call the function, which computes and returns a value. Print the results.

### Why This Matters

Knowing that `def` creates an object, that calls create frames, and that frames are discarded on return explains many things you'll meet later: why local variables vanish after a function ends, why functions can be passed around like values, and how decorators work.

### Conclusion

Functions exist to remove repeated code and make programs maintainable. Under the hood, Python compiles the body into a code object, `def` wraps it in a function object and binds a name to it, and each call creates a fresh frame, runs the body, returns a value, and discards the frame. The function is written once, but executed in a clean workspace every time.
