function in c++
function in c++
The C++ break is used to break loop or switch statement. It breaks the current flow of the program at
the given condition. In case of inner loop, it breaks only inner loop.
1. jump-statement;
2. break;
Flowchart:
Let's see a simple example of C++ break statement which is used inside the loop.
1. #include <iostream>
3. int main() {
5. {
6. if (i == 5)
7. {
8. break;
9. }
10. cout<<i<<"\n";
11. }
12. }
Output:
The C++ break statement breaks inner loop only if you use break statement inside the inner loop.
1. #include <iostream>
3. int main()
4. {
5. for(int i=1;i<=3;i++){
6. for(int j=1;j<=3;j++){
7. if(i==2&&j==2){
8. break;
9. }
11. }
12. }
13. }
Output:
11
12
13
21
31
32
33
The C++ continue statement is used to continue loop. It continues the current flow of the program and
skips the remaining code at specified condition. In case of inner loop, it continues only inner loop.
1. jump-statement;
2. continue;
1. #include <iostream>
3. int main()
4. {
5. for(int i=1;i<=10;i++){
6. if(i==5){
7. continue;
8. }
9. cout<<i<<"\n";
10. }
11. }
Output:
2
3
10
C++ Continue Statement continues inner loop only if you use continue statement inside the inner loop.
1. #include <iostream>
3. int main()
4. {
5. for(int i=1;i<=3;i++){
6. for(int j=1;j<=3;j++){
7. if(i==2&&j==2){
8. continue;
9. }
11. }
12. }
13. }
Output:
22.5M
522
12
13
21
23
31
32
33
The C++ goto statement is also known as jump statement. It is used to transfer control to the other part
of the program. It unconditionally jumps to the specified label.
It can be used to transfer control from deeply nested loop or switch case label.
1. #include <iostream>
3. int main()
4. {
5. ineligible:
8. int age;
9. cin>>age;
12. }
13. else
14. {
16. }
17. }
Output:
16
22
#include <iostream>
int main(){
if (num % 2==0){
goto print;
}
else {
cout<<"Odd Number";
print:
cout<<"Even Number";
return 0;
C++ Functions
The function in C++ language is also known as procedure or subroutine in other programming languages.
To perform any task, we can create function. A function can be called many times. It provides modularity
and code reusability.
Functions are used to perform certain actions, and they are important for reusing code: Define the code
once, and use it many times.
Create a Function
C++ provides some pre-defined functions, such as main(), which is used to execute code. But you can
also create your own functions to perform certain actions.
To create (often referred to as declare) a function, specify the name of the function, followed by
parentheses ():
Syntax
void myFunction() {
// code to be executed
}
Example Explained
inside the function (the body), add code that defines what the function should do
Call a Function
Declared functions are not executed immediately. They are "saved for later use", and will be executed
later, when they are called.
To call a function, write the function's name followed by two parentheses () and a semicolon ;
In the following example, myFunction() is used to print a text (the action), when it is called:
Example
// Create a function
void myFunction() {
cout << "I just got executed!";
}
int main() {
myFunction(); // call the function
return 0;
}
Advantage of functions in C
1) Code Reusability
By creating functions in C++, you can call it many times. So we don't need to write the same code again
and again.
2) Code optimization
Suppose, you have to check 3 numbers (531, 883 and 781) whether it is prime number or not. Without
using function, you need to write the prime number logic 3 times. So, there is repetition of code.
But if you use functions, you need to write the logic only once and you can reuse it several times.
Types of Functions
1. Library Functions: are the functions which are declared in the C++ header files such as ceil(x), cos(x),
exp(x), etc.
2. User-defined functions: are the functions which are created by the C++ programmer, so that he/she
can use it many times. It reduces complexity of a big program and optimizes the code.
Declaration of a function
2. {
3. //code to be executed
4. }
Scope of variables
The scope of a variable is defined as the extent of the program code within which the variable can be
accessed or declared or worked with. There are mainly two types of variable scopes:
1. Local Variables
2. Global Variables
Local Variables
Local variables do not exist outside the block in which they are declared, i.e. they can not be
accessed or used outside that block.
#include<iostream>
void func()
{
// this variable is local to the
int age=18;
cout<<age;
int main()
return 0;
Global Variables
#include<iostream>
// global variable
int global = 5;
// global variable accessed from
// within a function
void display()
cout<<global<<endl;
// main function
int main()
{ display();
global = 10;
display();
Output:
5
10
There are different ways in which parameter data can be passed into and out of
methods and functions. Let us assume that a function B() is called from another
function A(). In this case A is called the “caller function” and B is called the “called
function or callee function”. Also, the arguments which A sends to B are called actual
arguments and the parameters of B are called formal arguments.
Call by value and call by reference in C++
There are two ways to pass value or data to function in C language: call by value and call by
reference. Original value is not modified in call by value but it is modified in call by reference.
In call by value, value being passed to the function is locally stored by the function parameter in stack
memory location. If you change the value of function parameter, it is changed for the current function
only. It will not change the value of variable inside the caller method such as main().
1. #include <iostream>
4. int main()
5. {
6. int data = 3;
7. change(data);
8. cout << "Value of the data is: " << data<< endl;
9. return 0;
10. }
12. {
13. data = 5;
14. }
1. #include<iostream>
4. {
5. int swap;
6. swap=*x;
7. *x=*y;
8. *y=swap;
9. }
11. {
16. return 0;
17. }