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
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
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

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,301
Helped
14,233
Reputation
28,727
Reaction score
12,925
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,678
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

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
thank you both of you, yeah i want it in vhdl
 
Last edited by a moderator:

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,060
Helped
2,075
Reputation
4,167
Reaction score
2,028
Trophy points
1,393
Activity points
39,068
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

Top