Question

Give brief examples of different variable scoping using Haskell, Prolog, Java, C, JavaScript, and/or MIPS

Give brief examples of different variable scoping using Haskell, Prolog, Java, C, JavaScript, and/or MIPS

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*------------------- Scope of variables in Java -------------------------*/

1. Member Variables (Class Level Scope) : These variables should be declared inside class but outside any member function/method . They can be directly accessed from anywhere in the class.

Example :

public class ScopeClassTest
{

int variable1;
private String variable2;
String function1() {}
float function2() {}
char c;
}

2. Local Variables (Method Level Scope) : These variables are declared inside a method/function and can’t be accessed outside the method.

Example :

public class ScopeMethodTest
{
void function1()
{
int localVar;
}
}

3. Loop Variables (Block Scope) : The variables declared inside pair of brackets “{” and “}” in a method has scope withing the brackets only.


public class BlockScopeTest
{
public static void main(String args[])
{
{
int y = 5550;
System.out.println(y);
}
}
}


/*----------------------Scope of variables in JavaScript --------------------------*/

1. Local Variable : Variables declared within a function. These variables can only be accessed from within the function.


Example :

function getHospitalName() {
var hospitalName = "ABC";

// Only here we can use this variable hospitalName

}


2. Global Variables : Variables which are declared outside a function, has global scope.


Example
var hospitalName = "ABC";

// code here can use hospitalName

function getHospitalName() {

// code here can also use hospitalName

}


/*----------------------Scope of variables in C --------------------------*/

1. Local Scope :

int main ()
{
int a,b,c; /* local variables */

a = 80;
b = 50;
c = a - b;

printf ("value of a = %d, b = %d and c = %d\n", a, b, c);

return 0;
}


2. Global Scope :

#include <stdio.h>

int c; /* global variable */

int main ()
{
int a,b; /* local variables */

a = 20;
b = 30;
c = a + b;

printf ("value of a = %d, b = %d and c = %d\n", a, b, c);

return 0;
}

/*----------------------Scope of variables in Haskell --------------------------*/


1. Global Scope :

a = 100


2. Local (Function) Scope :

multiply x = x * x

/*----------------------Scope of variables in Prolog --------------------------*/

Two uses of an identical name for a variable only refer to the same memory location if they are within a single clause.


good(X) :- old(X).
good(Y) :- wise(Y).


/*----------------------Scope of variables in MIPS --------------------------*/

data
globalVariable: .abc 20

.text

#access
lw $a0, globalVariable

#modify
la $a0, globalVariable #get address
li $a1, 11 #new value
sw $a1 0($a0) #save new value

lw $a2, globalVariable #get new value

Add a comment
Know the answer?
Add Answer to:
Give brief examples of different variable scoping using Haskell, Prolog, Java, C, JavaScript, and/or MIPS
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT