Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

(CPP)

Arguments, Inline functions &


Loops By
Dr Bandaru Kiran
Assistant Professor Senior
SCHEME
VIT Vellore
Mob: 7981343089
Default Arguements
• 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.
• .
Following is a simple C++ example to demonstrate the use of default arguments. We don’t
have to write 3 sum functions, only one function works by using default values for 3rd and
4th arguments
#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);
}

/* Driver program to test above function*/


int main()
{
cout << sum(10, 15) << endl;
cout << sum(10, 15, 25) << endl;
cout << sum(10, 15, 25, 30) << endl;
return 0;
}
• 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

#include<iostream>
using namespace std;

// A function with default arguments, it can be called with


/* Driver program to test above function*/
// 2 arguments or 3 arguments or 4 arguments.
int main()
int sum(int x, int y, int z=0, int w=0)
{
{
cout << sum(10, 15) << endl;
return (x + y + z + w);
cout << sum(10, 15, 25) << endl;
}
cout << sum(10, 15, 25, 30) << endl;
int sum(int x, int y, int z=0, float w=0)
return 0;
{
}
return (x + y + z + w);
}
• 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

• 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;

inline void displayNum(int num) {


cout << num << endl;
}
Output:
int main() { 5
// first function call 8
displayNum(5); 666

// second function call


displayNum(8);

// third function call


displayNum(666);

}
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

• Function call overhead doesn’t occur.


• It saves the overhead of a return call from a function.
• It saves the overhead of push/pop variables on the stack when the function is called.
• When we use the inline function it may enable the compiler to perform context-specific
optimization on the function body, such optimizations are not possible for normal function
calls.
• It increases the locality of reference by utilizing the instruction cache.
• An inline function may be useful for embedded systems because inline can yield less code
than the function call preamble and return.
CPP if Statement
if (condition)
{
// body of if statement
}
Program to print positive number entered by the user // If the user enters a negative
number, it will be skipped
#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter an integer: ";


cin >> number;

// checks if the number is positive


if (number > 0) {
cout << "You entered a positive integer: " << number
<< endl;
}
cout << "This statement is always executed.";
return 0;
}
C++ if...else The if statement can have an optional else clause. Its syntax is

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;

cout << "Enter an integer: ";


cin >> number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}
C++ if...else...else if statement

• 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>

using namespace std;

int main() {
int num, sum;
sum = 0;

cout << "Enter a positive integer: ";


cin >> num;

for (int count = 1; count <= num; ++count) {


sum += count;
}

cout << "Sum = " << sum << endl;

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;

int main() { // take input again if the number is positive


int number; cout << "Enter a number: ";
int sum = 0; cin >> number;
}
// take input from the user
cout << "Enter a number: "; // display the sum
cin >> number; cout << "\nThe sum is " << sum << endl;

while (number >= 0) { return 0;


// add all positive numbers }
sum += number;
C++ do...while Loop

• 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> // take input from the user


using namespace std; cout << "Enter a number: ";
cin >> number;
int main() { }
int number = 0; while (number >= 0);
int sum = 0;
// display the sum
do { cout << "\nThe sum is " << sum << endl;
sum += number;
return 0;
}
C++ break Statement

• In C++, the break statement terminates the loop when it is encountered.


• The syntax of the break statement is:
break;
Break with loop
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
// break condition
if (i == 3)
{
break;
}
cout << i << endl;
}
return 0;
}
C++ continue Statement
• In computer programming, the continue statement is used to skip the
current iteration of the loop and the control of the program goes to the next
iteration.
• Syntax: continue;
continue with for loop

#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 5; i++) {
// condition to continue
if (i == 3) {
continue;
}

cout << i << endl;


}

return 0;
}
C++ switch..case Statement

• The switch statement allows us to execute a block of code among many


alternatives.
Create a Calculator using the switch
Statement
#include <iostream> num1 + num2;
using namespace std; break;
case '-':
int main() { cout << num1 << " - " << num2 << " = " << num1
char oper; - num2;
float num1, num2; break;
cout << "Enter an operator (+, -, *, /): "; case '*':
cin >> oper; cout << num1 << " * " << num2 << " = " << num1
cout << "Enter two numbers: " << endl; * num2;
cin >> num1 >> num2; break;
case '/':
switch (oper) { cout << num1 << " / " << num2 << " = " << num1
case '+': / num2;
cout << num1 << " + " << num2 << " = " <<
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}

return 0;
}
C++ goto Statement

• In C++ programming, goto statement is used for altering the normal


sequence of program execution by transferring control to some other part
of the program.
// This program calculates the average of numbers entered by the user.
// If the user enters a negative number, it ignores the number and
// calculates the average number entered before it.
if(num < 0.0)
# include <iostream> {
using namespace std; // Control of the program move to
jump:
int main() goto jump;
{ }
float num, average, sum = 0.0; sum += num;
int i, n; }

cout << "Maximum number of inputs: "; jump:


cin >> n; average = sum / (i - 1);
cout << "\nAverage = " << average;
for(i = 1; i <= n; ++i) return 0;
{ }
cout << "Enter n" << i << ": ";
cin >> num;

You might also like