C++ Course Notes - Functions and Pointers - Week 4
C++ Course Notes - Functions and Pointers - Week 4
Topics to be covered:
Functions
Pointers
Functions are used to perform certain actions, and they are important for reusing code: Define the code
once, and use it many times.
Why functions ?
For example , below written code is to write a function to sum two numbers.
#include<iostream>
using namespace std;
int main(){
cout<<sum(4 , 5);
}
Output :
Explanation :
In this code, we have written a separate function named as sum , that accepts two parameters a and
b and returns an integer, which is the sum of the passed parameters.
Components of a function :
Function name
Function return type
Argument list (Optional)
Function name : This is name of the function that the user chooses. In general practice, function name
does not start with uppercase character.
For example, myCode , difference are valid function names.
Function return type : This defines what type of data will be returned by the function. It can be int ,
char , string , void etc.
Argument list : This is a list of parameters that we will be passing in our function. There can be any
number of parameters in a function. A function can also be created without any argument list therefore
this is optional.
Declaration: the return type, the name of the function, and parameters (if any)
Definition: the body of the function (code to be executed)
Note : A function must be declared before it is called. This will become more clear with the help of the
below given example :
int main() {
functionWithPW();
return 0;
}
void functionWithPW() {
cout << "Hello pwians";
}
// Error
The above stated code will generate an error, since functionWithPW is not declared before its call.
To avoid this error we can split the definition and declaration of the function in two different parts as
shown in the example below:
//function declaration
void functionWithPW();
//main program
int main() {
functionWithPW();
return 0;
}
//function definition
void functionWithPW() {
cout << "Hello pwians";
}
Pass by value
Pass by reference
Pass by value : This method copies the actual value of an argument into the formal parameter of the
function. In this case, changes made to the parameter inside the function have no effect on the argument.
For example :
#include<iostream>
using namespace std;
return;
}
int main(){
int a = 6 , b = 8;
cout<<"Values of a and b respectively before swap : "<<a<<" "<<b<<endl;
swap(a,b);
cout<<"Values of a and b respectively after swap : "<<a<<" "<<b<<endl;
}
Output :
Here we can see the values have not been swapped. This is because we have passed the parameters by
value that means a copy of a and b have been passed and the changes made by the swap function are
observed in those copies and not reflected back in the original variables. This is pass by value.
Pass by reference : This method copies the reference of an argument into the formal parameter. Inside the
function, the reference is used to access the actual argument used in the call. This means that changes
made to the parameter affect the argument.
For example,
#include<iostream>
using namespace std;
return;
}
int main(){
int a = 6 , b = 8;
cout<<"Values of a and b respectively before swap : "<<a<<" "<<b<<endl;
swap(a,b);
cout<<"Values of a and b respectively after swap : "<<a<<" "<<b<<endl;
}
Output :
Here, we have passed the address of the original variables a and b . Therefore all the changes made by
the swap function are reflected in the original variables as well.
Formal Parameter : A variable and its type as they appear in the prototype of the function or method.
Actual Parameter : The variable or expression corresponding to a formal parameter that appears in the
function or method call in the calling environment.
Data types will not be mentioned. The data type must be included.
They are written in the function call. They are written in the function definition.
Scope of a variable : scope of a variable is defined as the extent of the program code within which the
variable can be accessed or declared or worked with.
#include<iostream>
using namespace std;
void function1(){
int a = 5 ;
}
void myFunction(){
cout<<a<<" ";
}
int main(){
function1();
myFunction();
}
Global variables : they can be accessed from any part of the program. They are available through out
the life time of a program. They are declared at the top of the program outside all of the functions or
blocks.
For example,
#include<iostream>
using namespace std;
int b = 10;
void myFunction(){
cout<<b<<endl;
}
int main(){
myFunction();
cout<<b<<endl;
}
Output :
10
10
What is a pointer ?
The pointer is a variable, it is also known as locator or indicator that points to an address of a value. The
symbol of an address is represented by a pointer. In addition to creating and modifying dynamic data
structures, they allow programs to emulate call-by-reference.
Dereferencing operator(*) : Dereferencing is the method where we are using a pointer to access the
element whose address is being stored. We use the * operator to get the value of the variable from its
address.
int a;
// Referencing
int *ptr=&a;
// Dereferencing
int b=*ptr;
int array[5]={2,4,6,8,11}
ptr++; //pointer increment won’t add 1 to address instead it move to the next immediate
index.
int array[5]={1,2,3,4,5}
int *ptr=array;
ptr++;
ptr–; //pointer decrement will decrement to one less index in the array therefore we
have first incremented to the next index so that it might not result in runtime error.
Double pointers : A pointer stores the memory address of other variables. So, when we define a pointer to
a pointer, the first pointer is used to store the address of the variables, and the second pointer stores the
address of the first pointer. For this very reason, this is known as a Double Pointer or Pointer to Pointer.
For example,
#include <bits/stdc++.h>
using namespace std;
int main(){
int data = 1;
int* pointer1;
int** pointer2;
pointer1 = &data;
pointer2 = &pointer1;
Output :
Value of data :- 1