You should be familiar with the concepts discussed in those lessons before proceeding.

Parameters vs Arguments

In the next three lessons, we’ll talk quite a bit about parameters and arguments, so let’s revisit those definitions before proceeding.

In common usage, the terms parameter and argument are often interchanged. However, for the purposes of further discussion, we will make a distinction between the two:

A function parameter (sometimes called a formal parameter) is a variable declared in the function declaration:

1

2

3

4

5

voidfoo(intx);// declaration (function prototype) -- x is a parameter

voidfoo(intx)// definition (also a declaration) -- x is a parameter

{

}

An argument (sometimes called an actual parameter) is the value that is passed to the function by the caller:

1

2

foo(6);// 6 is the argument passed to parameter x

foo(y+1);// the value of y+1 is the argument passed to parameter x

When a function is called, all of the parameters of the function are created as variables, and the value of the arguments are copied into the parameters. For example:

1

2

3

4

5

voidfoo(intx,inty)

{

}

foo(6,7);

When foo() is called with arguments 6 and 7, foo’s parameter x is created and assigned the value of 6, and foo’s parameter y is created and assigned the value of 7.

Even though parameters are not declared inside the function block, function parameters have local scope. This means that they are created when the function is invoked, and are destroyed when the function block terminates:

1

2

3

voidfoo(intx,inty)// x and y are created here

{

}// x and y are destroyed here

There are 3 primary methods of passing arguments to functions: pass by value, pass by reference, and pass by address. We’ll look at each of those in the next set of lessons.

Hi!
I'm preparing for an IT exam in University and I'm facing a little problem. In one exercise it ask to write the prototype of the function fz, giving you these details:

int x;
double y[100];

I have to write the prototype based on this last information:

1.5 + fz(y, y[4], x + y[x]);

in a way that it is possible.

My question is: is it possible?
'cause searching everywhere, I can't find anything that says that is correct to identify a var with (x+y[x]), so I wanted to ask you if it is correct.
In that case my prototype should be: int fz (int[], int[], int[]); is it correct?

Hi Steven!
y is a double array, so int[] is wrong.
Judging by the context fz returns a double (1.5 + fz()).
The first parameter to fz is y, we know y is a double[].
The second parameter is y[4], since y is a double array every element in y is a double.
The third parameter is (int + double) which is a double.

y is defined as a double array, so the first parameter is clearly double[].
y[4] is the type of element 4 in the double array, so the second parameter is clearly double.
An int plus a double yields a double result, so the third parameter is also a double.

The return value isn't well defined (it could be anything), but since it's being added to a double, it's most likely a double.

There are several errors here. First, in function number(), you're using assignment (=) instead of comparison (==). Second, you call number(x) from main() twice per iteration. You should only call it once (stick the result in a variable if need be).

Hi Alex,
Just write this down to thank you for putting such a comprehensive and yet easy-to-pick-up tutorial for c++. I have been slacking off for several days since finishing coding the blackjack at the end of Chapter 6. It's super rewarding experience. Now ready to move on!

1) I talk about passing functions as parameters to other functions in the section on function pointers.
2) There is no way to directly access the x1 from function foo from foo2. This is intentional, as variable x1 is destroyed as soon as function foo() is done. So when foo2() is executing, x1 doesn't even exist at that point.

Somebody once said, I don't know who, there are no stupid questions only stupid answers. In other words ask questions and learn, the only stupid thing you could do is not to ask questions; don't beat yourself up.

Hi Alex! I have extremely enjoyed your guide and have learned a ton. In terms of learning the language, is it common to have to refer back to previous lessons? I ask this because I find myself looking back to remember how to do things. For example, I completely understand the concepts (pointers, for each loops, etc.) but when I want to implement them I don't remember the exact syntax. I guess I want to make sure I am okay to continue into future lessons even if I need to look back to remember how to implement certain concepts. Thanks a bunch!!

Yes! It's totally expected. I have to look up the specific syntax for things all the time.

What's important is that you understand the concepts themselves, and that when you see a code example, you can appropriately match the syntax to a concept (e.g. if you see an &, you know that this can mean address-of or reference depending on context).

If the user will be entering the numbers, then you'll probably want to write a function that takes an array of numbers the user entered. The function can step through the array and process each element in whatever fashion it desires (e.g. add them up). In this way, you don't need a variable number of parameters; Instead, the array is variable length to accommodate the number of user inputs.