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

Pointers

Uploaded by

aaahsan.ch
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)
12 views

Pointers

Uploaded by

aaahsan.ch
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/ 16

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 02
(Spring 2024)

Course: Object Oriented Programming Lab Date: 20/02/2024


Course Code: Max Marks: 30
Faculty’s Name: Rizwan Khalid

Name: MUHAMMAD AHSAN Enroll No:

Objective(s):
Topic Function Basic
 Functions
o Need and importance of functions
 Divide and conquer strategy
 Reusability
o Predefine functions, user define functions
Objective o Value returning function, void function
o Function Prototypes, benefit of function prototype.
o Function Definition
o Function Calling
o Formal parameter, Actual parameter
o Value parameter, Reference parameter

Lab Description:

This lab is basically designed for the basics of functions and the importance of functions.

What is function?

It is a block of code that is used for a specific task to reduce number of lines in a program.

Importance of Function:

 A program may need to repeat the same piece of code at various places.
 It may be required to perform certain task repeatedly.
 The program may become very large if functions are not used.
 The real reason for using function is to divide a problem into sub problems
Advantages of Function:

 Easier to Code
 Easier to Modify
 Easier to Maintain
 Reusability
 Less Programming Time
 Easier to Understand
Types of Function:

Pre-define Function:

In C++, predefined functions are organized into separate libraries. For example, the header file iostream
contains I/O functions, and the header file cmath contains math functions.

User-define Function:

C++ does not provide every function that you will ever need and designers cannot possibly know a user’s
specific needs, you must learn to write your own functions. Those functions which are defined by user
are called user define functions.

User-defined functions in C++ are classified into two categories:

Value-returning functions

Functions that have a return type. These functions return a value of a specific data type using the return
statement, which we will explain shortly. Note that the function main has used a return statement to
return the value 0 in every program we’ve seen so far.

Void functions

Functions that do not have a return type. These functions do not use a return statement to return a
value.

We will first discuss value-returning functions. Many of the concepts discussed in regard to value-
returning functions also apply to void functions.
Function prototype:

The function heading, terminated by a semicolon, without the body of the function. By reading the
prototype you should understand the working of the function.

Function call:

That activates a function is known as Function call. The following steps take place when a function is
called:

 The control moves to the function that is called.


 All statements in function body are executed.
 Control returns back to calling statement.
In a function call, the number of actual parameters, together with their data types, must match with the
formal parameters in the order given. That is, actual and formal parameters have a one-to-one
correspondence.

Function definition:

A function is called (or invoked, or executed) by providing the function name, followed by the
parameters being sent enclosed in parentheses. To invoke a function, see the syntax below

Formal parameter and Actual parameter:

When a function is called, the value of the actual parameter is copied into the corresponding formal
parameter. If the formal parameter is a value parameter, then after copying the value of the actual
parameter, there is no connection between the formal parameter and actual parameter; That is, the
formal parameter is a separate variable with its own copy of the data. Therefore, during program
execution, the formal parameter manipulates the data stored in its own memory space.

Value Parameter:

After copying data, a value parameter has no connection with the actual parameter, so a value
parameter cannot pass any result back to the calling function. When the function executes, any changes
made to the formal parameters do not in any way affect the actual parameters. The actual parameters
have no knowledge of what is happening to the formal parameters. Thus, value parameters cannot pass
information outside of the function. Value parameters provide only a one-way link from the actual
parameters to the formal parameters.

Reference Parameters:

A reference parameter receives the address (memory location) of the actual parameter, reference
parameters can pass one or more values from a function and can change the value of the actual
parameter.

Reference parameters are useful in three situations:

When the value of the actual parameter needs to be changed, when you want to return more than one
value from a function, when passing the address would save memory space and time relative to copying
a large amount of data.

Function Calling Methods

• 3 ways to pass arguments to function

– Pass-by-value

– Pass-by-reference with reference arguments

– Pass-by-reference with pointer arguments

• Arguments passed to function using reference arguments

– Modify original values of arguments

– More than one value “returned”

• Pass-by-reference with pointer arguments

– Simulate pass-by-reference

• Use pointers and indirection operator

– Pass address of argument using & operator

– Arrays not passed with & because array name already pointer

– * operator used as alias/nickname for variable inside of function


// Cube a variable using pass-by-value.

#include <iostream>

using std::cout;

using std::endl;

int cubeByValue( int ); // prototype

int main()

int number = 5;

cout << "The original value of number is " << number;

// pass number by value to cubeByValue

number = cubeByValue( number );

cout << "\nThe new value of number is " << number << endl;

return 0; // indicates successful termination

} // end main

int cubeByValue( int n )

return n * n * n; // cube local variable n and return result

} // end function cubeByValue


// Cube a variable using pass-by-reference

// with a pointer argument.

#include <iostream>

using std::cout;

using std::endl;

void cubeByReference( int * ); // prototype

int main()

int number = 5;

cout << "The original value of number is " << number;

// pass address of number to cubeByReference

cubeByReference( &number );

cout << "\nThe new value of number is " << number << endl;

