Course Content

  • callbyvalue
  • callbyreference

Course Content

User Defined Functions- Types of Parameters

Hello everyone, my name is Aaditya… and we are continuing with this C programming course.

(7 seconds pause ; music)

In the last video, we practically used the functions and you saw.

In our text editor, we made two basic programmes with the help of functions, in that, we saw the three parts of the functions, which were the function declaration, function call and function definition, we saw in which they are interconnected with each other.

How the relationship between the three works.

We have seen all these things in the last video.

Now the time comes, when we were calling the function, the type of parameters we were writing it.

In which ways, we can define different types of parameters, so that the functionality of our function call changes.

So, these types of parameters that we will write in the function call, we will be discussing that in this video.

So, if we can see on the screen, we can see that there are two types of parameters and functions.

In which, the first type of parameters that comes, we call them as ‘call by value’, which means with the help of value they are called in our function call.

Apart from that, if we talk about the other one, we call it ‘call by reference’, which means, we call them by some reference in the function.

So, we will understand these things one by one.

And we will also differentiate between these things with the help of some examples.

 So, we will start without any delay and first of all we will see what exactly is ‘call by value’? So, we will understand it first by definition.

The definition says,

“it is a method of passing the parameters where it copies the actual value from the actual parameters into a formal parameter”.

(01/58)

So, what this definition says, this is a way to provide parameter’s list in our function call, in which what happens is, our actual parameters are there, the values inside them get copied and then they are stored in our formal parameters which are in our function definition.

So, basically what happens is the values are copied from the actual parameters and are stored in our formal parameters.

And we call this method as ‘call by value’.

Now we will see its importance practically.

Before that we will also read this second line.

What does the second line say?

“Changes made to the parameter inside the function have no effect on the actual parameters”.

The parameters that we are using inside the function, we call those formal parameters.

We do some calculation in formal parameters, if their values change, that stays limited to inside the particular function.

It does not go out and affect the value of the actual parameters.

So, that's why it is called as ‘call by value’, in this the value gets copied from the actual parameter and comes to the formal parameter.

One more thing that we see.

By default, whenever we write parameters in our C programming language, that is written by default call by value method only in our function call.

Now, this call by value method, we will understand it with a basic C program and we will go into our text editor where we will see one program, which is called ‘Swap function program’.

Now, what is this ‘swap function’? We have made one function which is called Swap.

What does swap mean? We took any two variables, ‘a’ and ‘b’.

The value inside ‘a’ will be stored in ‘b’, and the value inside ‘b’ will be stored in ‘a’.

So, the value of ‘a’ and ‘b’ will be swapped with each other.

So, to provide this functionality, we are making one function in this particular program.

So, we will see how it gets executed.

We will start with our preprocessor command.

After that comes our function declaration, in that what is our function name? ‘Swap’.

After this when we come to our main function, in the main function we see that we have defined two variables ‘a’ and ‘b’.

The value of ‘a’ that is given here is 10, which is the integer value, so 10 gets stored.

What will get stored in ‘b’? In ‘b’ 20 will be stored.

This storage is happening in our hard disk but how do we represent it in the C programming?

We say that it is getting stored inside one variable, it is getting initialised.

So, we will say that in a, 10 is initialised and 20 in b has got initialised.

So, in this our process of storing a and b.

 After that what did we see? After that we have written two print statements, what is the 1st statement doing? The 1st statement writes, before the swap function, we

will get the value of ‘a’ printed.

Before using the swap function, we are getting a’s value printed here.

That’s why we have written format specifier %d and in front of it we have written the variable’s name which is ‘a’.

After that when we look at the next line.

Even the next line is similar, before the swap value of ‘b’.

So, here we will get the b’s value printed.

So, why are we printing this? To see that when we use the swap

function, after that do the actual value of ‘a’ and ‘b’ get swapped or does it get

swapped only in the function and it doesn’t have any effect outside.

(06/04)

So, before and after, we will get the statement printed twice.

And we will check that our values have changed after using the function or not.

So, we have printed the ‘before swap’ statement.

Now what has happened is we have called the swap function and what are the parameters that we have given in it.

We have given the parameters ‘a’ and ‘b’.

