Hello World!
I was little busy that's why i was not able to write a post but now i got little bit of free time so i am here to continue our tutorial of basic c++. So in today post we are going to learn about Functions. Function is one of the most important topic in c++ and in other Programming language as well.
What is Functions
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
So without wasting time we should write a program with function.
Requirements
We will write a program that convert some Currency to other currency using c++. This program will ask user to choose the currency which they want to convert to USD. If the user enter 1 the program will call a function. We will use the if statement as well . So lets begin
Coding
We will use the Dev C++ compiler as usual.
In the previous programs the whole code was in the main function but as we are using and practicing functions we have to declare the function above or below the main function. I will declare the functions above the main function but first of all i have to write the basic code so that the program can take data from user and the convert the the specific data.
We are displaying a message to user to enter 1 for first currency and 2 for second currency so lets check if its works.
And its working it is good to test your code step by step so that you will face less errors.
In the above Pic you can see that we have called some functions. If the user enter 1 the first function will be called which us irrFun. Ok so now lets declare the functions.
We have declared the functions. we could just pass the value through parameters but parameters is another topic we will learn about it later in our course.
so lets check our code if its work.
Edit
The currency datatype should be declare in float type but by mistake i wrote int . it will work fine but it will return value in round it mean that it will not show result in .(points).