Hello, and welcome.
In this lecture, I want to talk about arguments and parameters.
These terms tend to be used interchangeably.
What they have to deal with is basically data that
you pass to a function.
So a parameter is a variable and a function declaration and a
function definition implementation.
Remember, this difference between function declaration, function
prototype and function definition.
Function declaration is just the method signature,
it's the function header.
Function definition is the implementation of the
function, the actual code.
In both of these, terms declaration definition,
understand those differences.
But the parameter is what's in the function declaration and
definition, it's in that header.
Because both the header is included in the definition in the declaration.
When a function is called, the arguments are the data you pass
to the function parameters.
Those are the actual value of a variable that gets
passed to the function.
So function parameters defined within the function header,
and their placeholders for the arguments that need to be specified
when the function is invoked.
So understand the difference here between arguments and parameters.
Parameters have to do with the function definition and declaration.
Arguments are where you call the function, when you
invoke the function, the data that you're passing.
So arguments are used when you invoke.
You'll say these are the arguments that are going to my function.
And when the function receives that data, they're referred to
as those are the parameters.
The parameters for a function are a list of parameter
names with their data types.
Each parameter is separated by a comma.
The entire list of parameters is enclosed in parentheses following
the name of the function.
A function can have no parameters, which means
there's no data passed to it.
And in this case, you just have the left and right parenthesis with a
void in between them or empty, you can still have it as empty nothing in
between them, but it's good practice to put a void in between them.
Void is a special keyword that means nothing, means no
parameters or no return data.
Parameters provide the means to pass data to a function.
Data passed from the calling function to the function that is called, the
invoke e to the receiving end, the calling end and the receiving end.
The names of the parameters are local to the function.
We're going to talk about that when we talk about local data.
But those parameter names only exist inside the function body.
They will assume the values of the arguments that are passed to it.
So if you have a function called add, you pass in a variable as an
argument, you pass in the value 5 and you have a function with a
parameter x, x is going to be equal to 5 anywhere in the function body.
The body of the function should use these parameters
in its implementation.
If it doesn't, then they shouldn't have been passed.
If you have a parameter variable defined in your function declaration
definition and you never use that variable inside the function, that's wasteful,
don't include it because that's just saying you don't need it.
Don't send data to a function that's not using it.
A function body may have additional locally defined variables that are
needed by the function implementation.
You may have inside the function body other variables declared
because they're needed for whatever task you're completing.
You don't just only operate on data that's passed to it.
You can operate on local data inside the function.
When passing an array as an argument to a function, you must
also pass an additional argument, have another parameter defined
specifying the size of the array.
The reason for this is that the function has no way of knowing how
many elements are in the array.
So even if you declared an array of size 50, you may
only be storing 30 elements.
And if you're going to use that array inside the function body,
you're probably going to need to know how big it is because you're
going to need either iterate through it using a loop or you're
going to need to access elements.
And if your access elements are iterating through it, you
need to know whether or not when you're out of bounds.
And we've talked about the out of bounds there and why that's important.
Well, here's an example.
When the printf function is called, you always supply one
or more values as arguments.
The first value is that string inside double quotes.
The remaining values are any variables to be displayed.
This is actually a function that takes a variable number of arguments
which we're probably not going to talk about since this is beginner class,
that's more of an advanced concept.
But the printf function is a good example as far as passing
data to it because you're passing multiple sets of data to it, either
the string inside the quotes or variables that you want to display.
This is why it's so important to be able to pass data to a function.
It greatly enhances the usefulness and the flexibility of a function.
If functions did not have the capability to accept data,
they wouldn't be as useful.
In the example that I provided in the last lecture where we
had a function that displayed output, it didn't really do much.
It was very static.
It did the same thing every time you invoked it.
But what if you had a function that took data, then you can
control what's displayed.
So just like the printf function.
If the printf function didn't have parameters, you wouldn't be
able to tell it what to display.
If you didn't accept data, you wouldn't be able to say, hey, display
this variable or display this string.
It wouldn't be very useful.
So you want to be able to pass data to a function makes, it
much more useful and flexible.
And you'll see that.
I mean if we do multiplication function, you wouldn't
want to multiply the same two numbers every time.
You would take parameters and data would be passed to it for two numbers.
So you could do different calculations based on the data that's passed to it.
Very, very important to understand data passing and why it's useful.
It's a good idea also to add comments before each of your
own function definitions.
It helps explain what the function is going to do and how
the arguments are to be used.
You'll typically see this.
You'll see this in my examples.
You have the function declaration at the top of the file, you
have the function definition.
Function definition includes the function header.
Right above the function header, you have comments,
usually multi-line comments.
So you have the slash, asterisk.
And there you say the name of the function, the purpose of the function
and then also describe any data that's being passed to the function.
This, again, is for maintenance and be able to find and
fix errors much quicker.
Usually, you want to have a good name for the function
that will tell you something.
But with additional comments, it will tell you even more.
Here's an example that is a function that multiplies two numbers, that
gets that's getting data passed to it.
We have our multiply two numbers.
We know it's a function because it has the parameters essentially.
It has the parentheses.
That's one way you can identify functions when you see parentheses.
I mean we could also identify because it has a return type.
There's multiple things that tell us it's a function.
But the main thing is the parentheses and then also you see the brackets,
the curly brackets that signify the starting and the ending of the block.
This multiply two numbers function though.
Remember that the function parameters are defined in between the parentheses
and then they're separated by commas.
Here we have two parameters.
We have a parameter named x and a parameter named y.
For each parameter, you have to identify the data
type that's coming in.
So here we have two ints.
This is the standard way to pass data and how to do it.
In between the parentheses, data type then a name for the variable.
The name for the variable because any anything that's valid in c for
the name of a variable, you could call that x y z whatever you want.
You just can't start it with a dollar sign symbol.
And then you separate those by commas, become accustomed to this convention,
parameters inside parentheses.
If you had no parameters, no data that's passed to the function,
what you would see in between those parenthesis is void or you would see
nothing in between the parenthesis.
This function all it's going to do is it's going to take the data that's
passed to it which are two variables now named x and y, and it's going to
multiply them together and store them in a variable named result or in this
case it looks like I misspelled it t.
And then what it does is it prints data.
It prints out the data that is multiplied.
But again, see the flexibility and usefulness of the function
because it takes data.
It can displaying different data based on what you pass it.
When we invoke the function in the main routine here, we say multiply two
numbers and we pass in the arguments.
We pass in the value 10 and 20.
We could pass in variable names here as well.
But in this example, we're just saying 10 and 20.
This is going to call the function multiply two numbers and it's
going to pass in data 10 for the first parameter 20 for the second.
So x is going to be equal to 10, y is going to be equal to 20.
So when we do our multiplication x times y, it's going to be 10 times
20, the result is going to store 200 and then it's going to display
that result along with the variables that are being passed to x y and z.
So it's going to display 10 20 and then 200.
Also notice that we can now reuse this function.
We're calling it again with different data, data values 20 and 30 in our
second line here in the main function.
And then we call it again a third time to multiply 50 by 2.
Very useful to be able to do this to create a function that
takes different data makes it reusable, makes it flexible.
And the syntax is very easy to follow.
It's just two variables inside of the parentheses separated by commas.
And then to call it's even easier, you just space a name
and you have the two data.
Whatever data is separated by common side parentheses and
then ending with a semicolon.
You can pass any kind of data type into a function.
You can pass an arrays.
We're also going to talk about how you can pass in other data types.
We have simple examples here, but you can also pass in pointers,
which we haven't talked about yet.
You can pass in structures.
Again, another concept we haven't talked about.
But it becomes really, really meaningful and useful
when you pass data here.
This is a simple example.
Notice the syntax, study this and will provide a good challenge
for you where you write a number of different functions.
Thank you.