L
lisp9000
I've read that C allows two ways to pass information between
functions:
o Pass by Value
o Pass by Reference
I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?
Also I read that, "C does not have run-time typing". What does this
mean and is it true?
C just cares that you pass the correct type of arguments (int, float,
char, ...) and doesn't care about the value or variable/data structure
name is that also correct?
I wrote 3 simple test programs for the different ways to pass
information in functions. Any critiques/improvements would be welcome:
/* Example 1 */
/* func.c */
int swap(int a, int b)
{
a = b;
return a;
}
/* main.c */
int b =1;
int c = 2;
int main(void)
{
swap(b,c);
printf("%d",c);
return 0;
}
BTW in my example of func.c and main.c here it doesn't seem to matter
if I make b, c global variables or local. In writing C how do I best
decide if I should make variables global or local, or if it won't
affect the purpose of the code should I not care?
/* Example 2 */
/* func2.c */
int swap(int &a, int &b)
{
a = b;
return a;
}
/* main2.c */
int *b =1;
int *c = 2;
int main(void)
{
swap(&b,&c);
printf("%d",&c);
return 0;
}
/* Example 3 */
/* func3.c */
int swap(int *a, int *b)
{
*a = *b;
return *a;
}
/* main3.c */
int *b =1;
int *c = 2;
int main(void)
{
swap(*b,*c);
printf("%d",&c);
return 0;
}
PS: I'm learning about closures in Lisp and they seem nifty, but I
read C doesn't have them. Or is this not quite true?
Lisp 9000
functions:
o Pass by Value
o Pass by Reference
I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?
Also I read that, "C does not have run-time typing". What does this
mean and is it true?
C just cares that you pass the correct type of arguments (int, float,
char, ...) and doesn't care about the value or variable/data structure
name is that also correct?
I wrote 3 simple test programs for the different ways to pass
information in functions. Any critiques/improvements would be welcome:
/* Example 1 */
/* func.c */
int swap(int a, int b)
{
a = b;
return a;
}
/* main.c */
int b =1;
int c = 2;
int main(void)
{
swap(b,c);
printf("%d",c);
return 0;
}
BTW in my example of func.c and main.c here it doesn't seem to matter
if I make b, c global variables or local. In writing C how do I best
decide if I should make variables global or local, or if it won't
affect the purpose of the code should I not care?
/* Example 2 */
/* func2.c */
int swap(int &a, int &b)
{
a = b;
return a;
}
/* main2.c */
int *b =1;
int *c = 2;
int main(void)
{
swap(&b,&c);
printf("%d",&c);
return 0;
}
/* Example 3 */
/* func3.c */
int swap(int *a, int *b)
{
*a = *b;
return *a;
}
/* main3.c */
int *b =1;
int *c = 2;
int main(void)
{
swap(*b,*c);
printf("%d",&c);
return 0;
}
PS: I'm learning about closures in Lisp and they seem nifty, but I
read C doesn't have them. Or is this not quite true?
Lisp 9000