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

04.Basics of C++ functions

The document provides an overview of basic concepts in C++, focusing on function overloading and the two methods of function calls: call by value and call by reference. Function overloading allows multiple functions to share the same name with different argument types, while call by value copies actual parameters to local parameters, preventing changes from affecting the original variables. In contrast, call by reference allows changes made within a function to affect the original variables by passing their memory addresses.

Uploaded by

Maniya Daxit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

04.Basics of C++ functions

The document provides an overview of basic concepts in C++, focusing on function overloading and the two methods of function calls: call by value and call by reference. Function overloading allows multiple functions to share the same name with different argument types, while call by value copies actual parameters to local parameters, preventing changes from affecting the original variables. In contrast, call by reference allows changes made within a function to affect the original variables by passing their memory addresses.

Uploaded by

Maniya Daxit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Basics of C++

Educator: Twinkle S. Panchal


Assistant Professor
Sutex Bank College of Computer
Applications and science
Contents
● Function overloading
● Call by Value
● Call by Reference
FUNCTION OVERLOADING:

Overloading refers to the use of the same name multiple forms.

This means that we can use the same function name to creates functions
that perform a variety of different tasks.

(This is known as function polymorphism in oops.)

Using the concepts of function overloading , a family of functions with one


function name but with different argument lists in the functions call .
The correct function to be invoked is determined by checking the number
and type of the arguments but not on the function type.

For example an overloaded add() function handles different types of data


as shown below.

//Declaration int add(int a, int b); //2

//prototype 1 int add (int a, int b, int c); //3

//prototype 2 double add(double x, double y); // data type different

//prototype 3 double add(double p , double q); // invalid


#include<iostream.h>

main( ) {

cout<<volume(10)<<endl;

cout<<volume(10,10.5)<<endl;

int volume( int s) { return (s*s*s); //cube }

double volume(int r, double h) { return(3.1416*r*r*h); //cylinder } }

output:- 1000

3297
Functions can be called by two ways:

1) call by value
2) call by reference
Call By Value
● In this parameter passing method, values of actual parameters are
copied to function’s local parameters and the two types of parameters
are stored in different memory locations.

● So any changes made inside functions are not reflected in actual


parameters of calling function.
a = 10 b = 20
2390 2391

x
x== 20
10 y
y== 10
20

Values are copied to local


variables and changes will
be only to local variable.
Explanation of output:
● As you can see in the output, although we swapped the values of
variable a and b inside the function swap()

● still in the main() method those changes are not passed on.

● Because actual parameters will be copied to local parameter and


they are at different locations with different values
Call By Reference
● Both the actual and local parameters refer to same locations,

● so any changes made inside the function are actually reflected in


actual parameters of calling function.
a = 10
20 b = 10
1002 1004

x = 1002 y = 1004

2002 2004

1) Address of actual variable is


passed to local variable
2) Swapping will be applied to value
at given address
Let’s Revise it again !!
Call By Reference Call By Value
Call By Value Call By Reference
While calling a function, we pass values of While calling a function, instead of passing the
variables to it. values of variables, we pass address of
Such functions are known as “Call By Values”. variables(location of variables) to the function
known as “Call By References”.
In Call By Value, the value of each variable in In Call by Reference, the address of actual
calling function is copied into local variables of the variables in the calling function are copied into the
called function. local variables of the called function.
In Call By Value, the changes made to the local In Call by Reference, the changes made to the local
variables in the called function have no effect on variables in the called function have effect on the
the values of actual variables in the calling values of actual variables in the calling function..
function.
In Call By Value, the values of variables are passed In Call by Reference, the pointer variables are
by Simple technique. necessary to define to store the address values of
variables.
Thank you

You might also like