C++ Module 1
C++ Module 1
Dividing a complex problem into smaller chunks makes our program easy
to understand and reusable.
When the function is invoked from any part of the program, it all executes
the codes defined in the body of the function.
C++ Function Declaration
The syntax to declare a function is:
// function declaration
void greet() {
cout << "Hello World";
}
Here,
Note: We will learn about returnType and parameters later in this tutorial.
Calling a Function
In the above program, we have declared a function named greet(). To use
the greet() function, we need to call it.
Here's how we can call the above greet() function.
int main() {
// calling a function
greet();
}
// declaring a function
void greet() {
cout << "Hello there!";
}
int main() {
return 0;
}
Run Code
Output
Hello there!
Function Parameters
As mentioned above, a function can be declared with parameters
(arguments). A parameter is a value that is passed when declaring a
function.
int main() {
int n = 7;
return 0;
}
#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main() {
int num1 = 5;
double num2 = 5.5;
return 0;
}
Run Code
Output
In the above program, we have used a function that has one int parameter
and one double parameter.
We then pass num1 and num2 as arguments. These values are stored by the
function parameters n1 and n2 respectively.
C++
function with parameters
Note: The type of the arguments passed while calling the function must
match with the corresponding parameters defined in the function
declaration.
Return Statement
In the above programs, we have used void in the function declaration. For
example,
void displayNumber() {
// code
}
It's also possible to return a value from a function. For this, we need to
specify the returnType of the function during function declaration.
Then, the return statement can be used to return a value from a function.
For example,
Here, we have the data type int instead of void. This means that the function
returns an int value.
The code return (a + b); returns the sum of the two parameters as the function
value.
The return statement denotes that the function has ended. Any code
after return inside the function is not executed.
// declaring a function
int add(int a, int b) {
return (a + b);
}
int main() {
int sum;
return 0;
}
Run Code
Output
100 + 78 = 178
In the above program, the add() function is used to find the sum of two
numbers.
We pass two int literals 100 and 78 while calling the function.
We store the returned value of the function in the variable sum, and then we
print it.
Working of
C++ Function with return statement
Notice that sum is a variable of int type. This is because the return value
of add() is of int type.
Function Prototype
In C++, the code of function declaration should be before the function call.
However, if we want to define a function after the function call, we need to
use the function prototype. For example,
// function prototype
void add(int, int);
int main() {
// calling the function before declaration.
add(5, 3);
return 0;
}
// function definition
void add(int a, int b) {
cout << (a + b);
}
In the above code, the function prototype is:
This provides the compiler with information about the function name and its
parameters. That's why we can use the code to call a function before the
function has been defined.
#include <iostream>
// function prototype
int add(int, int);
int main() {
int sum;
return 0;
}
// function definition
int add(int a, int b) {
return (a + b);
}
Run Code
Output
100 + 78 = 178
· Functions make the program easier as each small task is divided into a
function.
Some common library functions in C++ are sqrt(), abs(), isdigit(), etc.
In order to use library functions, we usually need to include the header file
in which these library functions are defined.
For instance, in order to use mathematical functions such as sqrt() and abs(),
we need to include the header file cmath.
int main() {
double number, squareRoot;
number = 25.0;
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}
Run Code
Output
Square root of 25 = 5
In this program, the sqrt() library function is used to calculate the square root
of a number.
The function declaration of sqrt() is defined in the cmath header file. That's
why we need to use the code #include <cmath> to use the sqrt() function.
Call by Value, Call by Reference, and Call by
Address in C++
1. Home>
2. C++ Tutorials>
3. Call by Value, Call by Reference, and Call by Address in C++
Summary: In this tutorial, we will learn the difference between call by
value, call by reference, and call by address in the C++ programming
language.
To understand the importance of each type of call and their difference, we
must know, what the actual and formal parameters are:
Actual Parameters are the parameters that appear in the function call
statement.
Formal Parameters are the parameters that appear in the declaration of
the function which has been called.
Call by Value
When a function is called in the call by value, the value of the actual
parameters is copied into formal parameters.
Both the actual and formal parameters have their own copies of values,
therefore any change in one of the types of parameters will not be reflected
by the other.
#include <iostream>
using namespace std;
int main()
{
int x = 5;
increment(x);
cout << "Value in Function main: "<< x <<endl;
return 0;
}
Output:
Note the output of the program. The value of ‘a’ has been increased to 6,
but the value of ‘x’ in the main method remains the same.
This proves that the value is being copied to a different memory location in
the call by value.
Call by Reference
In the call by reference, both formal and actual parameters share the
same value.
Both the actual and formal parameter points to the same address in the
memory.
That means any change on one type of parameter will also be reflected by
other.
#include <iostream>
using namespace std;
int main()
{
int x = 5;
increment(x);
cout << "Value in Function main: "<< x <<endl;
return 0;
}
Output:
This proves that changes made to formal parameters are also reflected by
the actual parameters as they share the same memory address space.
Call by Address
#include <iostream>
using namespace std;
int main()
{
int x = 5;
increment(&x); //Passing address of x
cout << "Value in Function main: "<< x <<endl;
return 0;
}
Output:
The output here is the same as in the case of call by reference i.e. the
value of both ‘a’ and ‘x’ changes.
It is clear that changes made by the formal parameters are also changes
the actual parameter value.
Conclusion
Hope you understood the different type of call mechanism in the C++
programming language. Comment below if you have any doubts.
· CPP
// Return reference
return x;
}
// Driver Code
int main()
{
int a = 20;
int& b = returnValue(a);
Output:
x = 20 The address of x is 0x7fff3025711c
a = 20 The address of a is 0x7fff3025711c
b = 20 The address of b is 0x7fff3025711c
x = 20 The address of x is 0x7fff3025711c
a = 13 The address of a is 0x7fff3025711c
Explanation:
Since reference is nothing but an alias(synonym) of another variable, the
address of a, b and x never changes.
Note: We should never return a local variable as a reference, reason
being, as soon as the functions returns, local variable will be erased,
however, we still will be left with a reference which might be a security
bug in the code.
Below is the code to illustrate the Return by reference:
· C++
// Global variable
int x;
// Driver Code
int main()
{
// Function Call for return
// by reference
retByRef() = 10;
// Print X
cout << x;
return 0;
}
Output:
10
Explanation:
Return type of the above function retByRef() is a reference of the
variable x so value 10 will be assigned into the x.
Inline Functions
To create an inline function, we use the inline keyword. For example,
int main() {
// first function call
displayNum(5);
return 0;
}
Run Code
Output
5
8
666
However, if arguments are passed while calling the function, the default
arguments are ignored.
Working of default arguments
1. When temp() is called, both the default parameters are used by the
function.
2. When temp(6) is called, the first argument becomes 6 while the default
value is used for the second parameter.
3. When temp(6, -2.3) is called, both the default parameters are
overridden, resulting in i = 6 and f = -2.3.
4. When temp(3.4) is passed, the function behaves in an undesired way
because the second argument cannot be passed without passing the
first argument.
int main() {
int count = 5;
return 0;
}
Output
#include <iostream>
using namespace std;
int main() {
int count = 5;
return 0;
}
Run Code
Things to Remember
1. Once we provide a default value for a parameter, all subsequent
parameters must also have default values. For example,
2. // Invalid
3. void add(int a, int b = 3, int c, int d);
4.
5. // Invalid
6. void add(int a, int b = 3, int c, int d = 4);
7.
8. // Valid