Function


Function


Large program broken down into smallest unit as function.
A function is a subprogram that acts on data and often returns a value.


Function in C++


Type of function

There are broadly two types of functions
1.    Built-in (or library) function
2.    User -defined function


1.   Built -in function

These functions are part of compiler package. These are part of standard library mode available by the compiler.
For eg. exit (), sqrt(), pow(), strlen() etc.

                                                                              
Header File Categorization
Header File 
Function
Standard input/output function
stdio.h
gets(),puts()
String function
string.h
strcpy(),strcat(),strlen(),
strcmp(),strcmpi(),strrev(),
strupr(),strlwr()
Character function
ctype.h
isalnum(),isalpha(),isdigit(),
islower(),isupper(),tolower(),
toupper()
Mathematical function
math.h
fabs(),pow(),sqrt(),sin(),cos(),
abs()
Other function
stdlib.h
randomize(),random()


2.    User -defined function

The user defined functions are created by you i.e. programmer. These functions are created as per the requirements of the program.
Ex.   int absval(int a)
        {
           return (a<0? -a: a);
         } 

Standard Input/output Function (stdio.h) 


1.gets()

The gets() function accept a string of characters entered from the keyboard and placed them in the string variable mentioned with it.
Ex.   char name[21];
         gets(name);
The string name stores 20 valid character and one extra for null character ‘\0’.


2.puts()
The function puts() write a string on the screen and advances the
cursor to the newline. For this, stdio.h header file is used.


Character function


1.isalnum()

This function returns non zero if its argument is a letter or a digit otherwise return zero.
Syntax
       int isalnum(int c);
Ex. char ch;
      cin>>ch;
  if(isalnum(ch))
   {
      cout<<”alphanumeric”;
    }


2.isalpha

This function is used to check whether the given argument is an alphabet. Ii returns zero when the argument does not have any of them.
Syntax
           int isalpha( int c);      
Ex.
 char ch;
 if(isalpha (c))
  {
   cout<<”it is alphabetic”;
  }


3.isdigit()

This function is used to check the character whether it is between 0-9 or a decimal. It returns zero when argument is not a decimal or an integer in the given range.
Syntax
            int isdigit(int c);
Ex.
char ch;
if(isdigit (ch))
 {
  cout<< “It is digit”;
}


4.islower()

This is used to check the function arguments for lowercase letters (a-z). It returns zero if lowercase letters are not found.
Syntax
  int islower(int c);
  Ex.
  if( islower(ch))
   {
     cout<<”lowercase letter”;
    }


5.isupper()

isupper() character function is used to check if the argument contains uppercase letters(A-Z) ,if not this functions returns zero.
Syntax
  int isupper(int c);
  Ex.
  if (isupper(ch))
 {
  cout<<”uppercase character”;
 }


6.toupper()

 This function returns the uppercase of the parameter if it’s a letter,   otherwise returns the same parameter without any change.
Syntax
    int toupper(int ch);
Ex.
#include<iostream.h>
#include<ctype.h>
 main()
 {
 char ch=’a’;
 cout<<toupper(ch);
}


7.tolower()

This function returns the lowercase of the parameter if it’s a letter, otherwise returns the same parameter without any change.
Syntax
                  int tolower(int ch);
Ex.
  char ch=’A’;
  cout<<tolower(ch);


String Function(string.h)



1.strcpy()

strcpy() manipulates function is used to copy the characters of one string to another.
Syntax
  char*strcpy(char*destination, const char*source);
Ex. #include<iosream.h>
#include<string.h>
void main()
{
 char *s1=”ace”;
 char *s2=”bag”;
strcpy(s1, s2);
cout<<s1;
}


2.strcat()

This function concatenates a copy of s2 to s1 and terminates s1 with a null.s1 should be large enough to hold both original contents and those of s2.
Syntax
           char *strcat(char *str1, const char *str2);
Ex.
     char *s1=”one”;
     char *s2=”two”;
     strcat(s2, s1);
     cout<<s2;


3.strlen()

It is used to find the length of a string between starting character and the terminating null character. The null is not counted.
Syntax
             int strlen(const char *str);
Ex.  char s1[50];
        strcpy(s1, “My name is Alex”);
        cout<<strlen(s1);

       output-15


4.strcmp()

