Skip to main content

Functions

A function is a self contain set of statements that perform a specific task.With the help of function larage program is divided into small program called function. main() is an example of user-defined function and strev(),sin() etc is an example of library function.

Advantages of function

  1. With the help of function larage program is divided into small program is called function. 
  2. Remove error more easily. 
  3. Reduce the repetition of code.
  4.  Reduce the complexity of a program.

 Types of function

C++ functions can be classified into two categories, namely library function and user-defined functions.main() is an example of user-defined function and sin(),cos() etc. is an example of library function.
  • Library Function
  • User-defined Function 
  1. Library function :-  There are certain set of general purpose operations which are quite frequently used by many programmers in their programs.For example- To calculate the square root of a number,to calculate power of number and many more.
    The  library functions are predefined functions which are designed to perform some specific tasks.Before using library function in the program, it is necessary to include header files.The header file contain the information about library functions.  The library  functions that perform mathematical operations.Ex:- sin(),cos() etc.They are include math.h header file.The library  functions that perform string operations.Ex:-strev,strcmp() etc.They are include String.h header file.
  2. User-defined functions:-These functions which are defined by the user to meet his requirements are called user-defined functions. Every C++ program contain atleast one function.The function which is present in every C++ program is main( ) which is user-defined function.The library and user-defined functions are normally used in the main() function.
Every function in C++ consists of following components
a)   Function Definition
b)  Function declaration
c)  Function call

Function Definition

Function definition contain the actual code of a function.The general syntax of function definition is

ret_type func_name(data_type par1,data_type par2,..........)
{
local variable declaration;
statement1;
........................
........................
return(expression);
}
The first line of function  definition is called the function declarator that consists of ret_type which specific the data type of the value to be  returned,function_name which specifies the name of the function and set of parameters and their data type separated by commas enclosed in pair of parentheses

Function declaration

While using library functions in the program, it is necessary to include the header file which contains the declaration of related library function.Similarly in C++,it is unavoidable to use a user-defined function   without its declaration.Infact ,before a  function can be called,it must be declared in the function that will do the calling. A function declaration specific the name of the function,number and type of the parameters and its return type followed by a semicolon.
The syntax of function declaration is as follows :  
ret_type func_name(data_type par1,data_type par2,..........);
Here,the ret_type specifies the data type of the value returned by the function.If  the function does not return any value.The func_name is any valid identifier which is followed by list of  parameter(s) preceded by their data types separated by commas and enclosed in parentheses.

Function call

A function which is declared and defined needs to be used somewhere in the program so that it can perform its intended task for which it is designed.In order to use it in the program,we need to call it.A function can be called by specifying the function name followed by a list of arguments separated by coms enclosed in the pair of parentheses.

 Program of using function addition of two number

#include<iostream.h>
#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
void add(int,int);           //Function declaration
add(num1,num2);        //Function call
getch();
}
void add(int x,int y)    //Function definition
{
int sum=x+y;
cout<<"Sum="<<sum;
}

Output of this program
Sum=30

Argument Passing Technique

Argument passing is a process of transfer data from calling function to called function.
Pass argument in three different ways
  • Pass by value
  • Pass by address or pass by pointer
  • Pass by refference
  •   Pass by value :- Pass by value is a technique used for argument passing.In this technique copy of actual argument is made and pass to the formal argument.Any change made in the formal argument  are not refer back to the actual argument.
Program using Pass by value technique swap two number     
#include<iostream.h>
#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
void swap(int,int);
cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl;
swap(num1,num2);
getch();
}
void swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
cout<<"After swaping value is x="<<x<<" and "<<"y="<<y;
}
 Output of this program
