returnType functionName ( .... , struct StructName param , .... )
{
// (Function body)
}
|
Always remember that:
|
| main program file (defines a) | help program file (declares a) |
|---|---|
#include <stdio.h>
/* ---------------------------
Define the structure FIRST
---------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* ------------------------------------
Now you can define variables
of that structure...
------------------------------------ */
int main(int argc, char *argv[])
{
struct BankAccount a;
a.accNum = 123;
a.balance = 1000.0;
print( a ); // Pass a struct
// variable as parameter
}
|
#include <stdio.h>
/* --------------------------
Define the structure FIRST
-------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* -------------------------------
Now you can define a paramter
variable of that structure.....
------------------------------- */
void print( struct BankAccount x )
{
printf("x.accNum = %d x.balance = %f\n",
x.accNum, x.balance);
}
|
How to run the program:
|
|
| main program file (defines a) | help program file (declares a) |
|---|---|
#include <stdio.h>
/* ---------------------------
Define the structure FIRST
---------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* ------------------------------------
Now you can define variables
of that structure...
------------------------------------ */
int main(int argc, char *argv[])
{
struct BankAccount a;
extern void update( struct BankAccount x );
// Declare update()
a.accNum = 123;
a.balance = 1000.0;
printf("Before calling update:
a.accNum = %d a.balance = %f\n",
a.accNum, a.balance);
update (a);
// Demonstrate that struct is
// passed-by-value
printf("AFTER calling update:
a.accNum = %d a.balance = %f\n",
a.accNum, a.balance);
}
|
#include <stdio.h>
/* --------------------------
Define the structure FIRST
-------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* -------------------------------
Now you can define a paramter
variable of that structure.....
------------------------------- */
void update( struct BankAccount x )
{
printf("Before update:
x.accNum = %d x.balance = %f\n",
x.accNum, x.balance);
x.accNum = 1001; // Update parameter
x.balance = 999999; // variable
printf("AFTER update:
x.accNum = %d x.balance = %f\n",
x.accNum, x.balance);
}
|
Result:
Before calling update: a.accNum = 123 a.balance = 1000.000000 Before update: x.accNum = 123 x.balance = 1000.000000 AFTER update: x.accNum = 1001 x.balance = 999999.000000 AFTER calling update: a.accNum = 123 a.balance = 1000.000000 UNCHANGED !!! |
How to run the program:
|