In this Blog we will learn about how to Add two Numbers.

Firstly I will Show you the Program

Look Carefully

C++ Programming
Code To Add Two Numbers

I shall start explaining as the order of execution.

Firstly Note that I used int main() in place of void main() as in the previous program as discussed in the last post.

So what is the difference between int and void?

The thing is they both fall under the same category Data Types.

Inside they both fall under the built in data types.

From the above sentence we can infer that there are also User defined Data Types.(of which we shall discuss later) 

Void-What is void used for?

  • To specify the return type of a function when it doesn’t return a value.
  • To specify that a functions argument list is empty.
  • For declaring generic pointers.

You can avoid bothering about the second and third ones as it will be covered in future topics.

1.Void is used to specify the data type of the value returned by a function.

In our “Hello World” program,the main function had to return no specific value or data type.

That is why we used Void main().

You will understand better when you read about int data type.

 int-what is it used for.

  • To specify the return type of a function if it returns an integer value.
  • To assign variables with integer type.

Look the statement     int a,b,sum;

This statement reserves 3 memory locations with name a,b and sum with size 2 bytes.

Now consider int main() .

Since main is a function it has to return a value.

Here in this program also there is nothing to return, so I simply added the statement return 0; in the end.

To understand more Look this snippet.

  1. C++ programming

To understand how a returning a value by a function works you just have to understand this code.

In this I have defined a function myself named add rather using the ones already defined in the header files.

Execution Order

  • Execution starts from the main()
  • As the next statement executes, 3 memory locations of int data type is reserved an are named a,b,sum respectively.
  • It asks to enter the value to be stored for values a and b.
  • add(a,b) :This is called a Function call.
  • This switches the control to the function definition.
  • Also it passes the values stored in ‘a’ ans ‘b’ to the function ‘add’.
  • Now the function executes,it reserves an integer variable ‘s’.
  • It stores the passed values ‘a’ and ‘b’ which are copied to variables ‘a1’ and ‘b1’ in the function definition.
  • return s; this tells the function to return the value stored in s.
  • Now again a switching of control occurs, this time control goes to the function call in main().
  • This switching of control to and from the function is called Context Switching

Once back in the main program the returned value ‘s’ is stored to variable ‘sum’.

C++Tip

C++ Programming
C++ Tip

The sum is displayed using cout.

Just try to remember the knowledge you aquired from today’s study.

Try some snippets for yourself!

Comment in Queries.

Happy Coding!!

Advertisements