We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,325 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Variables in separate functions

I'm coding an inventory program in C
and I need the variable "stock1" in more than two seperate functions

I'm not sure as to how they're supposed to be manipulated and read by the seperate functions
nor do I understand how to declare them

If I declare them as global variables then I'm almost certain that
they won't be manipulated by the seperate functions.

Anyway I can declare them so that I can change the values IN the separate functions?

(is it the use of a pointer?)

4
Contributors
10
Replies
1 Day
Discussion Span
4 Months Ago
Last Updated
11
Views
mattboy64
Newbie Poster
20 posts since Apr 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
// top level

int stock1 = 0;

// first way - pass by value and get return value
stock1 = alterValueOne(stock1);

// second way - pass a pointer to the variable
alterValueTwo(&stock1);

// functions
int alterValueOne (int input)
{
  int temp = input + 7;
  return temp;
}

void alterValueTwo (int& input)
{
  *input = *input + 7;
}
Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5

Did this compile? This is the C forum.

void alterValueTwo (int& input)
mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36

Oop. Typo from sloppy copying of the example usage. Meant to be a pointer, as used inside the function itself. Read as

void alterValueTwo (int* input)
Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5

Thank you so much!

For the pointer should I declare the stock variables as them as global variables?
and how do i declare more than one variable into the function with pointers on them as well?

mattboy64
Newbie Poster
20 posts since Apr 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0

For the pointer should I declare the stock variables as them as global variables?

Almost certainly not. Create them in the main function, and then pass pointers to them into the functions that need to change them.

and how do i declare more than one variable into the function?

void someFunction(int* firstVariable, double* secondVariable, char* thirdVariable, etc etc....)
Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5

got everything now :)
thank you so much
are the other languages any easier than c?

mattboy64
Newbie Poster
20 posts since Apr 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0

are the other languages any easier than c?

Depends on which "other" languages you're thinking about, but in general, yes. C is a simple language, but it's not easy because it doesn't hold your hand. You need to think about and manage a lot of low level things to write good C.

deceptikon
Challenge Accepted
Administrator
3,454 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

I heard Java's syntax is similar to C
I'm planning to do Java and HTML hopefully..

mattboy64
Newbie Poster
20 posts since Apr 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0

I heard Java's syntax is similar to C

It's derived from C, yes. Java as a language is easier than C, in my opinion, but as soon as you throw in the gigantic standard library it gets harder.

deceptikon
Challenge Accepted
Administrator
3,454 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

Great information!

This post has been really helpful,
and I can contribute as much as you all in the future years. :)

mattboy64
Newbie Poster
20 posts since Apr 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0853 seconds using 2.68MB