Functions in C++

Using Functions in C++?

Explanation

A Function is a set of statements that can be used anywhere in the program. Main reason for using it is to strcuture the programmming by creating function for repetitive tasks.

Syntax:


return-type function-name(parameter list)
{
Statements
}

In the above syntax the "return-type" defines the data type of the value returned by the functions, the "function-name", unique name used to call a function. The "Parameters" are seperated by commas that consist of the data type along with identfiers for the parameters.

Example :



#include<iostream.h>
int x,y,z,a,b,r;
addition(int a, int b)
{
r = a + b;
return (r);
}
int main()
{
cout << "Enter the first integer::"; cin >> x;
cout << "Enter the second integer::"; cin >> y;
z = addition(x,y);
cout << "Sum of X and Y is::" << z << '\n';
}

Result :

Enter the first integer::10
Enter the second integer::20
Sum of X and Y is:: 30

In the above example a function "addition()" is used to add two numbers.

C++ Tutorial


Ask Questions

Ask Question