We have given ‘a’ and ‘b’ parameters in our function call.

So, where will it take our compiler? It will take it inside our function definition.

Here you can see that its written type is void.

And what is its name? Swap.

Apart from that in the parameter list int type ‘a’ is given and it has also given int type ‘b’.

So, this is our function definition.

And inside this how the flow happens of swap, that we will see quickly.

We made one variable with the name t e m p.

Which we will make temporary, that’s why we have named it t e m p.

 Why are we making it temporarily? Because we have to swap the values of ‘a’ and ‘b’, between them.

For that we have to first store the a’s value somewhere.

Which means we have to store it in our temp variable.

Then we will put b’s value in ‘a’.

After that the value of temp that we had stored in ‘a’, that we will put in ‘b’.

If you see the process here, we 1st made the variable named temp.

Then whichever value was there in ‘a’, that we have put in temp variable.

And after that where we have put the b’s value? Inside ‘a’.

So, what has happened here? If we don't have to put a’s value in temp.

When we would be putting b’s value in a, a’s value would be vanished, it would get deleted.

That’s why a’s 1st value we have put it in temp, then we put b’s in

‘a’.

And again, in temp a’s 1st value which was stored, we have put it again in

‘b’.

In this way we have swapped ‘a’ and b’s value.

 So, here what are we trying to show? We are trying to show that the values of “A” and “B” are swapped but where has it happened, it has happened inside the function.

Since we are using ‘call by value’ method in this program.

What will happen ideally.

Where our values were of ‘a’ and ‘b’, 10 and 20 respectively.

In our function it will swap and become, 20 for ‘a’ and 10 for ‘b’.

But when we will print it out in the main function.

What will be the value? The value will be as it is, as it was before.

What does this mean? The changes in the function did not get reflected outside, in ‘a’ and b’s value and it remained as it is.

So, to check whether this has really happened.

We will run it once.

So, we understood the swap function’s definition and even call of the swap function.

How we are checking if the actual values of the parameters are changed or not.

To check this, we will quickly run our code.

clear ..up till now !

So, you can see here, here 4 statements are printed, 1st these 2 statements are printed which is printing

swap function’s value first which is saying that a’s is 10 and b’s is20.

Which we had defined early on.

After that, we have put swap function.

As it is in the function the value was swapped but did it reflect outside, which means when we got that value printed in the main function, did it change? So, it didn’t change, after swap you can see that a’s value remains 10 and b’s value has become 20.

If we see in this way, the value that was swapped, that happened.

That had happened inside this particular bracket, they were constrained within the curly brackets.

If we would have got it printed from there, its value would have changed.

Since we have printed it out.

Which means that in our main function we have written this PRINT F line.

Because of what happened, it considered the original value itself.

And whatever changes were made with the help of swap function, they didn’t get reflected as we used call by value method.

So, we saw what is ‘call by value’, from our actual parameters value is copied to our formal parameters and it doesn’t affect our actual parameter’s value.

Now we will talk about the ‘call my reference’ method and understand what its definition says.

“It is a method of passing the arguments, which copies the address of an argument into our formal parameter”.

So, what does this definition say?

This is a way to write the parameter list, in which our value is not getting copied, our actual parameters which were there, their address is getting copied.

Address means wherever in the hard disk it is getting stored.

Wherever it is getting stored it is getting stored at one address.

This address basically takes from our actual parameter, copies our formal parameters and keeps it inside themselves.

And what does this basically result in is like in ‘call by value’, we were making a second copy of the variable.

All the things were happening in the second copy and whenever we wanted to take the real value, we were getting the real value as it is.

But what is happening here is that the address of the actual parameter or value, that itself we're taking in our formal parameters.

What happens with it?

Whatever calculation we will perform in our formal parameters, that will also get reflected on the actual ones because the values that were stored on their address, on that basically, indirectly our function is working.

We'll understand this practically and that's why we will read the second line, which says, “changes made to the parameter affects the past argument”.

(12/22)

We call parameters as arguments as well.

that's why this line is saying whatever changes that we will make inside the function those will also happen in our actual parameter.

So, this is the difference between ‘call by value’ and ‘call by reference’.

