arrow
arrow

Functions

How function actually works?


                            
                               <p>Basically,When we call any function the control of the program shifts
                                   towards the function and execuate the all portion in the function and
                                   when it find the <strong>return</strong> statement the control goes to 
                                   the main and execuate the further part of the main function.So if we write 
                                   anyting after the return staement it does not execute so local varaibles are 
                                   formed and removed on the function calling ,so it helps us to know how every part
                                   of the program can execuate</p>
                                   <h2>How Function can be write as?</h2>
                                   dataType funationName(datatype varibaleName1,datatype varibaleName2,.....)
                                   <h1>Types of the functions</h1>
                                   1.Void Functions(non-return Type)
                                   2.dataType Functions(return-Type)
                                   3.dataType Functions(non-returnType)
                                   4.Pass by refernce Functions (argumantatiev dependent functions)
                                   5.Recursive Functions
                                   ***Functions has also Prototypes***
                                    
                            
                        

Input And Output Functions


                        
                            #include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void input(int arr[],int size)
{
  for(int i=0;i<size;i++)
  {
      arr[i]=(rand()%10)+1;
  }
}
void output(int arr[],int size)
{
   for(int i=0;i<size;i++)
   {
       cout<<arr[i]<<" ";
   }
}
void random()
{
    int x = time(0);
    srand(x);
}
int main()
{
    const int size=10;
    int arr[size];
    random();
    input(arr,size);
    output(arr,size);
    return 0;
}
                        
                    

For better understanding of the 2D-Character Arrays Functions understand this simple movement game.


                        
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <conio.h>
using namespace std;
const int row = 10;
const int col = 100;
void displaygeneral(char mat[][col], int row)
{
    system("cls");
    cout << " ____________________________________________________________________________________________________" << endl;
    for (int i = 0; i < row; i++)
    {
        cout << "|";
        for (int j = 0; j < col; j++)
        {

            cout << mat[i][j];
        }
        cout << "|";
        if (i != 10)
            cout << endl;
    }
    cout << " ____________________________________________________________________________________________________" << endl;
}
void start(char mat[][col], int row)
{

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if (i == 0 && j == 0 )
            {
                mat[0][0] = 3;
            }
            else
                mat[i][j] = ' ';
        }
    }
    displaygeneral(mat, row);
}
void moveright(char mat[][col], int row, int &pos,int &down)
{
    int temp;
    temp = mat[down][pos + 1];
    mat[down][pos + 1] = mat[down][pos];
    mat[down][pos] = temp;
    pos++;
}
void moveleft(char mat[][col], int row, int &pos,int &down)
{
    int temp;
    temp = mat[down][pos - 1];
    mat[down][pos - 1] = mat[down][pos];
    mat[down][pos] = temp;
    pos--;
}
void movedown(char mat[][col], int row, int &pos,int &down)
{
    int temp;
    temp = mat[down+1][pos];
    mat[down+1][pos] = mat[down][pos];
    mat[down][pos] = temp;
    down++;
}
void moveup(char mat[][col], int row, int &pos, int &down)
{
    int temp;
    temp = mat[down - 1][pos];
    mat[down - 1][pos] = mat[down][pos];
    mat[down][pos] = temp;
    down--;
}
void menu(char mat[][col],int row,int rightpos,int &downpos)
{
    bool flag = false;
    do
    {
        int ch;
        ch = getch();
        ch=tolower(ch);
        while (ch != 'd' && ch != 'a' && ch != 'w' && ch != 's' && ch != 'q' )
        {
            cout<<"Invalid input:"<<endl;
            cout<<"Again Enter the word:"<<endl;
            ch=getch();
        }
        ch=tolower(ch);
            switch (ch)
            {
            case 'd':
            {
                moveright(mat, row, rightpos, downpos);
                displaygeneral(mat, row);
                break;
            }
            case 'a':
            {
                moveleft(mat, row, rightpos, downpos);
                displaygeneral(mat, row);
                break;
            }
            case 'w':
            {
                moveup(mat, row, rightpos, downpos);
                displaygeneral(mat, row);
                break;
            }
            case 's':
            {
                movedown(mat, row, rightpos, downpos);
                displaygeneral(mat, row);
                break;
            }
            }
    } while (flag == false);
}
int main()
{
    int rightpos = 0;
    int downpos=0;
    char matrix[row][col];
    start(matrix, row);
    menu(matrix,row,rightpos,downpos);


    return 0;
}
                        
                    

Sequential Search Algorithum through Functions


                        
#include<iostream>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void input(int arr[],int size)
{
    for(int i=0;i<size;i++)
    {
        arr[i] = (rand() % 10) + 1;
    }
}
void output(int arr[], int size)
{
    
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }
}
int position(int num,int arr[],int size)
{
    int pos;
    bool flag=false;
   for(int i=0;i<size && flag==false;i++)
   {
       if(arr[i]==num)
       {
         pos=i+1;
         flag=true;
       }   
   }
   if (flag == true)
   {
       return pos;
   }
   else
   {
       cout<<" Not present! ";
       return 0;
   }
}
int main()
{
    const int size=10;
    int arr[size];
    input(arr,size);
    output(arr,size);
    int n;
    cout<<"To get position the element in the array:"<<endl;
    cin>>n;
    cout<<"The element that is present at the "<<position(n,arr,size)<<" position:"<<endl;
    return 0;
}
                        
                    

Searching Algorithum through the functions


                        
#include <iostream>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void random()
{
    int x = time(0);
    srand(x);
}
void input(int arr[], int size)
{
    for (int i = 0; i < size; i++)
    {
        arr[i] = (rand() % 10) + 1;
    }
}
void output(int arr[], int size)
{
   cout<<"The usorted elements in the array:"<<endl;
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }
    cout<<endl;
}
void selectionSort(int arr[],int size)
{
    cout<<"The sorted elements in the array are :"<<endl;
    int index;
    for(int i=0;i<size;i++)
    {
        index=i;
        for(int j=i+1;j<size;j++)
        {
            if(arr[j]<arr[index])
            {
                index=j;
            }
        }
        int temp;
        temp=arr[i];
        arr[i]=arr[index];
        arr[index]=temp;
    }
}
int main()
{
    const int size = 10;
    int arr[size];
    random();
    input(arr, size);
    output(arr, size);
    selectionSort(arr,size);
    output(arr, size);
}
                        
                    

Transpose of a matrix through the Functions


                        
#include <iostream>
#include <cstdlib>
#include <ctime>
#include<iomanip>
using namespace std;
const int row=5;
const int col=5;
void random();
void inputmatrix(int mat[][col],int row);
void outputmatrix(int mat[][col],int row);
void transpos(int trans[][col], int row, int mat[][col], int rowMat);
void displayTranspose(int trans[][col], int row) ;
void display();
int main()
{
    display();
}
void random()
{
    int x = time(0);
    srand(x);
}
void inputmatrix(int mat[][col],int row)
{
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            mat[i][j]=(rand()%10)+1;
            // cin>>mat[i][j];
        }
    }

}
void outputmatrix(int mat[][col], int row){
  cout<<"The matrix as:"<<endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout<<left;
            cout<<setw(5)<<mat[i][j];
        }
        cout<<endl;
    }
}
void transpos(int trans[][col], int row,int mat[][col],int rowMat){
    for(int i=0;i<rowMat;i++)
    {
        for(int j=0;j<col;j++)
        {
            trans[j][i]=mat[i][j];
        }
    }
}
void displayTranspose(int trans[][col], int row)
{
    cout << "The Transpose of a matrix as:" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << left;
            cout << setw(5) << trans[i][j];
        }
        cout << endl;
    }
}
void display()
{

    int matrix[row][col];
    int transpose[row][col];
    random();
    inputmatrix(matrix, row);
    outputmatrix(matrix, row);
    transpos(transpose, row, matrix, row);
    displayTranspose(transpose, row);
}
                        
                    

Recursive Functions

Alphabts Print


                        
#include<iostream>
using namespace std;
void alphabtaes(char ch)
{
    if(ch<=90)
    {
        cout<<ch<<endl;
        ch++;
        alphabtaes(ch);
    }
}
int main()
{
   char ch=65;
   alphabtaes(ch);

    return 0;
}
                        
                    

Numbers Print through the recursive Functions


                        
#include<iostream>
using namespace std;
void print(int n)
{
    if(n<=50)
    {
        cout<<n<<" ";
        n++;
        print(n);
    }
}
int main()
{
    print(1);
    return 0;
}
                        
                    

Factorial Of a given number through the recursive Function.


                        
#include<iostream>
using namespace std;
int fact(int);
int input();
int main()
{
    cout << fact(input());
    return 0;
}
int input()
{
    int n;
    cout<<"Enter a number:"<<endl;
    cin>>n;

    return n;
}
int fact(int n)
{
    if(n==1)
    {
        return 1;
    }
    return(n*fact(n-1));
}

                        
                    

Default Paramerters and function Overloading


Power of a number


                        
#include <iostream>
using namespace std;
int power(int n=2, int p=2);
int power(int , int , int );//prototypes
int main()
{
    int n, p;
    
    cin >> n;
    cin >> p;
    cout << power();         // default parameters
    cout << power(n,p)<<endl;//function overloading 
    cout << power(n,p,n);
    return 0;
}
int power(int n=2, int p=2)
{
    int x = 1;
    for (int i = 0; i < p; i++)
    {
        x = x * n;
    }
    return x;
}
int power(int n, int p,int v)
{
    int x = 1;
    for (int i = 0; i < p; i++)
    {
        x = x * n*v;
    }
    return x;
}

                        
                    

Whats make this website differents from others?

Amazing Feature That Increase your Productivity

If you don't find any problem,in this website and if you have a very difficult assignment question that is not present on internet ,then you dont bother yourself,we are here to solve every problem of your life you just click on the Problem asking button and write your problem we slove the code of your problem and sent you on your email addres that you put in the sin up form.