0% found this document useful (0 votes)
28 views6 pages

Lab Manual 12

The lab manual for Spring 2020 at UMT Lahore focuses on understanding functions in C++, including user-defined and built-in functions. It outlines objectives, provides examples of function declarations and definitions, and includes tasks for students to apply their knowledge, such as calculating the area of a triangle and creating a mini calculator. The manual is designed to enhance students' programming skills through practical exercises.

Uploaded by

groundedblogger1
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)
28 views6 pages

Lab Manual 12

The lab manual for Spring 2020 at UMT Lahore focuses on understanding functions in C++, including user-defined and built-in functions. It outlines objectives, provides examples of function declarations and definitions, and includes tasks for students to apply their knowledge, such as calculating the area of a triangle and creating a mini calculator. The manual is designed to enhance students' programming skills through practical exercises.

Uploaded by

groundedblogger1
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/ 6

Lab Manual (Lab 12)

Session: Spring 2020

School of Systems and Technology


UMT Lahore Pakistan

LAB INSTRUCTOR: AYESHA MAJID ALI


Contact: [email protected] Room #: 504 Last cabin 4th floor SST
Objective
The objective of the lab is to understand the concept of functions.

Functions
Objectives

● To introduce the concept of int and void functions


● To work with void functions that have no parameters
● To introduce and work with void functions that have parameters
● To introduce the concept of functions that return a value

TYPES OF FUNCTION:

1. User Defined Functions


2. Built in Functions

What are built in functions?

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

USER DEFINED FUNCTIONS:

The functions that we declare and write in our programs are user-defined functions.

HOW TO DECLARE A FUNTCION?


METHOD 1:
You can declare a function in the beginning and can define it after int main() function.

#include<iostream>
using namespace std;

void message(); //Function declaration

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:

FUNCTION WITH NO RETURN TYPE:

Void Function:

Example:

#include <iostream>
using namespace std;

void printmessage ()
{
cout << "I'm a function!";
}

int main ()
{
printmessage ();
}

Is same as the following program

#include <iostream>
using namespace std;

void printmessage (void)


{
cout << "I'm a function!";
}

int main ()
{
printmessage ();
}

Output:

FUNCTIONS WITH RETURN TYPE:

#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;
}

Find the error in the following code.

#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:

Create a mini calculator using functions.

You might also like