Monday, January 28, 2019

Functions in c++ programming

in this post, you will learn about the functions in c++ programming and learn how to declare, define and call a function in c++ programming.

functions in c++ programming, learn c++ programming
learn c++ programming


User defined functions in c++ programming.

To use a function in any programming language we need to learn about three things.

  1. function declaration.
  2. function definition.
  3. function call.

Declaration of a function(function prototype):

to declare a function we need four things.

  1. function type.
  2. function name.
  3. parameter list.
  4. terminating semicolon.

syntext:
            <return-type> <function-name><(parameter-list)>;

Example:
                int fun(int, int);

Explaination:
                     int = return type
                     fun = function name
                     (int, int) = parameter list, means we pass the two integer values in the function.

Note: we can declare the function always in global scope.

function definition:

we need function definition to perform a special task. and we need two things to define a function:

  1. function header/function declaration.
  2. function body.

syntext:
              <return-type> <function-name><(parameter-list)>
              {
                    variable declaration;
                    statements;
                    return statement;

              }

Example:
                 int fun(int a, int b)
                {
                        int c; // variable declaration

                        c=a+b; // statement

                        return c;  // return statement

                }

function call:

to perform a special task we need to call a function.

syntext:
            <function-name><(parameter-list)>;

Example:
               fun(20,30);

Note: without declaring or defining a function we do not call the function. and at the time of calling, declaring, defining a function the parameter lists should be same.

Example:
                int add(int a, int b)
               {

                    int c;
                    c=a+b;
                    return c;

                }
in the above example when we call the function to add then we need to pass two integer values. like:

               add(20,30);

we cannot change the type of parameter and number of parameter.
           
Example:
               add(20, 30.33);
this is wrong calling to the add function because of the first parameter is an integer but the second parameter is float type.
               add( 20);
there is only one parameter in the calling. so this should be wrong

More of function:

we can combine the function is definition and declaration.

Example:
                int add( int a, int b)
               {

                 int c;
                 c= a+b;
                 return 0;
              }

we can simply use the definition of a function without declaring a function.

Example program:

in this program I declare a function name add and defined the function and pass the two values and call the add function at the end of the program.
             #include <iostream>
       
             int add(int, int); // here i declare the function name add
           
             int main()
             {
               
                int d;

                d= add(20,30);
              /* here i call the add function and the return value is stored in d variable. and the 20 and 30 are the actual parameter. */
              std::cout<<"\n the addition of 20 and 30 is"<< d; 
              return 0;
             }

             int add(int a, int b) // here a and b are the formal parameter 
             {
                int c;
                c=a+b;

                return c;

             }

in above example, I declare an add function in global scope and pass the two integer values, and in the main function, I call the add function and pass the two integer values 20 and 30 in the function, and in the function definition, I declare two variable a and b in the parameter list. which can hold the value of 20 and 30. and in the body, I make a variable c which can be used to add the a and b variable and after that, I return the value c. which is stored in d variable in the main program.

what is the formal and actual parameter:

the parameter which can we pass at the time of calling the function is called actual parameter, and the parameter which can be passed at the time of definition of the function is called formal parameter.

Note: when we call the function then the compiler jump from the calling to the definition of the function, and after completing the function definition compiler return in the main function.

so, I hope now you fully understand how to declare, define, and call a function.

"Please share this post as much as possible as you can. and help others to learn to programme, and it also appreciates me to make posts such like this. 

0 comments: