-1

I'm a extreme beginner of C. And I was just wondering how C programming passes value of arguments into functions and results back to the teller?

3 Answers3

2

The C language says nothing about how argument values are passed into functions, or how results are returned. It merely requires it to work.

An example:

int add1(int n) {
    return n + 1;
}

/* ... */

int n = add1(42);

The call add1(42) passes the value 42 (of type int) as an argument to the function add1. This causes execution of the body of add1 to begin.

Inside the body of add1, n is a local variable, initialized to the value that was passed by the current call.

A function call is an expression whose value is whatever value was given to the return statement executed by the function. Execution of the return statement causes the execution of the function to terminate; execution continues with the caller.

The underlying mechanism by which these values are passed around is not specified. On some implementations, values might be pushed onto the "stack". On others, they might be copied via CPU registers.

Passing the values by carrier pigeon would be perfectly legal, as long as the program's behavior is as specified by the C standard.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

This is called a "calling convention." Different platforms define different calling conventions, and C compilers will generally aim to follow the established convention for the platform. There isn't any one way that "C does it," per se. There are a lot of things the C compiler intentionally doesn't specify, and this is one of them. The idea is that a particular implementation will know best what calling convention to use for the target platform.

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

Broadly speaking, the two main techniques are registers or on the stack. The C standard does not specify which technique should be used, but pragmatically speaking, you can think of parameter passing as using a stack.

The stack is often implemented in hardware as a register, the stack pointer (SP). Just like your other registers, the accumulators and index registers, it holds a binary value. The value in the SP register is used in the machine's PUSH and POP instructions which access the memory cell whose address is in the SP register and also modify the register's value to move the pointer up or down to the next cell.

So for simplicity, lets consider just int values, so they're all the same size and we can safely think of the memory cells as an array of ints.

To make a function call, such as

f(a, b);

First the arguments are pushed on the stack, assuming these values are in registers A and B.

PUSH B
PUSH A

Then control is passed to the function. CALL is often a single instruction which pushes the current instruction pointer on the stack and then performs a JUMP to the first byte of f.

CALL f

f will then access its arguments, mindful that the return address (the code location for the function to return to) is also on the stack.

When f is finished, it will either return its value on the stack, or more likely it will simply use a register as an optimization since the value is probably conveniently already in a register. It also needs to pop its argument from the stack as part of returning. This is often a single instruction which accepts an argument specifying how many extra cells to POP.

RET 2

If it returns the value on the stack, it will return differently. It now needs to index the stack, and request one fewer cell to be POPped.

MOVE A,(SP)-2
RET 1
luser droog
  • 18,988
  • 3
  • 53
  • 105