943,071 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 52913
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 9th, 2005
0

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

Expand Post »
In my old C++ programming classes I would often trip up on theses basic concepts when things got more and more complicated:

1. Arrays
2. Functions
3. Inheritance
4. Call by Reference
5. Call by Value
6. Pointers

I kind of know how they work but don't really know what they are good for in real life applications. Could somebody explain his or her:

(A) Definition of the concept
(B) Understanding of what the concept does/how it works
(B) Quick example of the concepts use in real life applications (i.e. what is it good for?)



I'll put my understandings and explanation up later tonight, tomorrow the latest
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 9th, 2005
1

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

Each of these are rather broad topics. Can you cut it down to specific questions so we don't have to write page after page of tutorials?
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 9th, 2005
0

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

Quote originally posted by Narue ...
Each of these are rather broad topics. Can you cut it down to specific questions so we don't have to write page after page of tutorials?

Okay, lets deal with functions first, then inheritance

lets start with the format of a function:

return-value-type function name( parameter-list)
{
declaractions and statements
}

^^^what does all that mean, and what does it mean whem you have nothing in the parameter-list compared to when you do have something(s) in the parameter-list?

that one always gets me
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 9th, 2005
0

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

return-value-type Any value, or none at all, that you want to return from the function: an error code, a calculated result, etc.

parameter-list Any value(s) -- or possibly references in C++, or none at all, that you want to pass to the function: a pointer to an array to calculate the sum, for example.

declaractions and statements The "meat"; what it takes to do whatever it is the function does.

what does it mean whem you have nothing in the parameter-list compared to when you do have something(s) in the parameter-list?

It means whatever you want it to mean. This is a little like asking how a painter turns paints and canvas into a portrait.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 9th, 2005
0

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

Quote originally posted by Dave Sinkula ...
parameter-list Any value(s) -- or possibly references in C++, or none at all, that you want to pass to the function

okay you touched on something there! what do you mean by "pass to the function"

explain with an example please
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 9th, 2005
0

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

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 result = multiply(2,3);
  11. printf("result = %d\n", result);
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. result = 6
  17. */
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. */
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 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 result = multiply(2,3);
  11. printf("result = %d\n", result);
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. result = 6
  17. */


hmmm...thanx for the break down...ok so did you pass parameters x and y in function multiply() to function multiply() found in the function main()? am i right or wrong?

and what is this "%d", what does it do?

please explain whats going on in the printf() function


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. */

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, please explain dave

and what does the function putchar() mean
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 10th, 2005
0

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

Quote originally posted by Fasola ...
hmmm...thanx for the break down...ok so did you pass parameters x and y in function multiply() to function multiply() found in the function main()? am i right or wrong?
In the first example, the values 2 and 3 from the call to multiply in main() become the values of the parameters x and y in the function multiply().

Quote originally posted by Fasola ...
and what is this "%d", what does it do?

please explain whats going on in the printf() function
It is just printing an integer, which was the result returned by the function.

Quote originally posted by Fasola ...
Hmmmmm....in the function multiply() how did you go from parameters x and y to i and j
In the second example, the values of i and j in main() are passed to the function multiply() as the parameters x and y.

Quote originally posted by Fasola ...
that looks like a "call-by-reference" but i don't understand why you did that and how it works, please explain dave
Nope. Pass by value. A copy of the value of i, for instance, is passed to the function as the parameter x.

Quote originally posted by Fasola ...
and what does the function putchar() mean
It puts a char to the stdout.
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 Dave Sinkula ...
In the first example, the values 2 and 3 from the call to multiply in main() become the values of the parameters x and y in the function multiply().

In the second example, the values of i and j in main() are passed to the function multiply() as the parameters x and y.

i noticed how you said "become the value"


let me make sure i got that right

does the funtion

multiply(int x, int y)

CALL THE VALUES 2 and 3 (i.e. call-by-value")

of the function

multiply(2,3);

to perform

return x * y;


i'm not sure, is that what you meant by "become"?
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 10th, 2005
0

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

then what is call-by-reference???
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