[SOLVED] Variable inside the function.

Thread Starter

Pushkar1

Joined Apr 5, 2021
294
I'm confused in C language. I clearly don't understand What happens inside the function when we call the function from main.

C:
int ADDITION ( int x; int y)
{
   int Results;
   Results = ( x + y) ;
   return  Results;
}

void main ( )
{
  ADDITION ( 10; 20);
}
In a program, there is function ADDITION that taks two value After that adds both and gives the result.

I'm trying to understand what happens when function ADDITION is called from main.

Where are the values 10 and 20 stored, is it a temporary store ?

x and y are local variables only accessible in the function where they are declared. It cannot be accessed outside of their scope.

It is said that when we call the function then at start variables are created and when the function ends then the variables are destroyed. What does it mean in context of hardware microcontroller?
 

Papabravo

Joined Feb 24, 2006
16,938
In the C language the arguments to a function are passed by value on a LIFO (Last In First Out) pushdown stack. This is a fundamental data structure that you should become familiar with. When control is passed to the starting address of the function, the Stack Pointer (SP) points to where the arguments are. They will be at fixed offsets from the present value of the stack pointer. The "value" of the function can be returned in various ways depending on the underlying hardware architecture. When the return from the function is executed the return address is pulled off of the stack and the information about where the arguments are is lost effectively erasing their (temporary) existence.
 

click_here

Joined Sep 22, 2020
439
What happens with that code? ...Well it doesn't compile

You need to use a "," instead of a ";" in between the arguments of your function - https://www.cprogramming.com/tutorial/c/lesson4.html

Here is a small program that will actually run...
Code:
#include <stdio.h>

int ADDITION ( int x, int y)
{ 
    int Results; 
    Results = ( x + y); 
    return Results; 
} 

int main(void) 
{ 
    int result = ADDITION ( 10, 20);
    
    printf("%d\n", result);
    
    return 0;
}
I've also changed to "int main(void)" https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376

As a beginner you are probably better off approaching this from a practical point of view, rather than worrying about what is happening behind the curtains.

You are creating 2 new variables "x" and "y" and giving them the values that are passed into the function.

Once the function ends those 2 variables fall out of scope and no longer exist, and what you "return" becomes what the function... well... returns (see how the variable "result" what was returned from the function)
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
294
@click_here

In your example function ADDITION start at line 3 and exits at line 8

What's meaning of creates variable when function start at line 3 and destroy variables when function exits at line 8.

Does it mean that when the function is called the value of the variable x and y is temporarily stored inside the function and when the function exits variable value will also destroyed?
 

KeithWalker

Joined Jul 10, 2017
1,933
When the program is compiled and run, variables x and y are created with no values assigned to them. When the function is called, x and y assume the values that are passed to them. When the function has completed its calculation and passed the answer back, the x and y assigned values do not magically disappear. They remain until the function is called again. Then the old values are replaced by new ones.
You are digging a little too deep into the mechanics of programming and confusing yourself. It really does not matter what happens to the x and y values when the function has finished its task as long as they are replaced by new values when it is called again.
Redirect your effort towards learning things that will improve your programming skills..
 
probably, you don't really have to know.

You do have to know that:
a) variables inside the function are local
b) There is call by value and call by reference (pointers)

People writing assembly code might have to know about the stack.
In a simple architecture, the program counter is pushed onto the stack as the address to return too.
the return statement pops the value off the stack into the program counter. Just not in the COSMAC RCA 1802.

It's also convenient if the processor status word (PSW) is pushed on the stack too. This is important with interrupts.
The PSW would have the results of the last operation like a compare,
If you had

compare A, #1
interrupt
(next statement)

When the processor came back from the interrupt, it would know the result of the compare.
 

click_here

Joined Sep 22, 2020
439
Does it mean that when the function is called the value of the variable x and y is temporarily stored inside the function and when the function exits variable value will also destroyed?
The values would be stored in *local variables* inside the function.

I don't know if "destroyed" is the correct word - More like if you look there, what you would find could be anything.

What you are asking about is "storage duration" and their lifetimes. There are 3 types of storage durations according to the C99 standards (6.2.4 Storage durations of objects): static, automatic, and allocated. The one that x and y use is automatic.

The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. i.e. Once you go outside the function ADDITION, that bit of memory is no longer guarenteed to be x/y - After that if you tried to write/read where that variable was in memory what you would be doing is "undefined behaviour" (which should be avoided like the Bubonic plague).

How and where the variables are stored is not straight forward and will probably lead to more confusion - You might find that x and y are put in different places in memory each time the function is called, (such as when recursion is used). You might find that the compiler has reserved 2 spots in memory that it only uses for x/y, or maybe those 2 spots are shared with other local variables from a different function, you just don't know!

When you use C you don't have to worry about that as long as you don't try and use a variable out of scope.

Going forward - You should get familiar with the standards, especially the section on undefined behaviour.

The standards define what you need to do to keep your C code portable
 

Papabravo

Joined Feb 24, 2006
16,938
If you're bound and determined to check it out for yourself, you can always get the compiler to output an assembly language listing. That way everything will be revealed to you. There will be no secrets.
 

Papabravo

Joined Feb 24, 2006
16,938
That will be implementation specific, not C language specific.
That is essentially a tautology. Everything about C is implementation specific. It could hardly be otherwise, unless the compiler outputs an intermediate language for an interpreter. the TS needs a concrete example not higher and higher levels of abstraction.
 

click_here

Joined Sep 22, 2020
439
That is essentially a tautology. Everything about C is implementation specific. It could hardly be otherwise, unless the compiler outputs an intermediate language for an interpreter. the TS needs a concrete example not higher and higher levels of abstraction.
The C standards do not specify how the variables are to be put in memory, only that memory is guaranteed to be reserved for the variable during its lifetime.

The TS can look at the listing file, but that will only be correct for that system/version.

Run the C code on another implementaion and you might find that it does it differently.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
294
You are digging a little too deep into the mechanics of programming and confusing yourself. It really does not matter what happens to the x and y values when the function has finished its task as long as they are replaced by new values when it is called again.
Redirect your effort towards learning things that will improve your programming skills..
the TS needs a concrete example not higher and higher levels of abstraction.
Thanks for the help i was thinking a too deep that i probably didn't need.
 

Larry Hatch

Joined Sep 19, 2017
1
Best if you really need to know, do what Papbravo said, look at ASM or LST file.

You can load the binary into a debugger, emulator, simulator as well. Step or list the code. But by all rights these days, no one looks at the level you are wanting to look.

If you get the right bug, you might be one day. Best of luck.
 
Top