C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 4,377
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #11
Mar 10th, 2005
Originally Posted by Fasola
i noticed how you said "become the value"
i'm not sure, is that what you meant by "become"?
This is the standard's definition of "parameter" or "formal parameter":
object declared as part of a function declaration or definition that acquires a value on entry to the function, or an identifier from the comma-separated list bounded by the parentheses immediately following the macro name in a function-like macro definition
And where the function, the "expression in the comma-separated list bounded by the parentheses in a function call expression" is called the "arguments".

The value of an argument is passed to a function as a parameter.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,377
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #12
Mar 10th, 2005
Originally Posted by Fasola
then what is call-by-reference???
http://www.parashift.com/c++-faq-lit...s.html#faq-8.1
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #13
Mar 10th, 2005
Hello,

Originally Posted by Fasola
Hmmmmm....in the function multiply() how did you go from parameters x and y to i and j, that looks like a "call-by-reference" but i don't understand why you did that and how it works
» One aspect of C functions may be unfamiliar to programmers who are used to some other languages, particularly Fortran, is Call by Value. In C, all function arguments are passed "by value". This means that the called function is given the values of its arguments in temporary variables rather than the originals. This leads to some different properties that are seen with "call by reference" languages. The main distinction is that in C the called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy.

Call by value is an asset, however, a liability. It usually leads to more compact programs with fewer extraneous variables, because paramaters can be treated as conveniently initialized local variables in the called routine. For example, here is a version of power that makes use of this property:
int power(int base, int n) {
	int p;

	for (p = 1; n > 0; --n)
		p = p * base;
	return p;
}

int main() {
	int x = 2, y = 3;

	printf("%d\n", power(2, 3));

	printf("%d\n", power(x, y));

	return 0;
}
The paramater n is used as a temporary variable, and is counted down until it becomes zero. Whatever is done to n inside power has no effect on the argument that power was originally called with.

Originally Posted by Fasola
and what does the function putchar() mean
» The function, putchar(), writes a character to the current position in the standard output (stdout) and increases the file pointer to point to next character.

Originally Posted by Fasola
what is call-by-reference???
» An argument passing convention where the address of an argument variable is passed to a function or procedure, as opposed to where the value of the argument expression is passed.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #14
Mar 11th, 2005
Originally Posted by Dave Sinkula
  1. #include <stdio.h>
  2.  
  3. int multiply(int x, int y)
  4. {
  5. return x * y;
  6. }
  7.  
  8. int main()
  9. {
  10. int i, j;
  11. for (i = 1; i < 10; ++i)
  12. {
  13. for (j = 1; j < 10; ++j)
  14. {
  15. printf("%2d ", multiply(i, j));
  16. }
  17. putchar('\n');
  18. }
  19. return 0;
  20. }
  21.  
  22. /* my output
  23.  1 2 3 4 5 6 7 8 9
  24.  2 4 6 8 10 12 14 16 18
  25.  3 6 9 12 15 18 21 24 27
  26.  4 8 12 16 20 24 28 32 36
  27.  5 10 15 20 25 30 35 40 45
  28.  6 12 18 24 30 36 42 48 54
  29.  7 14 21 28 35 42 49 56 63
  30.  8 16 24 32 40 48 56 64 72
  31.  9 18 27 36 45 54 63 72 81
  32. */

^^^i know you said that is "pass-by-value", but it looks like the same thing is going on in the pass-by-referrence example you linked me to, look

  1. //An alias (an alternate name) for an object.
  2. //References are frequently used for pass-by-reference:
  3.  
  4.  
  5. void swap(int& i, int& j)
  6. {
  7. int tmp = i;
  8. i = j;
  9. j = tmp;
  10. }
  11.  
  12. int main()
  13. {
  14. int x, y;
  15. ...
  16. swap(x,y);
  17. ...
  18. }
  19. //Here i and j are aliases for main's x and y respectively.
  20. //In other words, i is x — not a pointer to x, nor a copy of x, but x itself.
  21. //Anything you do to i gets done to x, and vice versa.


why is that?
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #15
Mar 11th, 2005
I'm reading your responses right now Stack!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,377
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #16
Mar 11th, 2005
Let's try this.
#include <iostream>
using std::cout;

void by_value(int copy)
{
   copy += 42;
   cout << "by_value: " << copy << '\n';
}

void by_reference(int &reference)
{
   reference += 42;
   cout << "by_reference: " << reference << '\n';
}

int main()
{
   int i = 0;
   cout << "main: " << i << "\n";
   by_value(i);
   cout << "main: " << i << " - Note: no change!\n";
   by_reference(i);
   cout << "main: " << i << " - Note: changed!\n";
   return 0;
}

/* my output
main: 0
by_value: 42
main: 0 - Note: no change!
by_reference: 42
main: 42 - Note: changed!
*/
Last edited by Dave Sinkula; Mar 11th, 2005 at 2:13 pm. Reason: Added color.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #17
Mar 12th, 2005
Originally Posted by Dave Sinkula
Let's try this.
#include <iostream>
using std::cout;

void by_value(int copy)
{
   copy += 42;
   cout << "by_value: " << copy << '\n';
}

void by_reference(int &reference)
{
   reference += 42;
   cout << "by_reference: " << reference << '\n';
}

int main()
{
   int i = 0;
   cout << "main: " << i << "\n";
   by_value(i);
   cout << "main: " << i << " - Note: no change!\n";
   by_reference(i);
   cout << "main: " << i << " - Note: changed!\n";
   return 0;
}

/* my output
main: 0
by_value: 42
main: 0 - Note: no change!
by_reference: 42
main: 42 - Note: changed!
*/

when im done with this game of chess (do you play?) im definitely looking at this a lot closer. Looks like a real good/smart example

thanks a lot
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #18
Mar 14th, 2005
i got it!!!!


damn that feels good (i was starting to think i wasn't cut out for programming)


check this out:

when calling by value a copy is made of the callers value
which is then passed to the called function
changes made by the called function to the copied value doesn't have an affect on the value in the callers function

a disadvantage of this is if the value copied is HUGE it could take a while to process and slow down performance of program

when calling by reference the object of the called function is given a new name by the calling function and therefore BECOMES the new object. Any changes to the value of the called function reflects an exact change in the value of the callers function

a disadvantage of this is a security issue. Call by reference allows outside functions/whatever to manipulate the data in the original function.


CAN SOMEBODY PLEASE HELP ME WITH "POINTERS" and "INHERITANCE"...that can get very confusing also!!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,377
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #19
Mar 14th, 2005
Originally Posted by Fasola
i got it!!!!
I believe you do.

Originally Posted by Fasola
CAN SOMEBODY PLEASE HELP ME WITH "POINTERS" and "INHERITANCE"...that can get very confusing also!!!
How about pointers first? What do you already know, or think you know?

Let's start with a klunky definition.
A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’.
Also, A TUTORIAL ON POINTERS AND ARRAYS IN C may be a good starting point.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #20
Mar 14th, 2005
Originally Posted by Dave Sinkula
I believe you do.


*cartwheel cartwheel*

*backflip backflip*

:mrgreen:


I'm a VERY rusty beginner programmer, so give me a second to read the tutorials and my old C++ text book and i'll tell you, because right now all i know is a pointer is like an object or variable that points to the location/address of another object or variable. The problem is I don't know, how to really set it up. I also don't know what it's good for in real life applications (i.e. like a real like example of when it's would be needed), although I do remember a good chess example in my C++ book.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC