scope

Function hoisting and the return statement

旧街凉风 提交于 2021-02-19 06:22:31
问题 I would expect this (reduced for the sake of example) function to run without a hitch, but it fails on account of fn2 is not defined : void function(){ var var1 = fn1(); var var2 = fn2(); function fn1(){}; return function fn2(){}; }(); How does the return statement exclude the function expression for fn2 from hoisting? 回答1: Only a function created with a function declaration is hoisted. The function in return function fn2(){}; is created with a (named) function expression so is not hoisted.

Function hoisting and the return statement

一个人想着一个人 提交于 2021-02-19 06:22:11
问题 I would expect this (reduced for the sake of example) function to run without a hitch, but it fails on account of fn2 is not defined : void function(){ var var1 = fn1(); var var2 = fn2(); function fn1(){}; return function fn2(){}; }(); How does the return statement exclude the function expression for fn2 from hoisting? 回答1: Only a function created with a function declaration is hoisted. The function in return function fn2(){}; is created with a (named) function expression so is not hoisted.

How to declare and initialize local variables in gcc inline assembly without extended inline asm?

孤人 提交于 2021-02-18 12:20:14
问题 I know this is a very basic question but I am really stuck on it. In fact I am absolutely newbie in GCC syntax. I want to have local variables (Stack addresses with labels) without using extended inline assembly. Something like the following code in Intel syntax: DATA1 DB 100 MOV AL, DATA1 This is the code I guess may substitute in GCC: int someFunction(int x) { __asm__ volatile( "function1:" ".data;" ".2byte $4 data1 ;" ".text;" "pushq %rbp;" "movq %rsp , %rbp ;" "movl var , %eax;" // this

Creating an object inside an if statement [duplicate]

两盒软妹~` 提交于 2021-02-17 05:42:13
问题 This question already has answers here : Create Object in if-statement and use it later (3 answers) Closed 3 years ago . I'm trying to make a simple program for a bank account. I have created a class called Bank to make and instance and in the main class, where the main method is, I have made an if statement which will create an instance of the class "Bank" depending on the conditions met. The problem is that I can use the instance methods, which is outside the if statement. I have created

How do I use a variable declared in a method, outside that method?

和自甴很熟 提交于 2021-02-17 02:46:29
问题 I am using VS 2008 (C#)... I have created a function in a GlobalClass to use it globally.. This is for opening a dialog box. When I call this function in my method it works but I am not able to use the Object "OFD" that I have created here... static class GlobalClass { public static void OFDbutton() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Image files|*.jpg;*.jpeg;*.png;*.gif"; DialogResult dr = ofd.ShowDialog(); } } In the form method. I am using globalclass.ofdbutton(); /

Variable Scope in C programming

假装没事ソ 提交于 2021-02-16 21:26:39
问题 In c, when this block of code is run,it outputs 10 10 10 10 10. I think the loop should exit after 1st execution as i becomes 11 but it is not happening. Why it is so? #include<stdio.h> int main() { int i; for(i=0;i<5;i++) { int i=10; printf("%d\t",i); i++; } return; } But when program is written as below the output is similar to what i am thinking(i.e.10 ).What is the exact difference between above code and the code shown below? How C is handling these variable? I would be glad if anyone

Variable Scope in C programming

你离开我真会死。 提交于 2021-02-16 21:23:00
问题 In c, when this block of code is run,it outputs 10 10 10 10 10. I think the loop should exit after 1st execution as i becomes 11 but it is not happening. Why it is so? #include<stdio.h> int main() { int i; for(i=0;i<5;i++) { int i=10; printf("%d\t",i); i++; } return; } But when program is written as below the output is similar to what i am thinking(i.e.10 ).What is the exact difference between above code and the code shown below? How C is handling these variable? I would be glad if anyone

pass a variable to foreach function

雨燕双飞 提交于 2021-02-16 15:53:10
问题 Hi I want pass antwoord to opleidingArray.forEach(haalScoresOp, antwoord); So I can use it in the HaalScoresOp function. I cannot get this to work. I also tried binding but this does not function. I am getting antwoord is not defined as an error. var antwoordenPerVraag = [2,1,3]; console.log(VragenEnScores.vragen[0].opleidingen[0]); antwoordenPerVraag.forEach(berekenEindresultaten); function berekenEindresultaten(item, index) { var opleidingArray = VragenEnScores.vragen[index].opleidingen;

Why can't I declare a variable with the same name in different scopes in C#? [duplicate]

只愿长相守 提交于 2021-02-16 13:53:49
问题 This question already has answers here : Why doesn't C# allow me to use the same variable name in different scopes? (5 answers) Closed 5 years ago . Not sure if this is because the C# compiler is extra picky, but I try to do this in C#: public static void Main() { bool result = true; // some dummy value if(result) { int x = 5; Console.WriteLine(x); } int x = 10; Console.WriteLine(x); } The compiler complains that the variable name "x" is already being used: A local variable named 'x' cannot

What does “using namespace” do exactly?

核能气质少年 提交于 2021-02-16 05:49:12
问题 The following C++ test code does not link (gcc 4.9.2, binutils 2.25). The error is In function 'main': undefined reference to 'X::test' . 01: #include <string> 02: #include <iostream> 03: 04: namespace X 05: { 06: extern std::string test; 07: }; 08: 09: using namespace X; 10: std::string test = "Test"; 11: 12: int main() 13: { 14: std::cout << X::test << std::endl; 15: } Because of line 09, I was expecting line 10 to define the X::test variable declared on line 06. I believe that instead an