Hitec Univeristy, Taxila Cantt Engineering Department of Electrical
Hitec Univeristy, Taxila Cantt Engineering Department of Electrical
Lab Report # 12
Functions
Submit To:
Engr. Anza Aqeel
Submitted by:
Fariha Saeed
Registration no:
17-EE-088
Section:
“A”
Date: 15-01-2018
Lab # 12
Functions:
Functions are used to provide modularity to a program. Creating an application using
function makes it easier to understand, edit, check errors etc.
Syntax of Function
return-type function-name (parameters)
// function-body
return-type: suggests what the function will return. It can be int, char, some pointer
or even a class object. There can be functions which does not return anything, they
are mentioned with void.
Function Name: is the name of the function, using the function name it is called.
Parameters: are variables to hold values of arguments passed while function is
called. A function may or may not contain parameter list.
Function declaration, is done to tell the compiler about the existence of the function.
Function's return type, its name & parameter list is mentioned. Function body is written in its
definition.
Calling a Function
Functions are called by their names. If the function is without argument, it can be called
directly using its name. But for functions with arguments, we have two ways to call them,
1. Call by Value
2. Call by Reference
Call by Value:
In this calling technique we pass the values of arguments which are stored or copied into
the formal parameters of functions. Hence, the original values are unchanged only the
parameters inside function changes.
Call by Reference
In this we pass the address of the variable as arguments. In this case the formal parameter
can be taken as a reference or a pointer, in both the case they will change the values of the
original variable.
Task # 01:
Make a function to compute the square of a given number.
Program:
#include <iostream>
#include<conio.h>
using namespace std;
inline int sqr(int num)
{
return num*num;
}
int main()
{
float n;
cout<<"enter a number";
cin>>n;
cout<<"square="<<sqr(n)<<endl;
getch();
}
Result:
Task # 02:
#include <iostream>
using namespace std;
{int r;
r=a+b;
return r; }
int main ()
int z;
z = addition (5,3);
Result:
Task # 03:
Program:
#include<iostream>
using namespace std;
int main()
case 1:
cout << "The sum of " << num1 << " and " << num2 << " is " << num1+num2 << endl;
break;
case 2:
cout << "The difference of " << num1 << " and " << num2 << " is " << num1-num2 << endl;
break;
case 3:
cout << "The product of " << num1 << " and " << num2 << " is " << num1*num2 << endl;
break;
case 4:
cout << "The quotient of " << num1 << " and " << num2 << " is " << num1/num2 << endl;
break;
case 5:
cout << "The remainder of " << num1 << " and " << num2 << " is " << num1%num2 << endl;
break;
default:
cout << "Invalid selection\n"; }
return 0; }
Result:
Conclusion:
After this lab we are able to make different programs using functions. We have learnt
about function declaration, function definition and function calling.