C функции

From Ilianko

Предаване на стойност

/************************************************************************
 *
 * Purpose: Program to demonstrate functions.
 * Author:  M J Leslie.
 * Date:    28-Feb-94
 *
 ************************************************************************/

int add( int, int); 			/* Function declaration 	*/

main()
{
  int i=1;
  printf("i starts out life as %d.", i);

  i = add(1, 1);			/* Function call       		*/

  printf(" And becomes %d after function is executed.\n", i);
}

/************************************************************************/

int add( int a, int b) 			/* Function definition 		*/
{
  int c;
  c = a + b;
  return c;
}


Предаване на указател

/************************************************************************
 *
 * Purpose: Program to demonstrate passing a pointer to a scalar to 
 *	    a function.
 * Author:  M J Leslie.
 * Date:    11-Apr-94
 *
 ************************************************************************/

void add(int*); 			/* Function declaration 	*/

main()
{
  int i=4;				/* variable declaration 	*/
  int* ptr;				/* int pointer			*/

  ptr = &i;				/* 'ptr' now contains the
					   address of 'i'		*/

  printf("i starts out life as %d.\n", i);
  printf("             *ptr is %d.\n", *ptr);

  add(ptr); 				/* Function call       		*/

  printf("            i is now %d.\n", i);
}

/************************************************************************/

void add(int *ptr)	 		/* Function definition 		*/
{
  ++*ptr;				/* Add 1 to the value
					 * pointed too by 'ptr'		*/
  return;
}