0% found this document useful (0 votes)
47 views

Hitec Univeristy, Taxila Cantt Engineering Department of Electrical

The document is a lab report submitted by Fariha Saeed to Engr. Anza Aqeel detailing functions and tasks completed in Lab #12. It defines functions, syntax, declaring, defining, and calling functions. It also details 3 tasks completed: 1) making a function to compute the square of a number, 2) adding two integers using a function, and 3) selecting and performing math operations on two integers using a switch statement. The conclusion states the lab helped learn about function declaration, definition, and calling.

Uploaded by

Fariha Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Hitec Univeristy, Taxila Cantt Engineering Department of Electrical

The document is a lab report submitted by Fariha Saeed to Engr. Anza Aqeel detailing functions and tasks completed in Lab #12. It defines functions, syntax, declaring, defining, and calling functions. It also details 3 tasks completed: 1) making a function to compute the square of a number, 2) adding two integers using a function, and 3) selecting and performing math operations on two integers using a switch statement. The conclusion states the lab helped learn about function declaration, definition, and calling.

Uploaded by

Fariha Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

Declaring, Defining and Calling Function

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 addition (int a, int b)

{int r;

r=a+b;

return r; }

int main ()

int z;

z = addition (5,3);

cout << "The result is " << z; }

Result:

Task # 03:

Program:

#include<iostream>
using namespace std;

int main()

{ int num1, num2, selection;

cout << "Please enter an integer: ";

cin >> num1;

cout << "Please enter another integer: ";

cin >> num2;

cout << "\n\n\n";

cout << "Select the desired function:\n";

cout << "1. Addition\n";

cout << "2. Subtraction\n";

cout << "3. Multiplication\n";

cout << "4. Division\n";

cout << "5. Modular Division\n";

cout << "Selection: ";

cin >> selection;

cout << "\n\n\n";


switch(selection)

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.

You might also like