Skip to main content

Functions

Local Variable

What are Local Variables?

Let us assume, you keep your savings in a secret box and lock it. Now, a person comes and tries to take money from that box. Obviously, he/she won't be able to do that. ( Don't tell me what if they break the box or if they are professional thieves. Exclude these cases please 😅 ).

In programming, this is like having a variable (money) inside a function (secret box). The variable is like a memory box that stores information. But, if the variable is inside a function, you can't see it from outside that function. Was the analogy clear?? If not let's see one more example.

Suppose you keep your personal information(bank account password, number of assignments copied, cheated answers in exam) in a function namely PersonalInfo.This function PersonalInfo will have bank_account_password, copied_assignments and cheated_answers as variables. These variables would be accessible to this function only (They should be, otherwise imagine what will happen 🙂 ). These variables will be local to this function.

Let's look at a simpler code example:

#include <iostream>
using namespace std;
 
void AddNumbers(int num1, int num2) {
    int result = num1 + num2;
    cout << result;
}
 
int main() {
 
    AddNumbers(2, 5);
    return 0;
}

Easy, right? The output will be 7.

Now, instead of printing the result variable from inside the function, what will happen if we print the result variable after the function call?

#include <iostream>
using namespace std;
 
void AddNumbers(int num1, int num2) {
    int result = num1 + num2;
}
 
int main() {
 
    AddNumbers(2, 5);
    cout << result;
 
    return 0;
}

In this code, you will get an error.

error: 'result' was not declared in this scope

That's because the result is like a secret inside the AddNumbers function and you can't see inside from outside. The computer wants to keep things tidy and organized! In the above example, the result variable is used to store the sum of num1 and num2 inside the AddNumbers function. However, once the function finishes its execution, C++ forgets about the result variable because it's local to that function.

When we create variables inside a function those variables are considered local to that function. It means they only exist and have meaning within that specific part of the code. When the execution of the function ends, all the variables inside it are destroyed.

Now, let's think about how can we resolve this issue. Have you read the return statements article?? If yes, great ✌️.We can solve this issue by using a return statement. This way, the result is not just stored locally; instead, the function communicates the result back to the part of the code that called it.

Let's try to understand this with the help of code.

#include <iostream>
using namespace std;
 
int AddNumbers(int num1, int num2) {
    int result = num1 + num2;
    return result;
}
 
int main() {
 
    int output = AddNumbers(2, 5);
    cout << output;
 
    return 0;
}

When we are using the return statement, we are not using the local variable directly but we are returning the value to the function call. In the main function, the returned value(i.e. result) is stored in the output variable, which can then be printed or used further.

When a function finishes its job, the variables it uses are cleared from memory, ensuring that your program stays efficient and avoids unnecessary complications and confusion.

Why do we need Local Variables?

Curiosity takes you a long way, especially in programming it makes the concepts interesting and also enhances the understanding. So, Always ask Why.

Local variables are like rough notes the function uses while doing its task, and once the task is finished, the notes are thrown away.

These variables are important because they keep code tidy and prevent confusion. They make the functions independent. When you want to use a function, you only need to know what information it needs (like the arguments you give it) and what it gives back (like the result it returns). You don't have to worry about the details inside the function, and it won't mess with anything outside of its job.

So, local variables help in keeping functions neat and focused on their specific tasks, making your code easy to understand and use.

Quiz

See you in the next article with more energy and excitement ✌️ till then Happy Learning !! 😀