Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how does variables behave in reading functions?

Status
Not open for further replies.

h.m

Junior Member level 2
Joined
Jul 26, 2013
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
shiraz_iran
Activity points
151
hi, when a variable of a function get value after reading a function, what will happen about it's value in reading the function for the second time, i mean the variable take it's value in the end of the last reading of the function or it takes the initial value in each reading?

tnx alot for your helps
 

hi, when a variable of a function get value after reading a function, what will happen about it's value in reading the function for the second time, i mean the variable take it's value in the end of the last reading of the function or it takes the initial value in each reading?

tnx alot for your helps
variables in functions are allocated and initialised on entery to the function
e.g. every time this function is called x has the value 10 - the value at the end of the function (30 is this case is lost)
Code:
void test(void)
{
    int x=10;
    ..
    x+=20;
    ..
}
if you wish a variable in a function to retain its value on exit declare it static, e.g.
Code:
void test(void)
{
    static int x=10;
    ..
    x+=20;
    ..
}
on first call x will be 10 (and allocated permenant memory until program termination), on subsequenct calls will take whatever value was on previous exit
 
  • Like
Reactions: h.m

    h.m

    Points: 2
    Helpful Answer Positive Rating
The thread is posted in the FPGA section, so it should be referring to VHDL or Verilog functions. There's no "static" variable in HDL.
 
  • Like
Reactions: h.m

    h.m

    Points: 2
    Helpful Answer Positive Rating
thank you both of you, yeah i want it in vhdl
 
Last edited by a moderator:

when you call a function, all variables inside the function are re-initialised (because they only exist within the scope of the function)
 
  • Like
Reactions: h.m

    h.m

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top