Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Module8 PointerwithFunctions

1. There are three main ways to pass arguments to functions in C++: by value, by reference, and by address. Passing by value copies the value, passing by reference passes the reference, and passing by address passes the address. 2. Passing by value is best for primitive types but is inefficient for large objects. Passing by reference allows modifying the original argument but provides no type safety. Passing by address is useful for arrays but is slower than other methods. 3. The document provides examples to illustrate each method and discusses their advantages and disadvantages. The key is choosing the right method based on whether the argument needs to be modified and its data type.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Module8 PointerwithFunctions

1. There are three main ways to pass arguments to functions in C++: by value, by reference, and by address. Passing by value copies the value, passing by reference passes the reference, and passing by address passes the address. 2. Passing by value is best for primitive types but is inefficient for large objects. Passing by reference allows modifying the original argument but provides no type safety. Passing by address is useful for arrays but is slower than other methods. 3. The document provides examples to illustrate each method and discusses their advantages and disadvantages. The key is choosing the right method based on whether the argument needs to be modified and its data type.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

www.sakshieducation.

com

Pointers with Functions and Arrays


Pointers and Functions
In the functions module we learned about passing arguments to functions. The arguments can be
passed to functions in two ways;
1. Passing argument by value

m
2. Passing arguments by reference

co
Passing arguments by value

n.
By default the arguments in C++ are passed by value. In the previous module all the

io
examples are passed by value only. When the arguments are passed by value, the values of
arguments are copied into the function parameters. In passed by value method, any changes

at
made to formal arguments do not modify the actual arguments. The pass by value is suitable
uc
many cases so it has some advantages and disadvantages.
Advantages of pass by value:
ed

• Functions arguments passed by value can be variables, expressions, enumerators,


structures and classes.
hi

• Functions arguments are never changed by the called function, which prevents the side
ks

effects.

Disadvantages of pass by value:


a

• Copying structures and classes can result a significant performance penalty, especially if
.s

the function is called many times.


w

Let us consider an example which shows the variables in modified in the called function will not
w

modify variables in the calling function. In the below example we are calling swap function from
w

main () function and in the swap function we are swapping the arguments and displaying on the
console.

www.sakshieducation.com
www.sakshieducation.com

m
co
n.
io
at
uc
ed

Figure1: Example program for Pass by value


hi
a ks
.s
w
w
w

Figure2: Result of the program

Passing arguments by reference


The pass by value has the following limitations which will affect the performance. When
passing a large structure or class to a function using pass by value will make a copy of the
argument into the function parameter and it has the only way to return a value back to the calling

www.sakshieducation.com
www.sakshieducation.com
function is using functions return value. These problems will be overcome in pass by reference,
in which the actual value is not passed instead of this the reference of arguments passed to the
function. In the example program two integer variables are declared and those integer values are
passed to swap function by reference. When the function is called the variables are reference to
the arguments ‘a’ and ‘b’ in the main function. Here the reference to the variable is treated
exactly same as the variable itself, any changes made to the reference variables that will modify

m
the actual arguments.

co
n.
io
at
uc
ed
hi
a ks
.s
w
w
w

Figure3: Example program for pass by reference

www.sakshieducation.com
www.sakshieducation.com

m
co
n.
Figure4: Result of the program

io
Advantages of pass by reference:
• The pass by reference allows a function to change the value of the argument. This will be

at
useful sometime but not good. We can use const reference to guarantee the function
uc
won’t change the arguments if needed.
• It is fast because the copy of argument is not made even when used with large structures
ed

and classes.
• References must be initialized, so there is no problem about null values.
hi

• References can be used to return multiple values from a function.


ks

Disadvantages of pass by reference:


a

• It is impossible to tell from the function call whether the argument may modify or not.
.s

We can only know that an argument is passed by value or passed by reference by seeing
w

at function declaration.
w

There is another method to pass arguments to functions and that is passing an argument by
w

address. In this method the address of variable is passed during the function call rather than
variable itself, because the argument is an address, so the function parameter must be pointer.
The function can then dereference the pointer to access the value being pointed. Let us consider
an example program which increments integer values by passing address of variables ‘a’ and ‘b’.
Here passing the address of variables ‘a’ and address of variable ‘b’ in the function call, so the
receiving formal arguments in the function definition should be declared of pointer type.

www.sakshieducation.com
www.sakshieducation.com
Whenever the function ‘ref ()’ is called the two pointers to integer variable ‘p’ and ‘q’ are
created and they are initialized with the address of variables ‘a’ and ‘b’. By dereferencing the
pointers variables ‘p’ and ‘q’, we will able to access the variables ‘a’ and ‘b’. Before the ‘ref ()’
function the values of ‘a’ and ‘b’ are ‘7’.
The values of actual arguments are copied into pointer variables ‘p’ and ‘q’ and here the
actual arguments are addresses of variables ‘a’ and ‘b’. Since ‘p’ contain address of variable ‘a’

m
and ‘q’ contain the address of variable ‘b’ inside the ‘ref ()’ function. In the function the
variables ‘p’ and ‘q’ are incremented and the values are displayed on the console and come back

co
into main function. Here we can observe that the values of variables ‘a’ and ‘b’ are changed.
Finally we can say the by the passing arguments through reference and by address it will modify

n.
the actual arguments also.

io
at
uc
ed
hi
a ks
.s
w
w
w

Figure5: Example program for pass by address

www.sakshieducation.com
www.sakshieducation.com

m
co
n.
Figure6: Result of the program

io
Advantages of pass by address:
• The pass by address allows a function to change the value of the argument.

at
• It is fast because the copy of argument is not made even when used with large structures
uc
and classes.
• It can be used to return multiple values from a function.
ed

Disadvantages of pass by address:


hi

• Pointer arguments must be normal variables because the literals and expressions do not
ks

have addresses.
• Accessing arguments passed by address is slower than the accessing arguments passed by
a

value because the dereferencing pointer is slower than accessing a value directly.
.s

• All values must be checked to see whether they are null, because trying to dereference a
w

null value crash the program.


w

Î Use ‘passing arguments by value’ for fundamental data types and enumerators.
w

Î Use ‘passing arguments by reference’ when you need to modify an argument and passing
classes and structures.
Î Use ‘passing arguments by addresses’ for passing arrays.
The returning values from a called function to calling function by value and reference or
address works almost same way as passing arguments to a function. Each of these methods has
their own advantages and disadvantages. The more common problem is returning local variables

www.sakshieducation.com
www.sakshieducation.com
declared in the functions, because the local variables declared in a function will go out of scope
when the function returns.

Returning by value
Return by value is the simplest return type to use. When the values are returned by the
value, a copy of that value is returned to the calling function. Using return by value we can return

m
only variables, literals and expressions. The advantage of return by value is that we can return

co
variables or expressions that involved local variables declared within the function without having
any problem about scope of the variable, because the variables are evaluated before the function

n.
goes out of the scope and a copy of the value is returned to the calling function. So there is no
problem with scope of variables at the end of the function. The problem with return by value is

io
slow for structures and classes.

Return by reference
at
uc
The values returned by reference must be variables. When a variable is returned by
ed

reference, a reference to the variable is passed back to the calling function. The calling function
then uses this reference to modify the variable, when it needed. Return by reference is also fast
hi

and it is mostly useful when returning structures and classes. Return by reference is not used for
literals and expressions because the literals and expressions do not have addresses.
a ks

Return by address
.s

Returning by address involves returning the address of a variable to the calling function.
Return by address can only return the address of the variable, so it cannot be used for expressions
w

and literals, because return by address copies an address from the function to the calling
w

function. When using return by reference or address makes sure that you are not returning a
w

reference to or an address of a variable that will go out of the scope when the function returns.

Returning more than one value from a function


We can return only one value from a function using return statement. By using pass by
reference we can overcome this problem and return more than one value. Let us take one

www.sakshieducation.com
www.sakshieducation.com
example to better understand this concept. Let us take a function which perform addition,
subtraction and multiplication on two integer variables and return sum, difference and
multiplication to the calling function. In the following program the variables ‘a’ and ‘b’ are
passed by value and the variables ‘sum’, ‘diff’ and ‘mul’ are passed by reference . The function
‘multiple’ knows the address of variables ‘sum’, ‘diff’ and ‘mul’, so it can access these variables
indirectly using pointers and assign the appropriate values to them.

m
co
n.
io
at
uc
ed
hi
ks

Figure7: Example program for returning more than one value from a function
a
.s
w
w

Figure8: Result of the program


w

The relation between pointer and an array


The pointers and arrays are closely related. We can access array elements using pointer
expressions. By default the compiler converts the array subscript notation to pointer notation to
access the array elements. Let us consider an example program which displays the address and

www.sakshieducation.com
www.sakshieducation.com
value of each element in an array using array notation and pointer notation. In the below example
program we defined integer array of five elements.

m
co
n.
io
at
uc
ed
Figure9: Example program for accessing address and value using array notation and pointer
notation
hi
a ks
.s
w
w
w

Figure10: Comparison between array notation and pointer notation

www.sakshieducation.com
www.sakshieducation.com
Already we know that the name of the array holds the address of the first element in the array, so
the address of array can be known by ‘arr’ or ‘&arr[0]’ and this two points to the same memory
address location. The name of the array is a constant pointer and according to pointer arithmetic
when an integer is added to a pointer then the address of the next element is of same base type,
so ‘arr+1’ will give address of the next element.

m
co
n.
io
at
uc
Figure11: Result of the program
ed

Let us consider example program to read input values from user and display on the console using
pointer notation.
hi
a ks
.s
w
w
w

Figure12: Example program to read input values from user and display on the console using
pointer notation.

www.sakshieducation.com
www.sakshieducation.com

m
co
n.
io
Figure13: Result of the program

Array pointer

at
Till now we learn that a pointer is pointed to 0th element of array. We can also declare a pointer
uc
that can point to the whole array instead of only pointing one element of array. These types of
pointers are useful when talking about multidimensional arrays.
ed

Example:
int (*ptr)[5];
hi

Here ‘ptr’ is a pointer to array of five integers. The subscripts have the higher precedence than
ks

indirection operator, so it is necessary to enclose indirection operator and pointer name in the
parentheses. The pointer that points to 0th element of array and the pointer that points to the
a

whole array are different. Let us consider a program which will show the difference between
.s

this.
w
w
w

www.sakshieducation.com
www.sakshieducation.com

m
co
n.
io
at
uc
ed

Figure14: Example program for array pointer


In the above, the pointer ‘ptr’ points to 0th element of array, whereas the pointer ‘ptr1’ points to
hi

whole array. After arithmetic increment on both pointers the ‘ptr’ shifted forward only four bytes
and whereas ‘ptr1’ is shifter, forward 20 bytes since ‘ptr1’ is pointer to point array of five
ks

elements.
a
.s
w
w
w

Figure15: Result of the program

www.sakshieducation.com

You might also like