User Defined Function in C++.

Function

  • A feature is a data-acting sub-program that often returns a value. A software written with multiple functions is simpler than a very long program to manage, upgrade and debug.  
  • Most programmers will work independently on separate functions by programming in a modular (functional) manner, which can be installed at a later date to construct the entire project. 
  • Every single feature has its own name.  When that name is encountered in a program, program execution branches to the function's core. 
  • After completion of the operation, execution returns to the system code area from which it was named, and the program proceeds to the next line of code.

www.mskuthar.blogspot.com
User Defined Function in C++.

Creating User-Defined Functions

Declare the function.

  • The statement, called the FUNCTION PROTOTYPE, tells the programmer of the functions to be used in a program, the argument they are taking and the type of value they are returning.

Define the function.

  • The description of function tells the compiler which role the function must perform. 
  • The prototype function and function description on return form, name and parameters must be the same.  
  • The only distinction between prototype function and header function is a semicolon.
  • Type description consists of header type and body function.  
  • The header is EXACTLY like the prototype function, EXCEPT it contains NO terminating semicolon.

//Prototyping, defining and calling a function
#include <iostream.h>



void starline();         // prototype the function
int main()
{
     starline( );      // function call
     cout<< "\t\tBjarne Stroustrup\n";
     starline( );    // function call
     return 0;
}

// function definitionvoid starline()
{
     int count;         // declaring a LOCAL variable
     for(count = 1; count <=65; count++)
          cout<< "*";
     cout<<endl;
}

Argument To A Function

  • The calling method also provides other values for the function called. These are referred to as criteria. 
  • The variables which supply a calling function with the values called actual parameters. 
  • Formal parameters are called the variable that receives the value from the argument.
Consider the following example that evaluates the area of a circle.

#include<iostream.h>

void area(float);
int main()
{
    float radius;
    cin>>radius;
    area(radius);
    return 0;
}
void area(float r)
{
    cout<< “the area of the circle is”<<3.14*r*r<<”\n”;
}

  • Here radius is called actual parameter and r is called formal parameter.

Return Type Of A Function

// Example program

#include <iostream.h> 

int timesTwo(int num);   // function prototype
int main()
{
     int number, response;
     cout<<"Please enter a number:";
     cin>>number;
     response = timesTwo(number);  //function call
     cout<< "The answer is "<<response;
     return 0;
}
//timesTwo function
int timesTwo (int num)
{
     int answer;   //local variable
     answer = 2 * num;
     return (answer);
}

Post a Comment

Previous Post Next Post