The document discusses functions in C++, including defining functions, parameter passing, inline functions, and default arguments. It covers:
- Defining functions with return types, function names, parameters, and bodies
- Passing parameters by value and by reference
- Using the inline keyword to avoid function call overhead for small functions
- Specifying default values for parameters so functions can be called without passing all arguments
INTRODUCTION
COMPARISON BETWEEN NORMAL FUNCTION AND INLINE FUNCTION
PROS AND CONS
WHY WHEN AND HOW TO USED?
GENERAL STRUCTURE OF INLINE FUNCTION
EXAMPLE WITH PROGRAM CODE
It tells about functions in C++,Types,Use,prototype,declaration,Arguments etc
function with
A function with no parameter and no return value
A function with parameter and no return value
A function with parameter and return value
A function without parameter and return value
Call by value and address
Functions in C++, this presentation will cover the following topics
• Functions
• Functions Basics
• Overloaded functions
o Different numbers of arguments
o Different kinds of arguments
Revision Fucntion overloading
• Inline functions
• Default arguments
INTRODUCTION
COMPARISON BETWEEN NORMAL FUNCTION AND INLINE FUNCTION
PROS AND CONS
WHY WHEN AND HOW TO USED?
GENERAL STRUCTURE OF INLINE FUNCTION
EXAMPLE WITH PROGRAM CODE
It tells about functions in C++,Types,Use,prototype,declaration,Arguments etc
function with
A function with no parameter and no return value
A function with parameter and no return value
A function with parameter and return value
A function without parameter and return value
Call by value and address
Functions in C++, this presentation will cover the following topics
• Functions
• Functions Basics
• Overloaded functions
o Different numbers of arguments
o Different kinds of arguments
Revision Fucntion overloading
• Inline functions
• Default arguments
We all have good and bad thoughts from time to time and situation to situation. We are bombarded daily with spiraling thoughts(both negative and positive) creating all-consuming feel , making us difficult to manage with associated suffering. Good thoughts are like our Mob Signal (Positive thought) amidst noise(negative thought) in the atmosphere. Negative thoughts like noise outweigh positive thoughts. These thoughts often create unwanted confusion, trouble, stress and frustration in our mind as well as chaos in our physical world. Negative thoughts are also known as “distorted thinking”.
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
What is the purpose of the Sabbath Law in the Torah. It is interesting to compare how the context of the law shifts from Exodus to Deuteronomy. Who gets to rest, and why?
How to Make a Field invisible in Odoo 17Celine George
It is possible to hide or invisible some fields in odoo. Commonly using “invisible” attribute in the field definition to invisible the fields. This slide will show how to make a field invisible in odoo 17.
The Roman Empire A Historical Colossus.pdfkaushalkr1407
The Roman Empire, a vast and enduring power, stands as one of history's most remarkable civilizations, leaving an indelible imprint on the world. It emerged from the Roman Republic, transitioning into an imperial powerhouse under the leadership of Augustus Caesar in 27 BCE. This transformation marked the beginning of an era defined by unprecedented territorial expansion, architectural marvels, and profound cultural influence.
The empire's roots lie in the city of Rome, founded, according to legend, by Romulus in 753 BCE. Over centuries, Rome evolved from a small settlement to a formidable republic, characterized by a complex political system with elected officials and checks on power. However, internal strife, class conflicts, and military ambitions paved the way for the end of the Republic. Julius Caesar’s dictatorship and subsequent assassination in 44 BCE created a power vacuum, leading to a civil war. Octavian, later Augustus, emerged victorious, heralding the Roman Empire’s birth.
Under Augustus, the empire experienced the Pax Romana, a 200-year period of relative peace and stability. Augustus reformed the military, established efficient administrative systems, and initiated grand construction projects. The empire's borders expanded, encompassing territories from Britain to Egypt and from Spain to the Euphrates. Roman legions, renowned for their discipline and engineering prowess, secured and maintained these vast territories, building roads, fortifications, and cities that facilitated control and integration.
The Roman Empire’s society was hierarchical, with a rigid class system. At the top were the patricians, wealthy elites who held significant political power. Below them were the plebeians, free citizens with limited political influence, and the vast numbers of slaves who formed the backbone of the economy. The family unit was central, governed by the paterfamilias, the male head who held absolute authority.
Culturally, the Romans were eclectic, absorbing and adapting elements from the civilizations they encountered, particularly the Greeks. Roman art, literature, and philosophy reflected this synthesis, creating a rich cultural tapestry. Latin, the Roman language, became the lingua franca of the Western world, influencing numerous modern languages.
Roman architecture and engineering achievements were monumental. They perfected the arch, vault, and dome, constructing enduring structures like the Colosseum, Pantheon, and aqueducts. These engineering marvels not only showcased Roman ingenuity but also served practical purposes, from public entertainment to water supply.
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
Synthetic fiber production is a fascinating and complex field that blends chemistry, engineering, and environmental science. By understanding these aspects, students can gain a comprehensive view of synthetic fiber production, its impact on society and the environment, and the potential for future innovations. Synthetic fibers play a crucial role in modern society, impacting various aspects of daily life, industry, and the environment. ynthetic fibers are integral to modern life, offering a range of benefits from cost-effectiveness and versatility to innovative applications and performance characteristics. While they pose environmental challenges, ongoing research and development aim to create more sustainable and eco-friendly alternatives. Understanding the importance of synthetic fibers helps in appreciating their role in the economy, industry, and daily life, while also emphasizing the need for sustainable practices and innovation.
Read| The latest issue of The Challenger is here! We are thrilled to announce that our school paper has qualified for the NATIONAL SCHOOLS PRESS CONFERENCE (NSPC) 2024. Thank you for your unwavering support and trust. Dive into the stories that made us stand out!
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
In this webinar you will learn how your organization can access TechSoup's wide variety of product discount and donation programs. From hardware to software, we'll give you a tour of the tools available to help your nonprofit with productivity, collaboration, financial management, donor tracking, security, and more.
Unit 8 - Information and Communication Technology (Paper I).pdfThiyagu K
This slides describes the basic concepts of ICT, basics of Email, Emerging Technology and Digital Initiatives in Education. This presentations aligns with the UGC Paper I syllabus.
How to Split Bills in the Odoo 17 POS ModuleCeline George
Bills have a main role in point of sale procedure. It will help to track sales, handling payments and giving receipts to customers. Bill splitting also has an important role in POS. For example, If some friends come together for dinner and if they want to divide the bill then it is possible by POS bill splitting. This slide will show how to split bills in odoo 17 POS.
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
OOP-Module-1-Section-4-LectureNo1-5.pptx
1. Functions In C++
Presented by:
Mr. Sachin Sharma, Dr. Vinod Jain , Ms. Kriti Bansal
Assistant Professor, Dept. Of CE&A
GLA University, Mathura
2. Syllabus
4.0 Functions In C++
4.1 Call by Value & Reference
4.2 Inline Function
4.3 Default Arguments
4.4 Function Overloading.
<Subject Code> <Name of Subject> 2
5. 4.0 Functions In C++
• A function is a group of statements that together perform a task.
• Every C++ program has at least one function, which is main()
6. 4.0 Functions In C++
A function declaration tells the compiler about a function's name,
return type, and parameters.
A function definition provides the actual body of the function.
Defining a Function
The general form of a C++ function definition is as follows −
return_type function_name( parameter list )
{
body of the function
}
8. 4.0 Functions In C++
• Return Type
• A function may return a value.
• The return_type is the data type of the
value the function returns.
• Some functions perform the desired
operations without returning a value. In
this case, the return_type is the
keyword void.
int fact(int no)
{
int i,f=1;
for(i=1;i<=no;i++)
f=f*i;
return(f);
}
9. 4.0 Functions In C++
• Function Name −
• This is the actual name of the function.
• The function name and the parameter
list together constitute the function
signature.
• Example
• Here function name is fact
int fact(int no)
{
int i,f=1;
for(i=1;i<=no;i++)
f=f*i;
return(f);
}
10. 4.0 Functions In C++
• Parameters −
• When a function is invoked, you pass a
value to the parameter.
• The parameter list refers to the type,
order, and number of the parameters of
a function.
• Parameters are optional; that is, a
function may contain no parameters.
• Example (int no)
int fact(int no)
{
int i,f=1;
for(i=1;i<=no;i++)
f=f*i;
return(f);
}
11. 4.0 Functions In C++
• Function Body −
• The function body contains a collection of
statements that define what the function
does.
• Example Function body here is
{
int i,f=1;
for(i=1;i<=no;i++)
f=f*i;
return(f);
}
int fact(int no)
{
int i,f=1;
for(i=1;i<=no;i++)
f=f*i;
return(f);
}
12. 4.0 Functions In C++
• // A program to write and use a function to calculate factorial of a
number
13.
14. 4.0 Functions In C++
// A program to write and use a function to
// calculate factorial of a number
#include<iostream>
#include<conio.h>
using namespace std;
//function prototype
int fact(int);
//main function
int main()
{
int n,factorial;
cout<<"Enter the n : "<<endl;
cin>>n;
//calculate factorial using function.
factorial=fact(n);
//print factorial
cout<<"Factorial of "<<n<<" is "<<factorial;
return 0;
}
//function
int fact(int no)
{
int i,f=1;
for(i=1;i<=no;i++)
f=f*i;
return(f);
}
Output
Enter the no.
5
Factorial of 5 is 120
15. 4.0 Functions In C++
• Parameter Passing to functions
1. Actual Parameters
2. Formal parameters
16. 4.0 Functions In C++
• Parameter Passing to functions
Actual Parameters
• The parameters passed to function are called actual parameters.
• For example, in the above program 5 is actual parameters.
• Formal parameters
• The parameters received by function are called formal parameters.
• For example, in the above program no is a formal parameter.
21. 4.1 Call by Value
• Call by Value:
• In this parameter passing method, values of actual parameters are
copied to function’s formal parameters.
• The two types of parameters are stored in different memory
locations.
• So any changes made inside functions are not reflected in actual
parameters of caller.
22.
23. 4.1 Call by Value
• // Call by value
• #include <iostream>
• using namespace std;
•
• // function declaration
• void swap(int x, int y);
•
• int main () {
• // local variable declaration:
• int a = 100;
• int b = 200;
•
• cout << "Before swap, value of a :" << a << endl;
• cout << "Before swap, value of b :" << b << endl;
•
• // calling a function to swap the values.
• swap(a, b);
•
• cout << "After swap, value of a :" << a << endl;
• cout << "After swap, value of b :" << b << endl;
• return 0;
• }
• // function definition to swap the values.
• void swap(int x, int y) {
• int temp;
• temp = x;
• x = y;
• y = temp;
•
• return;
• }
• Output
• Before swap, value of a :100
• Before swap, value of b :200
• After swap, value of a :100
• After swap, value of b :200
24. 4.1 Call by Reference
• call by reference
• The call by reference method of passing arguments to a function
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 passed
argument.
25. 4.1 Call by Reference
• call by reference
• To pass the value by reference, argument reference is passed to the
functions just like any other value.
• So accordingly you need to declare the function parameters as
reference types as in the following function swap(), which exchanges
the values of the two integer variables pointed to by its arguments.
void swap(int &x, int &y)
{
}
26. 4.1 Call by Reference
• Program to swap two numbers using call by reference
• Make a swap() function which pass parameters by reference
void swap(int &x, int &y) {
int temp;
temp = x;
x = y;
y = temp;
return;
}
30. 4.2 Inline Function
• The inline functions are a C++ enhancement feature to decrease the
execution time of a program.
• Inline function is a function that is expanded in line when it is called.
• When the inline function is called whole code of the inline function
gets inserted or substituted at the point of inline function call.
• Functions can be instructed to compiler to make them inline so that
compiler can replace those function definition wherever those are
being called.
32. 4.2 Inline Function :
How to make function inline:
To make any function as inline, start its definitions with the keyword
“inline”.
Example
inline int cube(int s)
{
return s*s*s;
}
34. 4.2 Inline Function
• #include <iostream>
• using namespace std;
• inline int cube(int s)
• {
• return s*s*s;
• }
• int main()
• {
• cout << "The cube of 3 is: " << cube(3) << "n";
• return 0;
• } //Output: The cube of 3 is: 27
35. 4.2 Inline Function : Why to use –
• In many places we create the functions for small work/functionality
which contain simple and less number of executable instruction.
• Imagine their calling overhead each time they are being called by
callers.
•
36. 4.2 Inline Function : Why to use –
1. When a normal function call instruction is encountered,
2. the program stores the memory address of the instructions
immediately following the function call statement,
3. loads the function being called into the memory, copies argument
values, jumps to the memory location of the called function,
4. executes the function codes, stores the return value of the function,
5. and then jumps back to the address of the instruction that was
saved just before executing the called function.
6. Too much run time overhead.
37. 4.2 Inline Function : Why to use –
• The C++ inline function provides an alternative.
• With inline keyword, the compiler replaces the function call
statement with the function code itself (process called expansion)
and then compiles the entire code.
• Thus, with inline functions, the compiler does not have to jump to
another location to execute the function, and then jump back as the
code of the called function is already available to the calling program.
38. 4.2 Inline Function : Pros :-
1. It speeds up your program by avoiding function calling overhead.
2. It save overhead of variables push/pop on the stack, when function
calling happens.
3. It save overhead of return call from a function.
4. It increases locality of reference by utilizing instruction cache.
5. You can put a function definition in a header file and it can be
included in multiple compilation unit.
39. 4.2 Inline Function : Cons
1. It increases the executable size due to code expansion.
2. Which means if you change the code of the inlined function, you
would need to recompile all the code using it to make sure it will be
updated
3. As mentioned above it increases the executable size, which may
cause thrashing in memory. More number of page fault bringing
down your program performance.
4. Sometimes not useful for example in embedded system where large
executable size is not preferred at all due to memory constraints.
40. 4.2 Inline Function : When to use -
• Function can be made as inline as per programmer need.
• Some useful recommendation are mentioned below-
• 1. Use inline function when performance is needed.
• 2. Use inline function over macros.
41. 4.2 Inline Function : Restrictions
• Inlining is only a request to the compiler, not a command.
• Compiler can ignore the request for inlining.
• Compiler may not perform inlining in such circumstances like:
• 1) If a function contains a loop. (for, while, do-while)
• 2) If a function contains static variables.
• 3) If a function is recursive.
• 4) If a function return type is other than void, and the return statement
doesn’t exist in function body.
• 5) If a function contains switch or goto statement.
44. 4.3 Default Arguments
• In C++ programming, we can provide default values
for function parameters.
• If a function with default arguments is called without passing
arguments, then the default parameters are used.
• However, if arguments are passed while calling the function, the
default arguments are ignored.
int sum(int x, int y, int z=0, int w=0)
{
return (x + y + z + w);
}
45. 4.3 Default Arguments
• A default argument is a value provided in a function declaration that
is automatically assigned by the compiler if the caller of the function
doesn’t provide a value for the argument with a default value.
int sum(int x, int y, int z=0, int w=0)
{
return (x + y + z + w);
}
49. 4.3 Default Arguments : program
• // Program of default argument
• #include<iostream>
• using namespace std;
•
• // A function with default arguments, it can be called with
• // 2 arguments or 3 arguments or 4 arguments.
• int sum(int x, int y, int z=0, int w=0)
• {
• return (x + y + z + w);
• }
• int main()
• {
• cout << sum(10, 15) << endl;
• cout << sum(10, 15, 25) << endl;
• cout << sum(10, 15, 25, 30) << endl;
• return 0;
• }
50. 4.3 Default Arguments : Key Points:
• Default arguments are different from constant arguments as constant
arguments can't be changed whereas default arguments can be
overwritten if required.
• Default arguments are overwritten when calling function provides
values for them. For example, calling of function sum(10, 15, 25, 30)
overwrites the value of z and w to 25 and 30 respectively.
• During calling of function, arguments from calling function to called
function are copied from left to right. Therefore, sum(10, 15, 25) will
assign 10, 15 and 25 to x, y, and z. Therefore, the default value is used
for w only.
51. 4.3 Default Arguments
• When Function overloading is done along with default values. Then
we need to make sure it will not be ambiguous.
• The compiler will throw error if ambiguous.
54. 4.4 Function Overloading
• Function overloading is a feature in C++ where two or more functions
can have the same name but different parameters.
• The definition of the function must differ from each other
• 1. By number of arguments in the argument list.
• 2. By types of arguments in the argument list.
• 3. Or Both in number and types
• You cannot overload function declarations that differ by return type.
55. 4.4 Function Overloading
• Examples
• int myFunction() { }
• int myFunction(int a) { }
• float myFunction(double a) { }
• int myFunction(int a, double b) { }
56. 4.4 Function Overloading
• The advantage of Function overloading is that it increases the
readability of the program because you don't need to use different
names for the same action.
59. 4.4 Function Overloading Example
• // Function Overloading Example
• #include <iostream>
• using namespace std;
•
• void display(int);
• void display(float);
• void display(int, float);
•
• int main()
• {
•
• int a = 5;
• float b = 5.5;
•
• display(a);
• display(b);
• display(a, b);
•
• return 0;
• }
• void display(int var) {
• cout << "Integer number: " << var << endl;
• }
• void display(float var) {
• cout << "Float number: " << var << endl;
• }
• void display(int var1, float var2) {
• cout << "Integer number: " << var1;
• cout << " and float number:" << var2;
• }
60. 4.4 Function Overloading and Ambiguity
• When the compiler is unable to decide which function is to be
invoked among the overloaded function, this situation is known
as Ambiguity in function overloading.
• When the compiler shows the ambiguity error, the compiler does not
run the program.
61. 4.4 Function Overloading and Ambiguity
• Causes of Ambiguity in Function Overloading:
1. Type Conversion.
2. Function with default arguments.
3. Function with pass by reference.
63. Reason
The fun(1.2) calls the second function
according to our prediction.
But, this does not refer to any function as in
C++, all the floating point constants are
treated as double not as a float.
So 1.2 is treated as double not float.
If we replace float to double, the program
works.
64. 4.4 Function Overloading and Ambiguity
// Ambiguity in Function overloading
// 1. By Type conversion
#include<iostream>
using namespace std;
void fun(int);
void fun(float);
void fun(int i)
{
std::cout << "Value of i is : " <<i<< std::endl;
}
void fun(float j)
{
std::cout << "Value of j is : " <<j<< std::endl;
}
int main()
{
fun(12);
fun(1.2);
return 0;
}
67. Explanation
1. The above example shows an error "call
of overloaded 'fun(int)' is ambiguous".
2. The fun(int a, int b=9) can be called in
two ways:
3. first is by calling the function with one
argument, i.e., fun(12)
4. and another way is calling the function
with two arguments, i.e., fun(4,5).
5. The fun(int i) function is invoked with
one argument i.e. fun(12).
6. Therefore, the compiler could not be
able to select among fun(int i) and
fun(int a,int b=9) and show an error
message that function call is
ambiguous.
68. 4.4 Ambiguity by Default Arguments
• // Ambiguity in Function overloading
• // By Default Arguments
• #include<iostream>
• using namespace std;
•
• void fun(int i)
• {
• cout << "Value of i is : " <<i<< std::endl;
• }
• void fun(int a,int b=9)
• {
• cout << "Value of a is : " <<a<< std::endl;
• cout << "Value of b is : " <<b<< std::endl;
• }
• int main()
• {
• fun(12);
• return 0;
• }
71. Explanation
1. The above example shows an
error "call of overloaded
'fun(int&)' is ambiguous".
2. The first function takes one
integer argument and the second
function takes a reference
parameter as an argument.
3. In this case, the compiler does
not know which function is
needed by the user as there is no
syntactical difference between
the fun(int) and fun(int &).
72. 4.4 Ambiguity by Call By Reference
// Ambiguity in Function overloading
// Ambiguity because of call by reference
include <iostream>
using namespace std;
void fun(int);
void fun(int &);
int main()
{
int a=10;
fun(a); // error, which fun()?
return 0;
}
void fun(int x)
{
std::cout << "Value of x is : "
<<x<< std::endl;
}
void fun(int &b)
{
cout << "Value of b is : " <<b<<
std::endl;
}
[Error] call of overloaded 'fun(int&)' is
ambiguous