You are on page 1of 9

American International University Bangladesh (AIUB)

Faculty of science & Technology


Department of Computer Science

LAB MANUAL 03
CSC 2207 Programming Language 2 [EEE]

1
TITLE

Functions: User define function and library functions

PREREQUISITE

• knowledge about variable and data types


• Knowledge about different Operators.
• Knowledge about conditional statement and iteration (loops).

OBJECTIVE

• To know about function prototype, implementation.


• To know about library function and user define function
• To learn about function overloading.
• To learn about template function and default arguments.
• To be able to solve the exercises, lab work and assignments at the end of this manual

THEORY

User Define Function in C++

User-defined functions are the functions which are created by the C++ programmer, so that
he/she can use it many times. It reduces complexity of a big program and optimizes the code.

#include <iostream>
using namespace std;

// Function prototype (declaration)


int add(int, int);

int main()
{
int num1, num2, sum;
cout<<"Enters two numbers to add: ";
cin >> num1 >> num2;
// Function call
sum = add(num1, num2);
cout << "Sum = " << sum;

2
return 0;
}

//Function definition
int add(int a, int b)
{
int add;
add = a + b;
// Return statement
return add;
}

Standard Library Functions: Predefined in C++

Library Functions are the functions which are declared in the C++ header files such as ceil(x),
cos(x), exp(x) sqrt (square root) etc. from C++ math Header().

#include <iostream>
#include <cmath>
using namespace std;

int main() {
double number, squareRoot;
number = 25.0;
// sqrt() is a library function to calculate the square root
squareRoot = sqrt(number);
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}

User Define function and library function example

#include<iostream>
#include<cmath>
using namespace std;

int factorial(int number)


{
int product = 1;
for ( ; number > 0 ; number--)
{
product *= number;
}
return product;
}

3
int main()
{
int whole_number;
cout << "Enter a positive integer:\n";
cin>> whole_number;
cout << "The factorial of " << whole_number << " is ";
cout<< factorial(whole_number);
cout << ", and the square root of " << whole_number << " is ";
cout << sqrt(whole_number) << ".\n";
return 0;
}

Call By Reference

The call by reference method of passing arguments to a function copies the address of an
argument into the formal parameter. Inside the function, the address is used to access the actual
argument used in the call. It means the changes made to the parameter affect the passed
argument.

#include<iostream>
using namespace std;

void swap(int &x, int &y){


int temp;
temp = x;
x=y;
y = temp;
}

int main(){

int a=10;
int b = 20;
swap(a,b);
cout<<a<<" "<<b;

return 0;
}

Call By Reference Example 2

4
#include<iostream>
using namespace std;

int area(int length, int width)


{
return length * width;
}
void get_dimensions(int &length, int &width)
{
cout << "Enter the length: ";
cin >> length;
cout << "Enter the width: ";
cin>> width;
cout << "\n";
}
int main()
{
int this_length, this_width;
get_dimensions(this_length, this_width);
cout << "The area of a " << this_length << "x" << this_width;
cout << " rectangle is " << area(this_length, this_width);

return 0;
}

Function Template

#include<iostream>
using namespace std;

template<class T>
T maximum(T a, T b){
return a>b?a:b;
}

int main(){
cout<<maximum(10,15)<<endl;
cout<<maximum(10.20f,20.2f)<<endl;
cout<<maximum(2.5,5.6)<<endl;
return 0;
}

Function: Default Arguments

5
#include<iostream>
using namespace std;

int add(int a, int b, int c=0){


return a+b+c;
}

int main(){
cout<<add(5,5,5)<<endl;
return 0;
}

Function: Constant Arguments

#include<iostream>
using namespace std;

void print(const int &n) {


//n++; // gives you error because of constant
cout << n<<endl;
}

int main(){

int x=10000000;
print(x);
x++;
cout<<x<<endl;
return 0;
}

Function: Random Number Generation

#include <iostream >


using namespace std;
#include <cstdlib > // C standard library
// defines rand(), srand(), RAND_MAX
#include <ctime> // C time functions - defines time()

int main() {

srand(time(0) ); // Set the seed;

// time(0) returns current time as a number

6
int randNum = rand()%100+1;

cout << "A random number: " << randNum << endl;
return 0;
}

Polymorphism and Overloading

#include<iostream>
using namespace std;

int average(int x, int y, int z)


{
return ((x + y + z) / 3);
}

int average(int x, int y)


{

return ((x + y) / 2);


}

double average(double x, double y)


{
return ((x + y) / 2);
}

int main()
{
int a = 5, b = 3, c = 10;
cout << "The integer average of " << a << " and ";
cout<< b << " is ";
cout << average(a, b) << ".\n\n";
cout << "The integer average of " << a << ", ";
cout << b << " and " << c << " is ";
cout << average(a, b, c) <<".\n";
cout<<"Double function: "<<average(2.0,3.4);
return 0;
}

Splitting Programs into Different Files

1. Create a project in code blocks.


7
2. Create heade file as function prototype , name: averages.h
#ifndef AVERAGES_H
#define AVERAGES_H
int average(int a, int b, int c);
int average(int a, int b);
#endif

3. implements function definition in averages.cpp(must use #include "averages.h")


#include <iostream>
#include "averages.h"
using namespace std;

int average(int a, int b, int c)


{
return ((a + b + c) / 3);
}
int average(int a, int b)
{
return ((a + b) / 2);
}

4. Use those function in main function(main.cpp). (must use #include "averages.h")

#include <iostream>
#include "averages.h"
using namespace std;

int main()
{
int a = 5, b = 3, c = 10;
cout << "The integer average of " << a << " and ";
cout<< b << " is ";
cout << average(a, b) << ".\n\n";
cout << "The integer average of " << a << ", ";
cout << b << " and " << c << " is ";
cout << average(a, b, c) <<".\n";
return 0;
}

8
LAB WORK

1. Write a program to calculate the area of Rectangle. Use function and take input length
and width from user.
2. Write a program in C++ to convert temperature in Celsius to Fahrenheit and Fahrenheit
to Celsius. Use two Functions.
3. Write a program that find a numbers is even or odd. Hint: use function
4. Write a program in C++ to check whether a number is prime or not. Use function
Sample Output:
Input a number to check prime or not: 13
The entered number is a prime number.
5. Write a program in C++ to find the factorial of a number. use function
Sample output:
Input a number to find the factorial: 5
The factorial of the given number is: 120
6. Write a program that takes two inputs from the user and adds, subtracts, multiplies and
divide it and also print the output. Use function.
7. Write a program to find area of circle. Hints: Area = PI x radius x radius. Use function.

ASSIGNMENT

1. Write a program in C++ to find the first 10 natural numbers.


Sample output:
The natural numbers are: 1 2 3 4 5 6 7 8 9 10
2. Write a program in C++ to find the sum of first 10 natural numbers. use function.

Sample Output:

The sum of first 10 natural numbers: 55

3. Write a program in C++ to display n terms of natural number and their sum. Use function.

Sample Output:

Input a number of terms: 7

The natural numbers up to 7th terms are: 1 2 3 4 5 6 7

The sum of the natural numbers is: 28

You might also like