LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-26-2010, 10:45 AM   #1
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: Fedora14,Scientific 6.1?, Mandriva 2010 ;GO MAGEIA!!!Next up Gentoo
Posts: 774

Rep: Reputation: 51
Parameter passing: Parameters in General Info requested


Ive read a few books and a lot of tutorials on C but can't find this topic explained in a deliberate way.I can find bits and pieces but nothing thorough.

Thanks in advance
 
Old 04-26-2010, 12:02 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by theKbStockpiler View Post
Ive read a few books and a lot of tutorials on C but can't find this topic explained in a deliberate way.I can find bits and pieces but nothing thorough.

Thanks in advance
OK, so give a link to any of the articles you read and copy-paste here the first item/sentence you do not understand.
 
Old 04-26-2010, 01:13 PM   #3
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: Fedora14,Scientific 6.1?, Mandriva 2010 ;GO MAGEIA!!!Next up Gentoo
Posts: 774

Original Poster
Rep: Reputation: 51
You are a brave person Sergei Steshenko: Thanks

The book/tutorial I'm currently reading is http://crasseux.com/books/ctutorial/with the first question athttp://crasseux.com/books/ctutorial/...l%20parameters
(My immediate Question is in the boldest print)
Actual parameters and formal parameters

There are two other categories that you should know about that are also referred to as "parameters". They are called "parameters" because they define information that is passed to a function.

* Actual parameters are parameters as they appear in function calls.
* Formal parameters are parameters as they appear in function declarations.

(A parameter cannot be both a formal and an actual parameter,) but both formal parameters and actual parameters can be either value parameters or variable parameters.
What in the hell is the context to this? It can't mean that I have to use a different declared parameter than the one I used in the function. If formal is the parameter that I intended to use while declaring it or prototyping it and actual is the data that the function will use for the existence of that particular function.

Let's look at calculate_bill again:

#include <stdio.h>

int main (void);
int calculate_bill (int, int, int);

int main()
{
int bill;
int fred = 25;
int frank = 32;
int franny = 27;

bill = calculate_bill (fred, frank, franny);
printf("The total bill comes to $%d.00.\n", bill);

exit (0);
}

int calculate_bill (int diner1, int diner2, int diner3)
{
int total;

total = diner1 + diner2 + diner3;
return total;
}

In the function main in the example above, fred, frank, and franny are all actual parameters when used to call calculate_bill. On the other hand, the corresponding variables in calculate_bill (namely diner1, diner2 and diner3, respectively) are all formal parameters because they appear in a function definition.

Although formal parameters are always variables (which does not mean that they are always variable parameters), actual parameters do not have to be variables. You can use numbers, expressions, or even function calls as actual parameters. Here are some examples of valid actual parameters in the function call to calculate_bill:

bill = calculate_bill (25, 32, 27);

bill = calculate_bill (50+60, 25*2, 100-75);

bill = calculate_bill (fred, franny, (int) sqrt(25));

