You are on page 1of 22

20CS411 – DATA STRUCTURES USING C++

Title of Topic: Parameter passing methods, Inline functions


Course Code: 20CS411
Course Title: Data Structures Using C++
Session Number: 1.8
Faculty Name: Ms. S. Padmavathi
Academic Year: 2021 – 2022 (Even Sem)

MODULE 1 1
20CS411 – DATA STRUCTURES USING C++

MODULE – 1

C++ PROGRAMMING
An overview of C++ - Difference between procedure and object oriented
programming language, Data Types, Variables, Operators, Expressions
and Statements-Functions and Arrays- C++ Class Overview- Class
Definition, Objects, Class Members, Access Control, Constructors and
destructors, parameter passing methods, Inline functions, static class
members, this pointer, friend functions, dynamic memory allocation and
deallocation (new and delete) - Inheritance basics, base and derived
classes, inheritance types, runtime polymorphism using virtual functions,
abstract classes - Generic Programming- Function and class templates.

MODULE 1 2
20CS411 – DATA STRUCTURES USING C++

MODULE 1 - C++ PROGRAMMING

1.1 An overview of C++ - Difference between procedure and object


oriented programming language
1.2 Data Types, Variables

1.3 Operators, Expressions and Statements

1.4 Functions

1.5 Arrays
1.6 C++ Class Overview- Class Definition, Objects, Class Members

1.7 Access Control - Constructors and destructors

1.8 Parameter passing methods, Inline functions

MODULE 1 3
20CS411 – DATA STRUCTURES USING C++

MODULE 1 - C++ PROGRAMMING

1.9 Static class members, this pointer

1.10 Friend functions, dynamic memory allocation and deallocation (new

and delete)
1.11 Inheritance basics, base and derived classes, inheritance types

1.12 Runtime polymorphism using virtual functions

1.13 Abstract classes


1.14 Generic Programming- Function and class templates

MODULE 1 4
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

Function
• Functions are building blocks of the programs
• It makes the programs more modular, easy to read and
manage.
• All C++ programs must contain the function main().
• The execution of the program starts from the function
main( )

MODULE 1 5
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

• There are different ways in which parameter data can be


passed into and out of methods and functions
• A function B() is called from another function A().
• In this case,
• A is called the “caller function”
• B is called the “called function or callee function”
• The arguments which A sends to B are called actual
arguments and the parameters of B are called formal
arguments.

MODULE 1 6
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

Semantics Models of Parameter Passing


Formal parameters are characterized by one of three distinct
semantics models:
• They can receive data from the corresponding actual parameter,
called in mode. i.e., Passes info from caller to callee.
• They  can transmit data to the actual parameter, called out mode.
i.e., Callee writes values in caller.
• Inout mode - Caller indicates callee with the value of variable,
which may be updated by callee.

MODULE 1 7
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

MODULE 1 8
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

C++ supports three types of argument passing:


• Pass by Value
• Pass by Reference
• Pass by Address

MODULE 1 9
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

Call by Value
• This method uses in-mode semantics.
• Passing the argument to the function at calling position.
• In call by value, original value can not be changed or modified.
#include <iostream> int main()
using namespace std; {
int a =10;
void fun(int a) fun(a);
{ cout<<"Value of A:
a=20; "<<a<<endl;
} return 0;
}

MODULE 1 10
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

MODULE 1 11
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods


Example – Call by Value
#include <iostream>
using namespace std;
void my_swap(int x, int y) {    
int temp;     Output:
temp = x;   (a,b) = (10, 40)
 x = y;     (a,b) = (10, 40)
y = temp;
}
int main()
{    int a, b;    
a = 10;    b = 40;  
cout << "(a,b) = (" << a << ", " << b << ")\n";  
 my_swap(a, b);  
 cout << "(a,b) = (" << a << ", " << b << ")\n"; }
MODULE 1 12
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods


Pass by address

• Call by address works by passing the address of the


variables into the function.
• When the function updates on the value pointed by that
address, the actual value will be updated automatically.

MODULE 1 13
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

MODULE 1 14
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods


Example - Pass by address

#include <iostream> int main() {  


using namespace std;  int a, b;    
void my_swap(int *x, int *y) {     a = 10;    
int temp;   b = 40;  
 temp = *x;      cout << "(a,b) = (" << a << ", " << b
*x = *y;     << ")\n";  
*y = temp;  my_swap(&a, &b);    
} cout << "(a,b) = (" << a << ", " << b
<< ")\n";
}
MODULE 1 15
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

Pass by reference
• Pass by reference uses in/out-
mode semantics.
• Pass the reference variable of
the argument, so for updating
it, the actual value will be
updated.
• Only at the function definition,
put & before variable name

MODULE 1 16
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods


Example - Pass by reference
#include <iostream>
using namespace std;
void my_swap(int &x, int &y) {  
 int temp;    
temp = x;  
 x = y;    
y = temp;
}
int main() {  
 int a, b;  
 a = 10;    
b = 40;  
 cout << "(a,b) = (" << a << ", " << b << ")\n";    
my_swap(a, b);    
cout << "(a,b) = (" << a << ", " << b << ")\n"; }
MODULE 1 17
20CS411 – DATA STRUCTURES USING C++

1.8 Parameter passing methods

Pass by Result
• This method uses out-mode semantics.
• The control is transferred back to the caller, the value of the
formal parameter is transmitted back to the actual
parameter.
• This method is sometimes called call by result.

MODULE 1 18
20CS411 – DATA STRUCTURES USING C++

1.8 Inline Functions

• An inline function is a function that expanded in line when it is


invoked.
• That is the compiler replaces the function call with the
corresponding function code .

Syntax:
inline function-header
{
Function body
}

MODULE 1 19
20CS411 – DATA STRUCTURES USING C++

1.8 Inline Functions

• Any changes made to an inline function will require the inline


function to be recompiled again because the compiler would
need to replace all the code with a new code; otherwise, it
will execute the old functionality.
• When the inline function is encountered, then the definition of
the function is copied to it.

MODULE 1 20
20CS411 – DATA STRUCTURES USING C++

1.8 Inline Functions


#include <iostream>
Example
int multiply(int);
int main( )
{
int x;
cout<< "\n Enter the Input Value: ";
cin>>x;
cout<<"\n The Output is: " << multiply(x);
}
inline int multiply(int x1)
{ return 5*x1; }

MODULE 1 21
20CS411 – DATA STRUCTURES USING C++

1.8 Inline Functions Once the compilation is done, the


#include <iostream>   code would be like as shown as below:
using namespace std;  
inline int add(int a, int b)   #include<iostream>  
{   using namespace std;  
    return(a+b);   inline int add(int a, int b)  
}   {  
int main()        return(a+b);   
{   }  
    cout<<"Addition of 'a' and 'b' is:" int main()  
<<add(2,3);   {  
    return 0;         cout<<"Addition of 'a' and 'b' is
   :"<<return(2+3);  
}       return 0;  
}   

The main use of the inline function in C++ is to save memory


space. 
MODULE 1 22

You might also like