Various C function constructs, passing to and returning a value from functions

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: How to pass argument to and return a value from functions in various C function constructs

To show: Various C function constructs, passing to and returning value from function

 

/*** Function skeleton example ***/

#include <stdio.h>

 

/* function prototypes and their variations */

/* In C++ it is required by standard* /

/* notice and remember these variations... */

void FunctOne(void);

double FunctTwo();

int FunctThree(int);

void FunctFour(int);

 

/* main() program, a function that receive noting and return an integer */

int main(void)

{

double q = FunctTwo();

int y = 100, x, r;

 

printf("\n-----PLAYING WITH A FUNCTION-----\n");

printf("All call by value ONLY!!!\n");

printf("Starting: I'm in main()...\n");

 

/* function call, go to FunctOne() without any argument */

FunctOne();

printf("\nBack in main()...\n");

 

/* function call, go to FunctTwo() without any argument */

printf("Back in main()...\n");

/* display the returned value */

printf("The returned value = %.4f\n", q);

 

/* function call, go to FunctThree() with an argument */

x = FunctThree(y);

printf("Back in main()...\n");

 

/* display the returned value */

printf("Display the returned value from FunctThree() = %d\n", x);

 

r = 50;

/* pass r to FunctFour() */

FunctFour(r);

 

printf("Finally back in main()...\n");

return 0;

}

 

void FunctOne()

{

/* do nothing here just display the following text */

printf("\nNow I'm in FunctOne()!...\n");

printf("Receives nothing, return nothing...\n");

 

/* return to main, without any returned value */

}

 

double FunctTwo()

{

/* receive nothing but do some work here */

double p = 10.123;

printf("\nNow I'm in FunctTwo()!\nmay do some work here..."

"\nReceives nothing but returns something"

"\nto the calling function...\n");

/* and return something */

return p;

}

 

int FunctThree(int z)

{

/* receive something, do some work and return the something */

int a = z + 100;

 

printf("\nThen, in FunctThree()!...\n");

printf("Receives something from calling function\ndo some work here and"

"\nreturn something to the calling function...\n");

/* then return to main, with return value */

return a;

}

 

void FunctFour(int s)

{

/* received something but return nothing */

int r = s - 20;

 

printf("\nNow, in FunctFour()...\n");

printf("Received something, but return nothing...\n");

printf("The value processed here = %d\n", r);

printf("Then within FunctFour(), call FunctOne()...\n");

FunctOne();

printf("Back in FunctFour()....\n");

}

 

Output example:

 

Now I'm in FunctTwo()!

may do some work here...

Receives nothing but returns something

to the calling function...

-----PLAYING WITH A FUNCTION-----

All call by value ONLY!!!

Starting: I'm in main()...

Now I'm in FunctOne()!...

Receives nothing, return nothing...

Back in main()...

Back in main()...

The returned value = 10.1230

Then, in FunctThree()!...

Receives something from calling function

do some work here and

return something to the calling function...

Back in main()...

Display the returned value from FunctThree() = 200

Now, in FunctFour()...

Received something, but return nothing...

The value processed here = 30

Then within FunctFour(), call FunctOne()...

Now I'm in FunctOne()!...

Receives nothing, return nothing...

Back in FunctFour()....

Finally back in main()...

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index