| | |
C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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"?
•
•
•
•
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
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
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???
"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
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#13 Mar 10th, 2005
Hello,
» 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: 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.
» 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.
» 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
•
•
•
•
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
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; }
•
•
•
•
Originally Posted by Fasola
and what does the function putchar() mean
•
•
•
•
Originally Posted by Fasola
what is call-by-reference???
- 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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#14 Mar 11th, 2005
•
•
•
•
Originally Posted by Dave Sinkula
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int multiply(int x, int y) { return x * y; } int main() { int i, j; for (i = 1; i < 10; ++i) { for (j = 1; j < 10; ++j) { printf("%2d ", multiply(i, j)); } putchar('\n'); } return 0; } /* my output 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 */
^^^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)
//An alias (an alternate name) for an object. //References are frequently used for pass-by-reference: void swap(int& i, int& j) { int tmp = i; i = j; j = tmp; } int main() { int x, y; ... swap(x,y); ... } //Here i and j are aliases for main's x and y respectively. //In other words, i is x — not a pointer to x, nor a copy of x, but x itself. //Anything you do to i gets done to x, and vice versa.
why is that?
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#15 Mar 11th, 2005
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
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
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
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
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!!!
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!!!
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#19 Mar 14th, 2005
•
•
•
•
Originally Posted by Fasola
i got it!!!!
•
•
•
•
Originally Posted by Fasola
CAN SOMEBODY PLEASE HELP ME WITH "POINTERS" and "INHERITANCE"...that can get very confusing also!!!
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’’.
"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
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Exercise using: unsigned int datecode(int year, int month, int day);
- Next Thread: Break Out Of A Function~
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






