6 Replies - 991 Views - Last Post: 01 September 2009 - 12:21 PM Rate Topic: -----

#1 jolinar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 27-November 08

Passing arguments to functions

Posted 01 September 2009 - 09:32 AM

Okay, so it seems I'm really bad at passing arguments from function to function. In theory and in the easy examples, that's fine, I get it. But I'm a bit stuck when it comes to command line arguments and passing them to functions.

I have a function which handles command line parameters (NOT main()), turns the second one into unsigned long int. Now, I need that function to pass that unsigned long int number into another function, but I'm not quite sure how to do that.

I'm probably not just getting it when it's connected with command line, so I'd appreciate if somebody explained it to me like for a 3-year-old. :D

int param(int argc, char *argv[]) {
unsigned long num;
/* conversion of argv[2] to num, already have that */
return num;
}

int calling() {
unsigned long m;
/* need to use the num value here */
}


Thank you! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Passing arguments to functions

#2 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: Passing arguments to functions

Posted 01 September 2009 - 09:54 AM

Can you call calling() from param or does param have to return num?

Option A:

int param(int argc, char *argv[]) {
unsigned long num;
/* conversion of argv[2] to num, already have that */
calling(num); /* pass num*/
return num;
}

int calling(int num) {
unsigned long m;
/* need to use the num value here */
/* use num */
}



That'll work if you don't ened to return from param() before calling the calling() function.


Option B:

#include <cstdio>

int param(); 
int calling(int);

int main() {

	calling(param());
	return 0;
}


int param() {
	int num = 5;
	return num; /* dummy function for emulation */
}

int calling(int num){
	/* use num for whatever */
	printf("%d\n\n", num);
	return 0;
}


Pass the param function (which returns an int) as the param to the calling function.
Was This Post Helpful? 1
  • +
  • -

#3 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Passing arguments to functions

Posted 01 September 2009 - 10:14 AM

If you are asking how to call the function param from calling then you have to know first what is char** ,it is a pointer which points to another pointer means it holds the address of anoyher pointer.For example
char **p,*k,c;
c = 'A';
k = &c; //k will point to variable c and hold the address of c
p = &k;//p will point to pointer k and hold the address of k (***main stuff)
//Now
char ch[100] = "",*b;
b = ch;//this means b will hold the address from where ch is starting in memory.


Now yours problem
int param(int argc, char *argv[]) {
unsigned long num;
num = 0;
/* conversion of argv[2] to num, already have that */
return num;
}

int calling() {
unsigned long m;
//Creating the parameters for param() function
char **p,*k,ch[2]="A";
k = ch;//k points to the address of ch[0] starting of the array
p = &k;//p pointing to k
m = param(2, p);//call the function
return 0;
}



Good luck.

This post has been edited by debjit625: 01 September 2009 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 jolinar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 27-November 08

Re: Passing arguments to functions

Posted 01 September 2009 - 10:20 AM

Maybe I was a bit unclear at first. It should work like this: main() calls the param(), which handles the command line parameters, converts argv[2] to num, evaluates some conditions and calls function calling(), passing the converted num into the calling(). The function uses the num and the program ends.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: Passing arguments to functions

Posted 01 September 2009 - 10:22 AM

Then Option A would work for you. Your functions can be void in that case as well, unless you need something that calling() calculates back in main().

This post has been edited by KYA: 01 September 2009 - 10:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 jolinar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 27-November 08

Re: Passing arguments to functions

Posted 01 September 2009 - 10:24 AM

Thank you so much, KYA, I have finally figured it out! God, I feel stupid. :D Thank you! :)
Was This Post Helpful? 0
  • +
  • -

#7 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Passing arguments to functions

Posted 01 September 2009 - 12:21 PM

if you have complex command line parameters it is worth looking at GNU getopt() in <unistd.h>
which automates the parsing of command lines
http://www.gnu.org/s...ogram-Arguments
http://www.gnu.org/s...ogram-Arguments

although originally written for UNIX many Windows compilers have a version of unistd.h
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1