Getting Started

(lily) #1

Chapter 6: C Functions and Program Structures


the argument now not only has the type ‘char’ but a specific variable

In the calling function, ‘myByte’ is an alias for the address
n this case a char. The called function takes that char and puts in
this case, with the name ‘myData’.
e but are not stored in the same place.
’ not the actual ‘myByte’ itself. If the
hange is not reflected in the calling
ising number of bugs
t’s make a function adder that adds
o numbers.

e getboinked();

unsigned char add2 = 1;

adder(add1,add2,results);

{
// Do stuff with the variable ‘data’
}

Note that
‘myData’. It doesn’t matter what you name the argument in the calling function,
as long as the type matches, so:


sendChar(myByte);


This is just fine, since the sendchar function will use the data in ‘myByte’ as the
data in ‘myData’ in the function definition. An important consideration is that the
data in ‘myByte’ is copied to sendchar(myByte), but the variable ‘myByte’ is not
sent. Think about this.
of some data, i
memory at another address aliased, in
‘myByte’ and ‘myData’ have the same valu
The function only sees a copy of ‘myByte
function chages the ‘myData’ variable, that c
functions ‘myByte’ variable. This is a source of a surpr
among novice C programmers. To clarify le
tw


void adder(unsigned char a1, unsigned char a2, unsigned char r)
{
r = a1 + a2;


if(r == 2) getrewarded();
els
}


Let’s call it in main()


int main()
{
unsigned char add1 = 1;


unsigned char results = 0;

Free download pdf