Monday, 26 September 2022

What is Function in programming language |what is function in C++ |what is function call in programming language |what is function in computer |What is function in programming

-: Functions in C++ :-

     Function is a block of code, which created once and reused it again and again in our programme anywhere when needed.

      Function runs when it is called inside the program. To serve different purposes(operations) create different functions.


    - Basic Syntax of Function in C++ is written below - 

__________________________________________________________

#include<iostream>

using namespace std;

DataType  FunctionName( Parameter1, parameter 2,.......){

       Return  WriteOperationToBePerformed(example +, -, *, / etc);

int main() {

        cout<<FunctionName(value1, value2,......) ;

    return 0 ;

} 

___________________________________________________________


-:Let's See one example for how to create and use functions:-

#include<iostream>

using namespace std;

int AddFunc(int X, int Y) {

      return  X + Y;    } 

int  SubFunc(int A, int B) {

     return  A - B;    } 

int MulFunc(int P, int Q) {

     return  P * Q;   } 

int DivFunc(int M, int N) {

     return  M/N ;

int main() {

       

       cout<<"Summation of  X & Y = " <<AddFunc(10, 5) <<endl;

        cout<<"Subtraction of A & B= " <<SubFunc(50, 20) <<endl;

        cout<<"Product of  P & Q = " <<MulFunc(15, 2) <<endl;

        cout<<"Division of  M & N = " <<DivFunc(40,20) <<endl;

         return 0 ;

} 

         

___________________________________________________________

 output :-   Summation of X & Y = 15

                    Subtraction of A & B=  30

                    Product of  P & Q =  30

                   Division of  X & Y =  2

 

___________________________________________________________

       Click on image to see clear view




            Output:-


let's understand that what happened above -  

  • firstly we created a function with FunctionName SumFunc() and passed two parameters as container to contain those values which are to be added at the time of function run. 
  • where we took X and Y as parameter-1 and parameter-2 respectively. 
  • similarly we created 3 another functions for subtraction, multiplication and division. 
  • After successfully creating all functions we simply called these functions by their FunctionName [SumFunc(), SubFunc(), MulFunc(), DivFunc()]. it is not necessary to call these functions in same sequence, at a same time we can call either single or multiple functions too as we needed. 
  • On Calling Function, in the place of parameter-1 and parameter-2 we assigned directly values 10 and 5 respectively. You can put any value there. 

Use of functions :-

if we write a lengthy program for any special purpose and we have to add two numbers many times, so instead of writing repeatedly same code, we will simply call the function which is specially written to add two numbers and only assign two new values in the place of parameter-1 and parameter-2 which are to be added. 

  • To avoid repetition of code to serve single purpose. 
  • To keep clean our programme. 
  • To make shorter our programme. 




Must Read :-

What is function in programming language (Hindi explanation)

what is class in programming language C++





No comments:

Post a Comment