return 0; // indicates successful termination

} // end main

void cubeByReference( int *nPtr )

*nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr

} // end function cubeByReference


Lab Tasks
Task 1 Temperature Problem:

Make two function F_to C(float temp1 ); and C_to_F(float temp2); and convert the temperature entered
by the user

Fahrenheit to Celsius:

C = 5/9 x (F-32)
Celsius to Fahrenheit:

°F = (°C × 9/5) + 32

Program:
#include<iostream>

using namespace std;

float FahrenheitTOCelsius( float fahrenhiet)


{
float calsius;
calsius= (fahrenhiet-32.00)*5.00/9.00;
return calsius;
}
float CelsiusTOFahrenheit( float celsius)
{
float fahrenhiet;
fahrenhiet= (celsius+32.00)*9.00/5.00;
return fahrenhiet;
}
int main()
{
float Fahrenheit , Celsius;
int choice;
cout<<"---------------Temperature Converter---------------"<<endl;
cout<<"Enter 1 for(F TO C) Enter 2 for(C TO F) ->"<<endl;
cin>>choice;

if (choice==1)
{

cout<<"Enter the temperature in Fahrenheit ->";


cin>> Fahrenheit;
cout<< FahrenheitTOCelsius( Fahrenheit)<<"degree C" <<endl;
}
else if (choice==2)
{

cout<<"Enter the temperature in Calsius ->";


cin>> Fahrenheit;
cout<< CelsiusTOFahrenheit( Celsius)<<"degree F" <<endl;
}
else
{
do{
cout<<"Invalid Input "<<endl;
cin>>choice;
}
while(choice!=1 && choice!=2);

return 0;
}

OUTPUT:

Task 2 Mathematics problem:

We have multiple predefined mathematics function. Let’s try to define these function on our own.

Square root:

The method accepts an integer value as input find square root and returns a double integer as output.

Power:
The function returns the result of the first argument raised to the power of the second argument.

Floor:

The floor function returns the largest possible integer value which is equal to the value or smaller than
that.

Ceil:

The ceil function returns the smallest possible integer value which is equal to the value or greater than
that

Program:
#include<iostream>

using namespace std;

float FahrenheitTOCelsius( float fahrenhiet)


{
float calsius;
calsius= (fahrenhiet-32.00)*5.00/9.00;
return calsius;
}
float CelsiusTOFahrenheit( float celsius)
{
float fahrenhiet;
fahrenhiet= (celsius+32.00)*9.00/5.00;
return fahrenhiet;
}

int main()
{
float Fahrenheit , Celsius;
int choice;
cout<<"---------------Temperature Converter---------------"<<endl;
cout<<"Enter 1 for(F TO C) Enter 2 for(C TO F) ->"<<endl;
cin>>choice;

if (choice==1)
{

cout<<"Enter the temperature in Fahrenheit ->";


cin>> Fahrenheit;
cout<< FahrenheitTOCelsius( Fahrenheit)<<"degree C" <<endl;
}
else if (choice==2)
{

cout<<"Enter the temperature in Calsius ->";


cin>> Fahrenheit;
cout<< CelsiusTOFahrenheit( Celsius)<<"degree F" <<endl;
}
else
{
do{
cout<<"Invalid Input "<<endl;
cin>>choice;
}
while(choice!=1 && choice!=2);

return 0;
}

Task 3

Write a program that contains a function which takes some integer types of arguments

and display sum in main function without returning any value.

Note:

1. Don’t use cout statement in the function.


2. Don’t use global variables;
3. There must be three variables in a main function num1, num2 and sum;
4. Num1 and num2 use to input the values from user in the main().
5. The result of addition must be saved in the variable of sum.
6. Return type of function must be void

:Program
#include <iostream>
using namespace std;
void add(int a ,int b, int &sum)
{
sum= a+b;

}
int main ()
{
int num1 ;
int num2;
int sum;

cout << "Enter 1st Number :>";


cin>> num1;
cout<<"Enter 2nd Number :>";
cin>> num2;
add( num1, num2 , sum);
cout<< sum;
return 0; }

OUTPUT
Task 4
Write a program that contains a function with the name of find_fibonacci_terms(int
terms) function will display the fibonaccii terms.

Program:
#include <iostream>

using namespace std;


void find_fibonacci_terms(int terms) {
int a = 0, b = 1;
cout << "Fibonacci Sequence:" << endl;
cout << a << " ";
for (int i = 1; i < terms; ++i) {
cout << b << " ";
int next_term = a + b;
a = b;
b = next_term;
}
cout << endl;
}

int main() {
int num_terms;
cout << "Enter the number of Fibonacci terms you want to find: ";
cin >> num_terms;
if (num_terms <= 0) {
cout << "Please enter a positive integer." << endl;
return 1;
}
find_fibonacci_terms(num_terms);
return 0;
}

OUTPUT

Lab Grading Sheet :


Max Obtained
Task Comments(if any)
Marks Marks

1. 10

2. 10

3. 10

Total 30 Signature
Note : Attempt all tasks and get them checked by your Lab Instructor

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy