943,910 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 52936
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 10th, 2005
0

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

Quote 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":
Quote ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 10th, 2005
0

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

Quote originally posted by Fasola ...
then what is call-by-reference???
http://www.parashift.com/c++-faq-lit...s.html#faq-8.1
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 10th, 2005
0

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

Hello,

Quote 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.

Quote 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.

Quote 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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Mar 11th, 2005
0

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

Quote originally posted by Dave Sinkula ...
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 11th, 2005
0

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

I'm reading your responses right now Stack!
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 11th, 2005
0

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

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 12th, 2005
0

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

Quote 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
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 14th, 2005
0

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

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!!!
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 14th, 2005
0

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

Quote originally posted by Fasola ...
i got it!!!!
I believe you do.

Quote 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.
Quote ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 14th, 2005
0

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

Quote 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.
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Password verification C++
Next Thread in C++ Forum Timeline: Creating set as a tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC