954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

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?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
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

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 
#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int result = multiply(2,3);
   printf("result = %d\n", result);
   return 0;
}

/* my output
result = 6
*/
#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 
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
#include <stdio.h>

int multiply(int x, int y)
{
   return x * y;
}

int main()
{
   int result = multiply(2,3);
   printf("result = %d\n", result);
   return 0;
}

/* my output
result = 6
*/

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

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

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

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 
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().and what is this "%d", what does it do?

please explain whats going on in the printf() functionIt is just printing an integer, which was the result returned by the function.Hmmmmm....in the function multiply() how did you go from parameters x and y to i and jIn the second example, thevalues of i and j in main() are passed to the function multiply() as the parameters x and y.

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.and what does the function putchar() meanItputs a char to the stdout.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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 rightdoes 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"?

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

then what is call-by-reference???

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

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Hello,

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

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.

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

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 
#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

//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?

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

I'm reading your responses right now Stack!

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

Let's try this.

<strong>#include</strong> <iostream>
<strong>using</strong> std::<strong>cout</strong>;

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

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

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

/* my output
main: 0
by_value: 42
main: 0 - Note: no change!
by_reference: 42
main: 42 - Note: changed!
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Let's try this.

<strong>#include</strong> <iostream>
<strong>using</strong> std::<strong>cout</strong>;

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

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

<strong>int</strong> <strong>main</strong>()
{
   <strong>int</strong> i = 0;
   <strong>cout</strong> << "main: " << i << "\n";
   <strong>by_value</strong>(i);
   <strong>cout</strong> << "main: " << i << " - Note: no change!\n";
   <strong>by_reference</strong>(i);
   <strong>cout</strong> << "main: " << i << " - Note: changed!\n";
   <strong>return</strong> 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

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

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!!!

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 
i got it!!!!

I believe you do.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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
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.

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You