Wednesday, 13 May 2015

Introduction Of Function

Function:- 
                  A function in c++ groups a number of program statements into single unit and gives it a name. This unit or part of function can be called from the other parts of the program.

Advantages of functions in c++:-

1- A complex program can be divided into small sub tasks and functions subprograms can be written for each.

2- They are easy to understand.

3- It has the facility to defining a function in terms of itself i.e, recursion.

4- A function can be utilized in many programs by separately compiling it and loading them together.

 Syntax :-

                     type  name_of_function(parameter passing)

                    {
                             Body of the function

                         }

:- Program to display general message using functions:-



 #include<iostream.h>

 void main()
 {


 void displaymsg();    // Function Prototype


 displaymsg();        // Function Call



 }

void displaymsg()    // Function Definition

 {

 cout<<"welcome to the function part in c++ programming language" ;


 }

Output:-




-Function Prototype:-
                                      It is necessary to declare a function before its use, if your are using main part before function. it tell the compiler that the function would be referenced at a later stage in the program. It could be in main function or it could be outside the main function.

Syntax:-      
                      type name_of_function(parameter passing)

example from above program:-        void displaymsg();

Actual Parameter-  Actual Parameter are the variables  that are provided in the function call.

Formal Parameter- Formal Parameter are the variables that are provided in the function definition.


Recursion:-
                      In C++, a function can call itself, this is called recursion. A function is said to be recursive if there exists a statement in its body for the function call itself.

There are two program that illustrates the use of recursion:-

Write a Program in C++ to generate first n fibonacci terms .

-Write a Program in C++ which calculates the factorial of a number using recursion.

- Function Overloading:- Overloading means to use the same thing for different purposes. In C++, an overloaded function refers to a function having one name and more than one different meanings. So by using overloading functions we provide the same name for several functions.

There is a Program that illustrate the use of function Overloading.

- Write a Program in c++ for function Overloading.




No comments:

Post a Comment