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
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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
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
*/
#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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Dave Sinkula
long time no c
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!
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314