Simple Parameters in C
Example 1 - pass by value - in only type parameters
#include <stdio.h>
/** prototypes **/
int cube_it(int value); /* int cube_it(int); */
int main(void)
{
int number = 3, cube;
cube = cube_it(number);
printf("\n%d cubed = %d\n", number, cube);
return(0);
}
/** cube_it - brief description, etc. since this is the place for
*** for the function header when commenting your code.
**/
int cube_it(int value) /* int cube_it(int value) */
{ /* { */
int answer; /* return(value * value * value); */
/* } */
answer = value * value * value;
return(answer);
}
Example 2 - Pass an address to simulate Ada's in out parameters
#include <stdio.h>
/* prototypes */
void changer(int *val); /* void changer(int *); */
int main(void)
{
int number = 3;
printf("\nBefore changer number = %d\n", number);
printf("\nAddress of number = %X\n", &number);
changer(&number);
printf("\nAfter changer number = %d\n", number);
return (0);
}
/** header goes here **/
void changer(int *value)
{
printf("\nIn changer value = %X", value);
printf("\n *value = %d", *value);
*value = 27;
}
Example 3 - Pass an address to simulate Ada's out only
C does not have this concept, the programmer must remember that the
actual parameter does not have a valid value associated with it.
#include <stdio.h>
/* prototypes */
void get_int(int *);
int main(void)
{
int number;
printf("\nEnter an integer ");
get_int(&number);
printf("\n number = %d\n", number);
return (0);
}
void get_int(int *value) /* void get_int(int *value) */
{ /* { */
int temp; /* scanf("%d", value); */
/* } */
scanf("%d", &temp);
*value = temp;
}