num1=10
num2=20
After swaping value is x=20 and y=10
  • Pass by address and pass by pointer :- Pass by address is also another technique used for argument passing.In this technique as compare to pass value.We pass the address of actual argument to the formal argument that act like a pointer variable.In this any change made in formal argument are refer back to actual argument.                                                                                                         Program using Pass by address technique swap two number                                                                                                                                                                                         #include<iostream.h>
    #include<conio.h>
    main()
    {
    int num1=10,num2=20;
    clrscr();
    void swap(int *,int *);
    cout<<"Num1="<<num1<<endl<<"Num2="<<num2<<endl;
    swap(&num1,&num2);
    cout<<"After swaping num1="<<num1<<" and "<<"num2="<<num2;
    getch();
    }
    void swap(int *x,int *y)
    {
    int temp=*x;
    *x=*y;
    *y=temp;
    cout<<"After swaping x="<<*x<<" and "<<"y="<<*y<<endl;
    } Output of this program Num1=10 Num2=20 After swaping x=20 and y=10 After swaping num1=20 and num2 10 
  • Pass by reference :-The pass by reference is a very easy and simple technique of argument passing which is only available in C++. Using this technique,any modifications in the formal arguments(s) in the called function are reflected back to the actual argument(s) in the calling function.This method of argument passing which allows the modifications back to the actual arguments(s) is similar to the pass by address technique of argument passing but without using the complex syntax of pointers.Before discussing the concept of argument passing by reference.
Program using Pass by reference technique swap two number
#include<iostream.h>
#include<conio.h>
main()
{
int num1=10,num2=20;
clrscr();
cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl;
void swap(int &,int &);
swap(num1,num2);
cout<<"After swaping num1="<<num1<<" and "<<"num2="<<num2;
getch();
}
void swap(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
cout<<"After swaping x="<<x<<" and "<<"y="<<y<<endl;
}        
 Output of this program
num1=10
num2=20
After swaping x=20 and y=10
After swaping num1=20 and num2=10

Comments

Popular posts from this blog

Generation of Computers

Generation of Computers   – Computers were developed in different phases known as generations of computer. Depending upon the technologies used the development of electronic computers can be divided into five generations. 1.  First generation The duration lasted from 1946-1959 was based on  vacuum tubes . Because thousands of such bulbs were used, the computers were very large and generate a large amount of heat, causing many problems in temperature regulation.  Magnetic drums   were used for  memory  purpose and instruction and data was given through  punch cards . Computer were operated manually and instruction given in  machine language . E.g.   –   UNIVAC  (Universal automatic computer),  ENIAC  (Electronic Numerical Integrator And Calculator ) ,  Mark I  etc. Main Features  – 1.        Vacuum tube technology used 2.     ...

Input and Output devices

Input device Input device is a device through which data and instruction are entered into computer system. An input devices converts the data and instructions into binary form that computer can understand. This transformation is performed by “Input interface”.   The data entered through input device can be some text, some graphical image or symbol, sound etc, depending on the form of the raw data the various input devices are available. Basic Function Performed by Input unit of a computer system -   1. It accepts the instruction and data from the user. 2. It converts these instruction and data in computer acceptable form. 3. It supplies the converted instruction and data to the computer system for further processing. Some of the commonly input devices used are:- 1. Keyboard 2. Mouse 3. Joy stick 4. Track ball 5. Touch screen 6. Light Pen 7. Digitizer 8. Scanner 9. Speech Recognition Devices 1. Keyboard Keyboard is an input device for enteri...

Computer Memory

Memory :  A memory is just like a human brain. It is used to store data and instructions. Computer memory is the storage space in computer where data is to be processed and instructions required for processing are stored. The memory is divided into large number of small parts. Each part is called cell. Each location or cell has a unique address, which varies from zero to memory size minus one. The computer storage memory is measure in term of Bytes. Eight bits make one Bytes. (Measure units)   Primary Memory/Main Memory Primary memory holds only those data and instructions on which computer is currently working. Has limited capacity and data gets lost when power is switched off. It is also called main memory. It is generally made up of semiconductor device. These memories are not as fast as registers. The data and instructions required to be processed earlier reside in main memory. Characteristic of Main Memory ·         ...