# How functions are executed in memory?

In this blog, I am going to walk you through, from the moment you call a function to when it finish its execution what actually happening under the hood. When I learned about this topic, I have made detailed notes to help myself understand this better, and now I’m excited to share my knowledge with you in this blog,

Let’s take an example of a function which returns whether a given number is **odd** or **even**.

```python
def check_num(num):
    """This function returns whether a given number is odd or even"""
    
    if num %2 == 0:
        return 'Even'
    else:
        return 'Odd'

print(check_num(7))
```

*Now, let’s understand how above function gets executed in memory step by step,*

## Step 1: Defining the Function

When python reads this line,

```python
def check_num(num):
```

Python doesn’t starts executing **check\_num()** function yet, instead it store the function object in memory, just like saving a recipe for later use. At this point, Python knows there’s a function **check\_num()** which takes a parameter **num**, but yet python doesn’t run the code written inside our function.

> Think of it as, installing an app in your mobile phone, when installed, an icon appears on your home screen. That icon is like **defining the function**, it’s ready to run, but the app only starts when you *click* the app icon, just like **calling the function**.

![](https://cdn.hashnode.com/uploads/covers/65b3562cf1af4991e66cc599/d9137f2a-69df-4943-8240-7f5b120d2fff.png align="center")

## Step 2: Calling the Function

Now, python directly reach to last line,

```python
print(check_num(7))
```

Here, we are calling our function **check\_num()**, passing **7** as an argument. When the function call happens, python creates a new **box** (separate memory block) for **check\_num()** also know as **stack frame**.

Inside this stack frame, python stores the pass argument like this,

![](https://cdn.hashnode.com/uploads/covers/65b3562cf1af4991e66cc599/d5b13554-e543-4f86-865c-9bc47fba787b.png align="center")

Now, the **check\_num()** function code will be executed **independently** inside this created stack frame.

You might have question in you mind,

> 🤔 **Why does Python create an separate stack frame for a function, instead of just running it in global frame?**

Suppose your computer **RAM** as a **city** then,

*   The **global frame** where your main program runs, **a house inside this city**.
    
*   Then, every time when you call a function, python creates a separate **room inside the house** (stack frame).
    
*   Now, this **room** has its **own variables**, separate from rest of house.
    

> **Note:** If we call more functions, then python will create more rooms, each with their own separate memory block (stack frame)

## Step 3: Executing the Function

Once the **check\_num()** function is called, now python begins to executing inside code line by line inside the new created separate stack frame.

```python
   if 7 % 2 == 0:
        return 'Even'
    else:
        return 'Odd'
```

Since, **7% 2** not equal **to 0**, the condition becomes **false**. So python, skips to **else** block and **return** **'odd'** back to main program.

![](https://cdn.hashnode.com/uploads/covers/65b3562cf1af4991e66cc599/981f84a4-0939-4613-b5ed-2638019d87ff.png align="right")

At this point, the **check\_num()** finished executing and **returns** **'Odd'** to global frame, so python destroys the stack frame created for this function and also the local variables inside the frame, **num** deleted from the memory.

![](https://cdn.hashnode.com/uploads/covers/65b3562cf1af4991e66cc599/b21c1399-f8eb-4e27-b876-494dfaeab2e4.png align="center")

* * *

<details data-node-type="hn-details-summary">
<summary>💡 Interview Question</summary>
<ol><li><p><strong>What's the lifespan of a function in memory? Answer:</strong> A function becomes active in memory (stack frame) only during the time the function is called and until it returns a value. Outside of this period, the function does not exist in memory.</p></li><li><p><strong>What about variables lifespan inside a function?</strong> <strong>Answer:</strong> Variables defined inside a function, like num, only exist during the function execution. Once the function execution finishes, these inside variables are automatically destroyed from memory.</p></li></ol>
</details>

* * *

## Step 4: Printing the Result

Finally, the **print(check\_num(7))** statement becomes, **print('Odd')** and the output printed on the screen,

![](https://cdn.hashnode.com/uploads/covers/65b3562cf1af4991e66cc599/b239ffb9-ce9d-400a-ba42-a0d3719a3432.png align="center")

And, this is how your written function code gets executed in memory, from **definition**, to **call**, to **memory allocation**, to **execution** and finally **printing the result**.

Thank you for your time.
