473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing Variables between functions

I don't want to use global variables to do this, but I'm stumped on how
to pass a variable between one function to another, so if someone could
help out, that'd be appreciated. Here's an example of what I need to
accomplish:

(note: Don't worry about whether or not the functions work properly,
but if you want to comment, I won't stop you.)

void playGame ()
{
int sum; /* sum of rolled dice */
int myPoint; /* point earned */
int balance; /* user's current balance */
..
..
..
}
void getWager()
{
int wager;

printf("Enter wager: ");
scanf("%d", &wager);

while (wager > balance || wager <= 0)
{
printf("Your wager must not exceed your current balance.\n");
printf("Enter a new wager: ");
scanf("%d", &wager);
}
}

Nov 15 '05 #1
3 14610

"tigrfire" <bb******@gmail.com> wrote > void playGame ()
{
int sum; /* sum of rolled dice */
int myPoint; /* point earned */
int balance; /* user's current balance */
.
.
.
}
void getWager()
{
int wager;

printf("Enter wager: ");
scanf("%d", &wager);

while (wager > balance || wager <= 0)
{
printf("Your wager must not exceed your current balance.\n");
printf("Enter a new wager: ");
scanf("%d", &wager);
}
}


Two ways

1)

int getWager()
{
int wager;

/* code to print message etc */
scanf("%d", &wager);

return wager.
}

in the caller

theWager = getWager();

2)

/*
get the wager, return 0 on success, -1 on fail
*/
int getWager(int *wager)
{
int result;
/* code to print message goes here */

result = scanf("%d", wager);

/* scanf tells you how many fields were read successfully*/
if(result == 1)
return 0;
else
return -1;
}

/* in the caller */

int ok;
int wager;

ok = getWager(&wager);
if(ok == -1)
{
/* code to handle error goes here. User didn't enter a valid wager */
}
The second way is better, but the first will work.
Nov 15 '05 #2
On Sun, 06 Nov 2005 12:33:54 -0800, tigrfire wrote:
I don't want to use global variables to do this, but I'm stumped on how
to pass a variable between one function to another, so if someone could
help out, that'd be appreciated. Here's an example of what I need to
accomplish:

(note: Don't worry about whether or not the functions work properly,
but if you want to comment, I won't stop you.)

void playGame ()
{
int sum; /* sum of rolled dice */
int myPoint; /* point earned */
int balance; /* user's current balance */
getWager(balance);

You needn't call it with a variable named "balance" - any expression that
has int type or can implicitly be safely converted to int is fine.

This is pretty fundamental stuff and comp.lang.c isn't the best place to
learn it. A text-book or online course would be better.
.
.
.
}
void getWager()
void getWager(int balance)
{
int wager;

printf("Enter wager: ");
scanf("%d", &wager);

while (wager > balance || wager <= 0)
{
printf("Your wager must not exceed your current balance.\n");
printf("Enter a new wager: ");
scanf("%d", &wager);
}
}


--
http://members.dodo.com.au/~netocrat
Nov 15 '05 #3
tigrfire wrote:
I don't want to use global variables to do this, but I'm stumped on how
to pass a variable between one function to another, so if someone could
help out, that'd be appreciated. Here's an example of what I need to
accomplish:

(note: Don't worry about whether or not the functions work properly,
but if you want to comment, I won't stop you.)

void playGame ()
{
int sum; /* sum of rolled dice */
int myPoint; /* point earned */
int balance; /* user's current balance */
.
.
.
}
void getWager()
{
int wager;

printf("Enter wager: ");
scanf("%d", &wager);

while (wager > balance || wager <= 0)
{
printf("Your wager must not exceed your current balance.\n");
printf("Enter a new wager: ");
scanf("%d", &wager);
}
}

This is one of the first things you should have been taught when
learning C. Functions are usually taught in the third or fourth class.
You are therefore obviously not a C programmer. This is BASIC stuff.

A function is:

return_type function_name (parameter_type parameter, ...) {
function_body
}
Therefore passing a value into a function:

getWager(balance);

getting a value out of the function:

wager = getWager();

pass a value in and get a value out:

wager = getWager(balance);

Once you understand this, then you can start playing with pointers.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: lawrence | last post by:
Those of you with backgrounds with the C language will laugh at my mistake, but those of you, like myself, who deal mostly with PHP should be warned about passing variables as references -...
7
by: Matthew Robinson | last post by:
i read a tutorial the other day on passing variables between php pages using a html form and setting the action to the php page to parse, can anybody see anything wrong with the code below? the...
2
by: Chieko Kuroda | last post by:
Hello all, I would like to learn the syntax for passing variables that I retreived from a database to a second asp page. Currently, I'm using: Response.Write "<tr><td>&nbsp;</td><td><Font size=...
1
by: Consuelo Guenther | last post by:
Hello, I am having problems with passing variables between pages. I have the following: First asp page has the function: -----------------------------------------------------------------------...
3
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford...
7
by: Khai | last post by:
First off, yes, I understand the crapload of tutorials out there, (well, rather, I understand there /are/ a crapload of tutorials out there), the problem is my comprehension. I'm trying to pass...
3
by: jsdeveloper | last post by:
Hi, I just started programming in javascript, and I'm having a problem passing variables between javascript and asp code in .asp page. Can you please help? I've given the sample code below. ...
1
satterfieldben
by: satterfieldben | last post by:
I have a newbie question about passing variables between functions. I am wanting to use a drop down box to select a value. Then base on which was selected, it would create a variable and I would call...
6
by: coool | last post by:
Hi :) anyone knows how can I send variables from a php page to a form - i need to fill the form with these variables ? maybe using (the process of passing variables to other pages - through...
6
BezerkRogue
by: BezerkRogue | last post by:
This is the most fundamental action I am sure, but I can't seem to make it happen. I am familiar with passing variables in ASP. But that doesn't seem to be the preferred method in .NET. I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.