We will also understand the call by reference thing practically once, that is how exactly it looks.

We will come back to our text editor and we will directly go to our ‘call by reference’ program.

Now you can see that this is again a basic program, in which again we are making a swap function.

And all the things would be same, before and after statement.

We would be checking that our actual parameter’s value, which is there, does it change after using the swap function or it remains the same as the ‘call by value’.

So, er have written preprocessor commands here.

Then we wrote the function declaration.

Now what is there in function declaration?

This is basically showing you Asterix mark.

 With this asterix mark we mean that, the variables that we would be taking we want their address.

We don’t want their value, we want their address.

That is the reason we are using the start mark here.

We will discuss the star mark in the future videos in our pointers topic.

But for now, we will understand what is the meaning of Start here? Wherever our actual parameters value was stored, this is basically considering the address of that place.

After that, we will come to the main function, in the main function like in the same program, a’s value is 10 and b’s is 20.

We will print the before statement.

It will print a’s 10 and b’s 20.

After that we will use our swap function but this time the parameters that we will pass.

They will be by which method? They will be by ‘call by reference’ method.

How can we get to know that? If you see here, before the name of the normal variable we have put one ampers and.

Which we also call as simple and.

And what does this represent? It represents the value of the

variable's address.

So, that address is basically considered by the formal parameter and the values stored on the address is changed based on the calculations so that when we actually want it, that changed value should be seen in our main function.

So, basically this is the work we are using here so that we can collect the address of that particular variable with the help of the parameter’s list.

(15/23)

So, here in the swap function, we have taken basically the address of that particular variable.

And since this was a function call, our compiler has come in the function definition.

So, this is our function definition.

It is starting from void.

 So, basically this is a swap function.

What is this function definition saying? You can again see that here there is one asterisk mark or a star mark.

It is in front of both a and b.

By which we understand that right now the work is happening on the address.

And so, the address that is there, whatever is the value stored there, that will be changed if we use the swap function.

So, you will see that whenever we are using this variable, we are putting the star mark, by which we understand that we are changing the address value because we will save that value in temp and we will put the b’s value in ‘a’ and the temp one we will again put it in ‘b’.

So, in this way the value got interchanged.

A’s value came in ‘b’ and b’s value came in ‘a’.

And what is going on in the background? The a’s address value is changed with the b’s address value and similarly vice-versa has been done.

Similarly, if we want to get printed the after statement.

Here you can see that the after statement is also printed.

What happens in the after statement is, we will see that our values are changing.

So, we will run it once.

With that we will get a clearer idea...

So, here we can see that one error has come.

So, we will see what is, the error that has come.

Here the error was of ‘percent D’.

We had not written ‘percent D’ properly.

That is the reason we were getting that error.

So, here I have corrected that.

And we will quickly run it and see whether our statements are getting printed.

Here you can see that both before and after statements have been printed.

So, in the previous one as it is the a’s value has come as 10 and the value in ‘b’ has come as 20.

But when by ‘call by reference’ method we called our swap function.

After that what happened? After the after swap value that was there, we get to see that here.

‘A’ is now 20 and ‘b’ is 10.

Which means the value that has been swapped has been effective even outside of the function.

And when we wanted to get the ‘a’ and ‘b’ values printed in the main function.

Those values got swapped.

The swapped values got printed.

So, what happens in call by reference, the & and Asterix difference comes.

And apart from that the values in our function that got changed, that value also reflect in our main function, it affects.

So, this was the difference between the call by value method and call by reference method.

In which any values that we would be changing in our function.

Those will not affect our actual parameters or arguments outside.

But in ‘call by reference’ method all the things that we would change inside the function, in the function definition.

Those things will also affect our actual parameters.

So, this was the difference in writing the two parameters, in ‘call by value’ and ‘call by reference’.

If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.

In the coming videos, we would be discussing, what is the scope of variable.

Which means we have defined the function inside the variable.

Can we use that variable outside of the function? Or any variable that we have defined in ‘if’ or in the main function, can we use it outside of the main function.

We will see all the answers to these questions in the coming video.

Until that time, you can practice the function.

Till that time, we will say goodbye.

We will meet in the next video, thank you.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere