30
Apr

I need to make a program that doesn't return any values, so I have the ability to only use Pass by value or pass by reference (pointers)between the functions.

I have the ability to get my program to compile, but it doesn't give the right results, and I think I'm doing something wrong. I suspect it has to do with my parameter passing.

I have an input function, a calculation function, a print function, and the normal main function. Since I cannot return any values I'm not sure how to get the data from the input function into my calculation function, without returning values. All of my functions are called from main.

My program is something like this (briefly):

void getNumbers

/* get input of x and y with printfs, scanfs */

void getMod

/* a calculation involving x and y to get mod */

void printMod

/* printing the mod value*/

int main

getNumbers();

getMod(x, y);

printMod(mod);

I think I’m getting confused about how to pass parameters. Could someone please help me out


Answer:
Something like this

void getNumbers (int* x, int* y)

{

/* do whatever gets numbers …. */

*x = 0; /* replace 0 with the number

*y = 0;

}

void getMod (int *mod, int y, int x)

{

*mod = x % y; /* replace with actual calculation */

}

int main ()

{

int x, y, mod;

getNumbers (&x, &y);

getMod (&mod, x, y);

printMod (mod);

}

Basically by putting the ampersand & in front of a variable you take its address. When you use that variable in a function put an asterisk * in front of the variable to mean its value.

In C you would do it a little different

void getNumbers (int& x, int& y)

{

/* do whatever gets numbers …. */

x = 0; /* replace 0 with the number

y = 0;

}

void getMod (int &mod, int y, int x)

{

mod = x % y; /* replace with actual calculation */

}

int main ()

{

int x, y, mod;

getNumbers (x, y);

getMod (mod, x, y);

printMod (mod);

}

Hope that helps


Answer:
decide how you’re planning to pass it

by value ? or by address (reference )

you can use pointers *var to pass by reference,

int *a for pointer variable

so use

void getMod( int *a, int *b)

{

// make calculation now.

}

or else just pass values

void getMod( int a, int b)

{

}

Note, you’ve to define variable out of function to keep it in memory ( remember variable defined within function life cycle is within its existance)

Done ?

Good luck


Answer:
In your main pass in place holder variables to getnumbers to be pointers to the values, then use those to pass the pointers into get mod. Be sure to pass the paramebers by reference and to getNumbers and getMod. Print mod pass by value as you’re not changing the data.

int main

getNumbers(x,y);

getMod(x, y);

printMod(x,y);

Book Mark it-> del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Netscape | Yahoo | BlinkList

This entry was posted on Wednesday, April 30th, 2008 at 2:29 pm and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or TrackBack URI from your own site.

Leave a reply

Name (*)
Mail (*)
URI
Comment