Variables, Parameters, Arguments: Difference between revisions
(66 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Internal= | =Internal= | ||
* [[ | * [[Programming_Languages_Concepts#Variables.2C_Parameters.2C_Arguments|Programming Language Concepts]] | ||
* [[Python_Language_Functions#Function_Parameters|Python Function Parameters]] | * [[Python_Language_Functions#Function_Parameters|Python Function Parameters]] | ||
* [[Java_Generics_Concepts#Type_Parameters|Java Generics Concepts | Type Parameters]] | * [[Java_Generics_Concepts#Type_Parameters|Java Generics Concepts | Type Parameters]] | ||
=Identifier (Name)= | |||
An identifier is a name given to program element, for example to a [[#Variable|variable]]. In this case, the identifier and the variable are equivalent. Identifiers are also used for functions, classes, labels, and many more elements. Identifiers must differ in spelling and case from the language keywords, or reserved words. | |||
=<span id='Keyword'></span><span id='Reserved_Word'></span>Keyword (Reserved Word)= | |||
Reserved words across languages: | |||
* [[Python_Language#Reserved_Words|Python reserved words]] | |||
* [[Go_Language#Keywords|Go keywords]] | |||
=Variable= | |||
Variables are names associated with memory locations that store values. In most programming languages, the values stored in memory have a formal [[Programming_Languages_Concepts#Type|type]] associated with them. Depending on the static or dynamic typing nature of the language, variables may or may not allow the modification their type throughout the execution of the program. A useful mental representation of a variable is a sticky note that can be attached to an object, and then re-attached to a different object. Depending on the language, the second object may or may not be of the same type. Variables and [[#Identifier|identifiers]] are equivalent. | |||
==<span id='Value'></span>Value Variable== | |||
{{Internal|Go_Variables#(No)_Reference_Variables|Go Variables}} | |||
==<span id='Reference'></span>Reference Variable== | |||
A reference is a variable which refers to another variable. It can be thought as an '''alias''', or an '''alternate name''' of a variable. A reference is different from a [[#Pointer|pointer]], described below ,in that once you make a copy of a reference, they are now independent, and while they may be referring to the same variable, when you manipulate the reference it will change what it refers to, rather than the referring value. From this perspective, one does have the access to the "implementation" of a reference variable, only to its target. | |||
Java and [[Python_Language#All_Python_Variables_are_Reference_Variables|Python]] use references variables. | |||
Go uses [[#Pointer|pointers]], and it does not have [[Go_Language#.28No.29_Reference_Variables|reference variables]]. | |||
C++ has both pointer and reference support. | |||
See also: | |||
{{Internal|Java_Language#Reference_Type_Value|References in Java}} | |||
{{Internal|Python_Language#References|References in Python}} | |||
==<span id='Pointer'></span>Pointer Variable== | |||
A pointer is a [[#Variable|variable]] that stores an address of a location in memory. The term "pointer" is also used to name the data type associated with addresses in memory. | |||
A pointer variable can store the address of another variable (the location in memory referred by that variable), or other memory items. | |||
When we reference a variable through a pointer, it's called "indirection". | |||
A pointer can be reassigned to a different address, so the pointers are more flexible than the [[#Reference_Variable|reference variables]]. | |||
The name '''pass-by-pointer''' comes from the fact that when a function is invoked, the argument corresponding to the parameter being passed-by-pointer is a pointer, the address to the location in memory the function needs access to. | |||
{{Internal|Pointers_in_Go#Overview|Pointers in Go}} | |||
==Variables in Specific Programming Languages== | |||
* [[Java_Language#Variables|Java variables]] | |||
* [[Python_Language#Variables|Python variables]] | |||
* [[Go_Language#Variables|Go variables]] | |||
* [[Bash_Parameters_and_Variables#Parameters_and_Variables|Bash variables and parameters]] | |||
=Parameter= | =Parameter= | ||
A parameter is a [[#Variable|variable]] name used in a function definition. Each parameter translates to a [[#Variable|variable]] private to the function. The parameters are handles for [[#Argument|arguments]] for a particular function invocation. For Java generics, the ordinary parameters and variables are replaced with [[Java_Generics_Concepts#Type_Parameters|type parameters]] ([[Java_Generics_Concepts#Type_Variable|type variables]]), and the arguments are replaced with [[Java_Generics_Concepts#Type_Argument|type arguments]]. | |||
Function parameters in various languages: | |||
* [[Java_Language#Method_Parameter|Java method parameters]] | |||
* [[Python_Language_Functions#Function_Parameters|Python function parameters]] | |||
* [[Go_Functions#Parameters|Go function parameters]] | |||
* [[Bash_Parameters_and_Variables#Parameters_and_Variables|Bash variables and parameters]] | |||
=Argument= | =Argument= | ||
When a function is invoked, we pass an argument for each [[#Parameter|parameter]] declared in the function definition. An argument is a value that is passed into the function as function's input. When the function is called with arguments, the values of those argument are copied to the function-scoped [[#Variable|variables]] corresponding to the associated parameters. The argument can be another variable, defined in the block that invokes the function. Usually, and depending on the syntax of the particular language, arguments are passed in parentheses and they are separated by commas. | |||
Function arguments in various languages: | |||
* [[Python_Language_Functions#Function_Arguments|Python function arguments]] | |||
* [[Go_Functions#Arguments|Go function arguments]] | |||
=Pass by Value vs. Pass by Reference vs. Pass by Pointer= | |||
{{Internal|Go_Functions#Pass-by-value_vs._pass-by-pointer_vs._pass-by-reference|Pass by Value vs. Pass by Reference vs. Pass by Pointer in the Go Language}} | |||
=Function Invocation= | |||
* [[Go_Functions#Invocation|Go function invocation]] |
Latest revision as of 00:35, 10 July 2024
Internal
Identifier (Name)
An identifier is a name given to program element, for example to a variable. In this case, the identifier and the variable are equivalent. Identifiers are also used for functions, classes, labels, and many more elements. Identifiers must differ in spelling and case from the language keywords, or reserved words.
Keyword (Reserved Word)
Reserved words across languages:
Variable
Variables are names associated with memory locations that store values. In most programming languages, the values stored in memory have a formal type associated with them. Depending on the static or dynamic typing nature of the language, variables may or may not allow the modification their type throughout the execution of the program. A useful mental representation of a variable is a sticky note that can be attached to an object, and then re-attached to a different object. Depending on the language, the second object may or may not be of the same type. Variables and identifiers are equivalent.
Value Variable
Reference Variable
A reference is a variable which refers to another variable. It can be thought as an alias, or an alternate name of a variable. A reference is different from a pointer, described below ,in that once you make a copy of a reference, they are now independent, and while they may be referring to the same variable, when you manipulate the reference it will change what it refers to, rather than the referring value. From this perspective, one does have the access to the "implementation" of a reference variable, only to its target.
Java and Python use references variables.
Go uses pointers, and it does not have reference variables.
C++ has both pointer and reference support.
See also:
Pointer Variable
A pointer is a variable that stores an address of a location in memory. The term "pointer" is also used to name the data type associated with addresses in memory.
A pointer variable can store the address of another variable (the location in memory referred by that variable), or other memory items.
When we reference a variable through a pointer, it's called "indirection".
A pointer can be reassigned to a different address, so the pointers are more flexible than the reference variables.
The name pass-by-pointer comes from the fact that when a function is invoked, the argument corresponding to the parameter being passed-by-pointer is a pointer, the address to the location in memory the function needs access to.
Variables in Specific Programming Languages
Parameter
A parameter is a variable name used in a function definition. Each parameter translates to a variable private to the function. The parameters are handles for arguments for a particular function invocation. For Java generics, the ordinary parameters and variables are replaced with type parameters (type variables), and the arguments are replaced with type arguments.
Function parameters in various languages:
- Java method parameters
- Python function parameters
- Go function parameters
- Bash variables and parameters
Argument
When a function is invoked, we pass an argument for each parameter declared in the function definition. An argument is a value that is passed into the function as function's input. When the function is called with arguments, the values of those argument are copied to the function-scoped variables corresponding to the associated parameters. The argument can be another variable, defined in the block that invokes the function. Usually, and depending on the syntax of the particular language, arguments are passed in parentheses and they are separated by commas.
Function arguments in various languages: