0
#include <stdio.h>
#include <cs50.h>


int square (int x);


int main(void)
{
    int x = get_int ("x:");
    printf("%i\n", square(x));
}
int square (int x)
//here in this line which variable to use, like can I use x which also comes 
//in same main function or some other variable. Can anyone clarify this?

{
    return x*x;
}
1

There is a concept called scope, which is the range of code that a variable exists. (I'll leave it for the course material to explain it in detail.) Simply put, a variable has scope within the curly braces it is created, or if created as a parameter, within the function for which it is created as a parameter. It's also a common practice to use the same or similar names for vars in the calling code as the parameter in the called function, but they are still two completely different and independent variables. Their only relationship is that the value in calling code's var is copied to the called code's var.

In more practical terms a variable used in main, say foo, is totally unrelated and independent of a variable foo that may be a parameter or an internal variable in a function. The same rule applies between functions. Functions and main are (generally speaking) totally self contained blocks of code, unless they use something from outside of themselves.

When a parameter is passed from main to a function, or from one function to another function that it may call, the variable from the calling function is NOT used in the called function. Instead, a copy of the original value is copied into the parameter var that will be used by the called function, leaving the calling function's var of the same (or different) name untouched. When the called function ends, the called function's var will cease to exist and the calling function's var will remain untouched. This will become really obvious and important when you learn about recursive calls.

The reason for this, and for scope, is so that programmers can create var names for their blocks of code without having to worry if the name appears elsewhere in other functions. While functions, and even main, can typically be small (a few hundred lines or less), an entire program can be massive. Imagine that all var names were global and what it would be like trying to create unique variable names across a massive program with hundreds of thousands of lines of code, or if a globally variable is altered in multiple places for different purposes and those purposes worked against each other! So, variables have scope, the space where they exist, and that scope is kept reasonably small to avoid those issues.

There are few exceptions to this. The most significant one is the use of an actual global variable. A global var is available to main and all functions, and only one exists (unless you create a shadow variable, a bad practice that you can google.) If used anywhere in the program, any changes are applied to the single, original variable, and that variable persists throughout the program. Globals should ONLY be created when there is a clear and justifiable reason for doing so because of their program-wide scope and because they can be the source of bugs if used carelessly.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
0

it is convenient to read something about functions and scope rules, you are passing x as a variable to the square function, I do not understand what is the question, strictly the two x that appear in the program are different, only conceptually, the x of square is a copy of the x of main. We could have used any other name for the square x for example:

#include <stdio.h>
#include <cs50.h>


int square (int a);


int main(void)
{
    int x = get_int ("x:");
    printf("%i\n", square(x));
}
int square (int a)
//here in this line which variable to use, like can I use x which also comes
//in same main function or some other variable. Can anyone clarify this?

{
    return a*a;
}
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .