Lab Manual 12
Lab Manual 12
Functions
Objectives
TYPES OF FUNCTION:
Built-in functions are also called library functions. These are the functions that are provided by C++ and
we need not write them ourselves. We can directly use these functions in our code.
Example:
pow(2,5)
Mean 2^5=32
The functions that we declare and write in our programs are user-defined functions.
#include<iostream>
using namespace std;
int main()
{
message() ; // Function Call
cout<< "\n this is main function" ;
message();
}
void message( ) // Function definition
{
cout<<"\n this is message function" ;
}
Output:
METHOD 2:
You can declare a function and you can also define it with its declaration.
#include<iostream>
using namespace std;
void message()
{
cout<<"\n this is message function" ;
} //Function declaration and definition
int main()
{
message() ; // Function Call
cout<< "\n this is main function" ;
}
Output:
Void Function:
Example:
#include <iostream>
using namespace std;
void printmessage ()
{
cout << "I'm a function!";
}
int main ()
{
printmessage ();
}
#include <iostream>
using namespace std;
int main ()
{
printmessage ();
}
Output:
#include <iostream>
using namespace std;
int mul ()
{
int a=2*3;
return a;
}
int main ()
{
int m=mul ();
cout<<"The product is product:"<<m<<endl;
}
#include <iostream>
using namespace std;
void add ()
{
int a=2+3;
return a;
}
int main ()
{
int sum=add ();
cout<<"The sum is sum:"<<sum<<endl;
}
Lab Tasks
.
Task 1
If the lengths of the sides of a triangle are denoted by a, b, and c, then area of triangle is given by
area = √ 2
S (S−a)(S−b)(S−c ¿)¿ where, S = ( a + b + c ) / 2. Write a program which includes a
function to find the sides of triangle.
Task 2
(Even or Odd) Write a program that inputs an integer value and passes them to function even, which uses
the remainder operator to determine if an integer is even.
Task 3:
Write a program to swap the values of two number using built in function.
Task 4: