Function arguments and parameter list for user define function C program example

 

 

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: Passing an argument to a function by value, process the argument and return a value to the calling function

To show: How to pass an argument to a function (through parameter list), process it and return a value to the calling program

 

 

// Demonstrates the difference between arguments and parameters

#include <stdio.h>

 

// main() function

void main(void)

{

float x = 3.5, y = 65.11, z;

float half_of (float);

// In this call, x is the argument to half_of() function

z = half_of(x);

printf("The function call statement is z = half_of(x)\n");

printf("where x = 3.5 and y = 65.11...\n\n");

printf("Passing argument x\n");

printf("The value of z = %f \n", z);

 

// In this call, y is the argument to half_of() function

z = half_of(y);

printf("\nPassing argument y\n");

printf("The value of z = %f \n\n", z);

}

 

// function definition, receive a float and return a float

float half_of(float k)

{

// k is the parameter. Each time half_of() is called,

// k has the value that was passed as an argument.

return (k/2);

}

 

Output example:

 

The function call statement is z = half_of(x)

where x = 3.5 and y = 65.11...

Passing argument x

The value of z = 1.750000

Passing argument y

The value of z = 32.555000

Press any key to continue . . .

 

 

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