HOME C,C++ HOME



tally erp9 training

PROGRAMMING IN C++

FUNCTIONS

FUNCTIONS


A function is a set of statements that perform a specific task. The specified task is repeated each time when the function is called. In large computing tasks, functions are break into smaller ones. They work together to accomplish the goal of the whole program. Moreover, functions increase the modularity of the program and reduce code redundancy.

Functions must be defined outside any other enclosing block where a block is delimited by braces ‘{ }’. The following terms are associated with functions.

* Function declaration - Introduces the function in the program.
* Function definition - It contains the function code.
* Function call - It used to execute the function.
* Function parameters - Data that is passed to the function before it starts execution.
* Function return type - Data type of the value returned after the function has completed the execution.
* Calling function - Function that executes another function.
* Called function - Function that is executed by another function.

// EX-13: TO FIND THE VOLUME OF CYLINDER USING FUNCTION

#include<iostream.h>
#include<conio.h>
float volume(float r, float h);
void main()
{
float x,y,z;
clrscr();
cout<<endl<<"Enter the value for radius: ";
cin>>x;
cout<<endl<<"Enter the value for height: ";
cin>>y;
z=volume(x,y);
cout<<endl<<"Volume of the cylinder: "<<z<<endl;
getch();
}

float volume(float r,float h)
{
float v;
v=((1.0/3.0)*((22.0/7.0)*r*r*h));
return(v);
}

getline():
The member function of the cin object getline() is used to accept data from the standard input device. It provides the flexibility of retrieving strings with embedded spaces, from the keyboard, which is not possible using the operator, ‘>>’, of the cin object.

Syn:
cin.getline(<string_name>,<size_of_string+1>);

Example:
char name[21];
cin.getline(name,21);

ignore():
If you need to invoke the getline() function immediately after a call to the operator, ‘>>’, of the cin object, the new line character needs to be extracted from the input buffer. This is done by calling the ignore() function.

Note: The functions getline() and ignore() are comes under the header file iostream.h

COMPARISON OF STRINGS:
The function strcmp() compares the two strings supplies as its parameters and returns an integer value. To use this function, you need to include the file, string.h.

• If the first argument is less than the second, strcmp() returns a value less than zero (negative number).
• If the first argument is equal to the second, the function returns a value equal to zero.
• If the first argument is greater than the second, the function returns a value greater than zero (positive number).

Syn: strcmp(<string1>,<string2>);

// Ex-14: PGM ILLUSTRATES THE FUNCTION strcmp()

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char s1[]="GOD";
char s2[]="GOD";
cout<<strcmp(s1,s2)<<endl;
char s3[]="sumi";
char s4[]="SUMI";
cout<<strcmp(s3,s4)<<endl;
char s5[]="SUJI";
char s6[]="SUMI";
cout<<strcmp(s5,s6)<<endl;
getch();
}