You are on page 1of 1

/******************************************************************************************* * * 02-08-02 0833 TMK * Started Code to be used for C reference and tutorial * * 02-08-02 1315 TMK * Added

function and included examples of passing by value and reference. Also * added looping, as an alternate way of calculating the square. Blah, Blah, Blah. * *******************************************************************************************/ #include <stdio.h> /* Loads the input and output functions, needed for standard I/O */

double squareFcn(int, float*); /* This is the prototype for the function */ int main() /* A main function is required for all C programs, this one returns an int */ { unsigned long int first; /* Creates the variable first, hold largest possible C int */ float second; /* Creates the variable second, for a floating point value */ double answer; /* Creates the variable answer, holds a double-precision float */ FILE *output; /* Specifies a file identifier */

if((output = fopen("output.txt","w"))== NULL) { /* This loop tests the file to make sure it opened properly, and if not exits */ puts("Cannot open the file"); exit(0); } printf("Please enter the first number: "); /* Prints Text */ scanf("%d", &first); /* Gets user input, %d is for an integer, & is important */ printf("Please enter the second number: "); /* Prints Text */ scanf("%f", &second); /* Gets user input, %f is for float */ if (first > second) /* Loop...if true does what is in next set of {} */ { answer = first + second; /* math */ } else if (first < second) /* loop ctnd. if if was false, this gets tested */ { answer = first - second; } else /* if last 2 pieces of loop were both false this gets executed */ { answer = (first * second) / (first + second); } first = squareFcn(first, &second);/* Calls fcn...passes 1st by value, 2nd by refrence*/ answer += (first + second); /* same as answer = answer + (first + second) */ printf("The answer is: %f!!!!\nGoodBye\n", answer);/* prints text and value to screen*/ fprintf(output,"%f",answer); /* prints value only to file */ return 0; } double squareFcn(int insideFirst, float *insideSecond) /* functino definition */ { /* insideFirst was passed by value, insideSecond was passed by reference */ int counter, top = insideFirst; /* internal only variables */ insideFirst = 0; /* sets to zero so it will start at x*1 */ for(counter = 1; counter <= top; counter++) /* for loop */ { insideFirst += top; } *insideSecond *= *insideSecond; not the variable*/ return insideFirst; } /* must have * before identifier because calling the memory location /* returns 0 from main fcn...standard practice sends 0, but any int works */

/* returns value of inside first from function */

/* Compiled by: gcc o c-tutorial.exe c-tutorial.c */

You might also like