Pointers
Pointers
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.
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:
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
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.
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.
– Pass-by-value
– Simulate pass-by-reference
– Arrays not passed with & because array name already pointer
#include <iostream>
using std::cout;
using std::endl;
int main()
int number = 5;
cout << "\nThe new value of number is " << number << endl;
} // end main
#include <iostream>
using std::cout;
using std::endl;
int main()
int number = 5;
cubeByReference( &number );
cout << "\nThe new value of number is " << number << endl;
} // end main
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>
if (choice==1)
{
return 0;
}
OUTPUT:
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>
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)
{
return 0;
}
Task 3
Write a program that contains a function which takes some integer types of arguments
Note:
: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;
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>
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
1. 10
2. 10
3. 10
Total 30 Signature
Note : Attempt all tasks and get them checked by your Lab Instructor