This function alphabetically compares two strings and returns -ve value is s1 is less than s2; 0 if s1 is equal to s2, and >0 (+ve value) if s1 is greater than s2.
Syntax
             int strcmp(cont char*str1, const char *str2);
ex.  char *s1=”Ace” ;char *s2=”Bag”;
        cout<<strcmp(s2,s1);                      (>0)
       cout<<strcmp(s1,s2);                      (<0)


5.strcmpi()

Compare string without case sensitivity.
Syntax
             int strcmpi(const char *s1, const char *s2);
ex.
  if (0==strcmpi(“hello”, “HELLO”))
      cout<<”this string are equivalent”;
 else
     cout<<”this string are not equivalent”;


6.strrev()

This function reverses all the characters in string(except for the terminating null).
Syntax
             char *strrev(char *str);
Ex.  char *str1=”Ace”;
        cout<<strrev(str1);


7.strupr()

This function will convert all the lower case characters in the string to uppercase.
Syntax
             char *strlwr(char *str);
Ex. char *s1=”HELLO”;
      cout<<strlwr(s1);


Mathematical function (math.h)



1.fabs()

fabs() is a mathematical function that returns the absolute value of the given argument(real number)
Syntax
            float fabs(float num);

                                                                   output
Ex.  cout<<fabs(2.5426);                        2.542600
 cout<<fabs(-8.6);                                   8.600000


2.pow()

This function returns the “base” value raised to the “exp” power. A domain error is triggered if the “base” value is “zero” and the exponent is less than or equal to zero.
Syntax
             float pow(float base, float exp);
             float pow(float base, int exp);
Ex.
#include<iostream.h>
#include<math.h>
int main()
{
  cout<<”2 to the power of 2 is:”<<pow(2,2)<<endl;
  cout<<”4 to the power of 2 is:”<<pow(4,2)<<endl;
}


3.sqrt()

This Mathematical function that returns the square root of the given “num” value.
Syntax
             float sqrt(float num);
            double sqrt(double num);
             long double sqrt(long double num);
Ex.    #include<iostream.h>
         #include<math.h>
         int main()
       {
          float r;
         r=sqrt(4);
         cout<<”square root of 4:”<<r;
        return();
        }


4.sin()

This function that returns sin value of the given argument represented in radian.
Syntax
           float sin(float arg);
Ex.
  #include<iostream.h>
 #include<math.h>
 int main()
{
 cout<<”The sine value of 60 degree is:”<<sin(60)<<endl;
 cout<<”The sine value of 90 degree is”<<sin(90)<<endl;
return 0;
}

Result
 The sine value of 60 degree is: -0.304811
 The sine value of 90 degree is: 0.893997


5.cos()

This mathematical function returns the cosine value for the given radians.
Syntax
       float cos (float arg);
       double cos(double arg);
       long double cos(long double arg);
Ex.
      #include<iostream.h>
      #include<math.h>
       const double PI=3.14159265
       int main()
        {
            cout<<”The cosine value of 0 is:”<<cos(0)<<endl;
            cout<<”the cosine value of PI is:”<<cos(PI)<<endl;
            return 0;
        }


6.abs()

 This function represents the absolute value of integer number.
Syntax
   int abs(int n);

Ex.
       int c=-15;
      cout<<abs(c);


Other Function (stdlib.h)

random() and randomize() function is just like rand() and srand() to    generate random no.
In order to generate in user’s specified range we can use two more macros, also defined in stdlib.h they are random() and randomize().

To generate a random number between 0 to 9 you can write
random(10);
Because random() function generate random number maximum up to n-1 and between L-U.
So.
    random(U-L+1) +L
Ex.
   #include<iostream.h>
  #include<stdlib.h>
  #include<conio.h>
 #include<time.h>
  main()
{
  int num, i;
  randomize();
  for (i=0; i<5; ++i)
 {
 num=random(14-5+1) +5;
 cout<<num<< ‘  ‘;
 }
getch();
}

Output:     1st run                  7  8  8  14  5
                 2nd run                 13  6  13  9  6
                 3rd run                 9   9   6   7    10
                 4th run                 5   13   11   12   5  








This post is about function in c++. In the next post we will discuss the topic User -Defined Function.

Post a Comment

0 Comments