Hello Youtubers, I'm Parwis and this is our ninth video in C++ programming language
In this video we are going to talk about C++ functions. So functions are used to group codes for performing specific tasks and for defining a function
in C++. First you need to write the return type of the function, after that you need to write
the name of the function. For example you can simply, so for defining a function you can first write the return
type of the function and after that you need to give the name for the function
And if the function has a parameter you need to specify the parameter less than here, so
parameter less and this is the function body. So like this you can define a function in C++, the return type, so if you see in here
this is also a main function that we have in C++ and the return type is int, it means
that it returns an integer value, you can see in here we have return 0
Now for example if your function returns a float you need to just use the return type
as a float or if your function does not return anything you can just use void for this
So now for example, so the return type is just the data type for the value of the function returns
And now let's just create an example of this, a practical example
Now there are two ways that you can define a function in C++, for example, let me just
show you. The first way that you can do, define a function after your main and the second way is that
you can do that before your main function. So first let me just do it after the main function
If you define a function like this you need to do prototyping
Now what I mean, for example in here I have just a function, so my function does not return
anything so I'm going to just use void and the function name is void greeting and it's
just a greeting and I'm going to just say cout hello world
Okay now if you're familiar with other programming language, now when you define a function you
need to call your function. Now we need to call our function in here so we can just say greeting
And now if I run this, let me just run this and I will show you what I mean
Okay now you can see we are receiving an error in here
So if I see the error, let me just, you can see the greeting identifier not found
Now when you're going to define a function after your main, the compiler does not know
about this greeting or this greeting function. Now if a user defined function is defined after the main function, compiler will show
error that we have saw. It is because compiler is unaware of the user defined function or types or arguments passed
to a function. So by this reason in C++ we have function prototype
So the function prototype is just a declaration of function without the body
So for this you can, at the top of your main, you can just say void
So it's a function but without a body. So you can just say void greeting
And if you have any parameter you need to specify in here
So for this we don't have anyone. And now if I run this, now you can see that we are receiving hello world
Now you can call this function as much as you want. So for example, you can, I'm going to just do three times
And if I say, if I run this, you will see that I will receive three times hello world
And now you can see that we have hello world in here. Now the second way was that you can just remove this
This time we are going to define a function at the top, at the top of the main
So let me just cut this and paste this in here. Now this time you don't need any prototype because we have already defined a function
at the top. Now our compiler knows about this function. Now if I run this again, you will see that I am receiving the same result as I received previously
So you can see this, the result is the same. So also you can pass parameters to a function with also you can add return type
So let me just show you, let me remove this. Okay. Now in here I'm going to just define a function that returns an integer
So I'm going to just say int add, and I want to pass two parameters, so int a and also
int b. And now this just return. So let me just create int c and a plus b
And I'm going to just return my c. So this is a function that just returns an integer volume
Okay, now after this in here, I'm going to just create three variables
I'm going to just call it num1, num2, and also sum. So first I'm going to just say c out and in here, please enter first number
And now I'm going to just get the number from the user. So and I'm going to add num1 in here
And also let me just copy this because I need this for the second number
And I want to add these two numbers. So please enter second number and this is num2
Now after this, we need to just sum our result and add and in here we are going to just pass
num1 with num2 like this. And also we need to just c out
So first let me just c out our numbers. So num1 plus num2
Let me end line in here. And also I'm going to just say c out the sum is and we are going to add sum in here
So this is just we have defined a function in here that returns an integer volume because
we are going to add two volumes. And in our main we have created three variables and in here we are going to get the number
or the input from the user. And we sum those two volumes
And after that we are going to just print the result of the sum
So now if I run this, we have error
Oh, sorry, this is the error. Okay now if I run it again
Okay now you can see that now we need to add numbers
So please enter first number for example 4. Please enter second number 5
And you can see 4 plus 5 the result is 9. So like this you can use parameters in a function
Let me just do it once more. So 10 and 20. 10 plus 20 is the sum is 30
Also you can add default parameters to a function. For example in here you can add a default int p
For example I want to just or let me just remove this
Show you. Now for example in here we have just int at
So now I'm going to just int a and for the int b I want to add a default value
For example I want to add 10. And in here we are going to just say return a plus b
And now in here if I call my function at. You can see now this function is we need to add two parameters for this
So the first one for example 10 and the second one for example 20
So now if I run this you will see that I am receiving
Now if I run this you will see that I am receiving 30
We also need to just cout our this. So int result and in here I'm going to just say cout and I want to cout the result
Ok now if I run this. So you can see I am receiving 30 but now if I remove this 20
Because I have already specified a default value for my this parameter
Now you can see that I will not receive any error. I will not receive any error and the result will be 20 because we have 10 for the a and
for the b we have also 10. So now you can see that we are receiving 20
Now you can see that the result is 20. So like this you can work with the functions in C++
Please subscribe to my channel, like the video and also share the video
For the source code you can just check with loop.org