Professional Documents
Culture Documents
WINSEM2021-22 CSE1002 LO VL2021220501708 Reference Material I 08-03-2022 Default Arguements
WINSEM2021-22 CSE1002 LO VL2021220501708 Reference Material I 08-03-2022 Default Arguements
#include<iostream>
using namespace std;
• 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
• Once default value is used for an argument in function definition, all subsequent arguments to it
must have default value. It can also be stated as default arguments are assigned from right to left.
For example, the following function definition is invalid as subsequent argument of default variable
z is not default
C++ Inline Functions
• In C++, we can declare a function as inline. This copies the function to the
location of the function call in compile-time and may make the program
execution faster
• To create an inline function, we use the inline keyword. For example,
inline returnType functionName(parameters)
{ // code }
C++ Inline Function
#include <iostream>
using namespace std;
}
Here is how this program works:
Develop a CPP code to calculate the cube using
inline functions
SOlution
#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3);
return 0;
}
Advantages of Inline function
int main() {
int number;
If (condition)
{
// block of code if condition is true
}
else
{
// block of code if condition is false
}
Example code for If-Else
#include <iostream>
using namespace std;
int main() {
int number;
• The if...else statement is used to execute a block of code among two alternatives.
However, if we need to make a choice between more than two alternatives, we use
the if...else if...else statement. if (condition1)
{
// code block 1
}
else if (condition2)
{
// code block 2
}
else
{
// code block 3
}
C++ Nested if...else
• Sometimes, we need to use an if statement inside another if statement. This
is known as nested if statement. Think of it as multiple layers
of if statements
• There is a first, outer if statement, and inside it is another,
inner if statement.
Example code for Nested If
#include <iostream> if ((num % 2) == 0) {
using namespace std; cout << "The number is even." << endl;
}
int main() { // inner else condition
int num; else {
cout << "The number is odd." << endl;
cout << "Enter an integer: "; }
cin >> num; }
// outer else condition
// outer if condition else {
if (num != 0) { cout << "The number is 0 and it is neither even nor odd."
<< endl;
// inner if condition }
cout << "This line is always printed." << endl;
}
For loop
• In computer programming, loops are used to repeat a block of code.
• For example, let's say we want to show a message 100 times. Then instead of
writing the print statement 100 times, we can use a loop.
• That was just a simple example; we can achieve much more efficiency and
sophistication in our programs by making effective use of loops.
There are 3 types of loops in C++.
• for loop
• while loop
• do...while loop
C++ for loop
Syntax:
for (initialization; condition; update)
{
// body of-loop
}
Flowchart
Printing Numbers From 1 to 5
#include <iostream>
using namespace std;
Int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << i << " ";
}
return 0;
}
Find the sum of first n Natural Numbers
#include <iostream>
int main() {
int num, sum;
sum = 0;
return 0;
}
C++ while loop
• A while loop evaluates the condition
• If the condition evaluates to true, the code inside the while loop is executed.
• The condition is evaluated again.
• This process continues until the condition is false.
• When the condition evaluates to false, the loop terminates.
• Flowchart
Sum of Positive Numbers Only
#include <iostream>
using namespace std;
• The do...while loop is a variant of the while loop with one important
difference:
• the body of do...while loop is executed once before the condition is checked.
• The body of the loop is executed at first. Then the condition is evaluated.
• If the condition evaluates to true, the body of the loop inside the do statement is
executed again.
• The condition is evaluated once again.
• If the condition evaluates to true, the body of the loop inside the do statement is
executed again.
• This process continues until the condition evaluates to false. Then the loop stops
Sum of Positive numbers
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
// condition to continue
if (i == 3) {
continue;
}
return 0;
}
C++ switch..case Statement
return 0;
}
C++ goto Statement