(The last example requires the inclusion of the math routines in math.h, and compilation with the -lm option. sqrt is the square-root function and returns a double, so it must be cast into an int to be passed to calculate_bill.
 
Old 04-26-2010, 01:31 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by theKbStockpiler View Post
The book/tutorial I'm currently reading is http://crasseux.com/books/ctutorial/with the first question athttp://crasseux.com/books/ctutorial/...l%20parameters
(My immediate Question is in the boldest print)
Actual parameters and formal parameters

There are two other categories that you should know about that are also referred to as "parameters". They are called "parameters" because they define information that is passed to a function.

* Actual parameters are parameters as they appear in function calls.
* Formal parameters are parameters as they appear in function declarations.

(A parameter cannot be both a formal and an actual parameter,) but both formal parameters and actual parameters can be either value parameters or variable parameters.
What in the hell is the context to this?
...
Look at the items in red and think that an animal can not be at the same time a dog and a cat - even though one can find a dog and cat having nickname "Billy", for example.
 
Old 04-26-2010, 10:28 PM   #5
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: Fedora14,Scientific 6.1?, Mandriva 2010 ;GO MAGEIA!!!Next up Gentoo
Posts: 774

Original Poster
Rep: Reputation: 51
Is this over or under my head?

."A parameter cannot be both a formal and an actual."
So I can have two(representations) instances of the same thing and although they represent the same thing are different because of there instances basically. They would be the same data stored in two different places. The gist which is not stated here is that you can use a different argument than the one you use to declare the function or make a prototype I assume.
"but both formal parameters and actual parameters can be either value parameters or variable parameters."Does this mean that I can declare a value parameter and then use this same function as a variable type?

Thanks Again
 
Old 04-27-2010, 01:59 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
I don't know if this will help but I am used to different terminology. The data that is passed into a function is known as the argument, whilst the data received by the function is the parameter. The parameter of a function can be passed to another function and so it become the argument of the function it is calling.

It's all about perspective where are you when you look at the data that is being manipulated in the code.
 
Old 04-27-2010, 04:45 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by theKbStockpiler View Post
."A parameter cannot be both a formal and an actual."
So I can have two(representations) instances of the same thing and although they represent the same thing are different because of there instances basically. They would be the same data stored in two different places. The gist which is not stated here is that you can use a different argument than the one you use to declare the function or make a prototype I assume.
"but both formal parameters and actual parameters can be either value parameters or variable parameters."Does this mean that I can declare a value parameter and then use this same function as a variable type?

Thanks Again
What/which "same thing" ?

Suppose you need to build a barn - rectangular one. Let's consider just walls, not roof. Suppose you hired a worker. How did you hire that worker ? You asked him: "How do you build a barn ?". The answer was: "I take two walls and two other walls, and then install them. In each pair the walls have the same length and the walls are parallel to each other, the walls from different pairs are perpendicular; altogether the base of the barn is a rectangle".

So, the worker is a function - it meanwhile stands in your yard and does nothing - like a function in your program which is described/declared, but not yet called.

In order to proceed you and the worker have to establish some terminology. Let's assume that you want to build barns whose one pair of walls are parallel to the street. So the barn walls can be called 'parallel' (to the street) and 'perpendicular'. These 'parallel' and 'perpendicular' are formal parameters. You could have given them any other names, just the two names should be different - otherwise you won't be able to distinguish between the pairs of walls.

So, you are calling the worker: "Hey, it's time to build a barn ! Bring 15ft pair of parallel walls and 10ft pair of perpendicular walls.". In this case 15ft and 10ft are actual parameters. Values if you wish.

You could also point your finger to two pairs of walls you already have on which you put labels 'parallel wall', 'perpendicular wall' and tell the worker: "Build a barn from those 'parallel wall', 'perpendicular wall' over here". In this case 'parallel wall', 'perpendicular wall' are kind of variables.
 
Old 04-27-2010, 02:10 PM   #8
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: Fedora14,Scientific 6.1?, Mandriva 2010 ;GO MAGEIA!!!Next up Gentoo
Posts: 774

Original Poster
Rep: Reputation: 51
If I babble to someone else more things get done

I think we are on the same sheet of music although parameters and arguments are a very too common misunderstanding.



If I made a function named "area( ) ( ):" I would need parameters of length and width. and the actual arguments for a room of 25 would be area (length , width); with area (5, 5); for an actual area of 25.



So my function is for the area of anything with the size ( arguments) I present to the function. Otherwise the function would only be good for one area and not really a function because it would not be re -useable



To understand the" What/which" If my formal parameters are the same as my actual ones the outcome will be the same so there is not difference from that point. The data exists in different places by they are exactly the same meaning wise. If I have a worker it does not matter when I tell them the information. 5 is 5. If the units that make up a group of 5 are the same it does not matter which 5 we are referring to. If I told my worker to retrieve a gallon of water from the next room and there were five of them there it would not matter which one was retrieved.

The parameters for area would be length and width and the arguments for a 25 would be 5, The parameters of area would exist in the function declaration and the arguments would exist when I call a function with a particular area in mind. The proposed arguments in the function(parameters) would not be used because they are overwritten by the arguments of the calling function.

I have not thought about it until now but the functions parameters would have to be declared as variables so parameters as far as a function is concerned is just a place holder or some valve to use as default.


A parameter and argument cannot co-exist in a function that is being processes at a particular instance if the valves are different because there are only so many place holders for the functions data,(parameters).


Thank You graemef as well

Last edited by theKbStockpiler; 04-27-2010 at 02:12 PM.
 
Old 04-27-2010, 03:01 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by theKbStockpiler View Post
I think we are on the same sheet of music although parameters and arguments are a very too common misunderstanding.



If I made a function named "area( ) ( ):" I would need parameters of length and width. and the actual arguments for a room of 25 would be area (length , width); with area (5, 5); for an actual area of 25.



So my function is for the area of anything with the size ( arguments) I present to the function. Otherwise the function would only be good for one area and not really a function because it would not be re -useable



To understand the" What/which" If my formal parameters are the same as my actual ones the outcome will be the same so there is not difference from that point. The data exists in different places by they are exactly the same meaning wise. If I have a worker it does not matter when I tell them the information. 5 is 5. If the units that make up a group of 5 are the same it does not matter which 5 we are referring to. If I told my worker to retrieve a gallon of water from the next room and there were five of them there it would not matter which one was retrieved.

The parameters for area would be length and width and the arguments for a 25 would be 5, The parameters of area would exist in the function declaration and the arguments would exist when I call a function with a particular area in mind. The proposed arguments in the function(parameters) would not be used because they are overwritten by the arguments of the calling function.

I have not thought about it until now but the functions parameters would have to be declared as variables so parameters as far as a function is concerned is just a place holder or some valve to use as default.


A parameter and argument cannot co-exist in a function that is being processes at a particular instance if the valves are different because there are only so many place holders for the functions data,(parameters).


Thank You graemef as well
If you want it shorter:
  1. a function describes an algorithm;
  2. the algorithm operates on data, part of which may come from outside the function;
  3. the data to come from outside in the function/algorithm description is described/written as formal parameters;
  4. when the function is called, actual parameters are passed and the algorithm is finally executed with those actual parameters.

...

You next big question will be closures . Not present in the official "C".
 
Old 04-27-2010, 08:52 PM   #10
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: Fedora14,Scientific 6.1?, Mandriva 2010 ;GO MAGEIA!!!Next up Gentoo
Posts: 774

Original Poster
Rep: Reputation: 51
Close enough for government work

Thanks again. Hopefully I won't come across an encloser but if I do I will know who to ask.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing kernel parameter to vms evil_empire Linux - Virtualization and Cloud 5 12-18-2009 09:57 AM
Passing a custom parameter to the kernel jkoshi Linux - Kernel 11 07-13-2009 11:54 AM
want to replace $ with \$ in the passing parameter siva512 Linux - Newbie 1 04-03-2008 12:24 PM
passing a file handle as a parameter in c nkoplm Programming 2 02-05-2008 08:28 PM
parameter passing mechanisms dhanakom Programming 2 09-01-2003 04:15 PM


All times are GMT -5. The time now is 11:35 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration