Monday, April 1, 2013

The double_triple function: two results by reference


A function that takes in an integer number and "returns" both twice the number and three times the number.


/* Problem: Write a function that takes in an integer number and
"returns" both twice the number and three times the number. */

#include <stdio.h>

/* double_triple function begins */
int
double_triple (int x, int *d)
{
 int t;
 *d=2*x; /* double "returned" via pointer */
 t=3*x;
 return (t); /* triple returned the standard way */
}
/* double_triple function ends */



/* main program begins */
int
main(void)
{

 int a,twice,thrice;
 a=10;
 thrice = double_triple (a, &twice);
 printf("%d is two times %d.\n",twice,a);
 printf("%d is three times %d.\n",thrice,a);

 return(0);
}
/* main program ends */



Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
20 is two times 10.
30 is three times 10.
Philip R. Brown

Hawaii Lawyer - Honolulu Attorney

Proudly Serving Honolulu, Maui, Kauai and the Big Island

Mr. Brown is AV Rated. The AV Peer Review Rating "shows that a lawyer has reached the height of professional excellence. He or she has usually practiced law for many years, and is recognized for the highest levels of skill and integrity." 

No comments:

Post a Comment