0% found this document useful (0 votes)
0 views

function in c++

The document explains the C++ break, continue, and goto statements, detailing their usage in loops and control flow. It also covers the concept of functions in C++, including their creation, calling, advantages, and types, as well as variable scope and parameter passing techniques. Additionally, it illustrates call by value and call by reference with examples to show how data can be passed to functions.

Uploaded by

sridhar2879
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

function in c++

The document explains the C++ break, continue, and goto statements, detailing their usage in loops and control flow. It also covers the concept of functions in C++, including their creation, calling, advantages, and types, as well as variable scope and parameter passing techniques. Additionally, it illustrates call by value and call by reference with examples to show how data can be passed to functions.

Uploaded by

sridhar2879
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

C++ Break Statement

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:

C++ Break Statement Example

Let's see a simple example of C++ break statement which is used inside the loop.

1. #include <iostream>

2. using namespace std;

3. int main() {

4. for (int i = 1; i <= 10; i++)

5. {

6. if (i == 5)

7. {

8. break;

9. }
10. cout<<i<<"\n";

11. }

12. }

Output:

C++ Break Statement with Inner Loop

The C++ break statement breaks inner loop only if you use break statement inside the inner loop.

Let's see the example code:

1. #include <iostream>

2. using namespace std;

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. }

10. cout<<i<<" "<<j<<"\n";

11. }

12. }

13. }

Output:

11
12

13

21

31

32

33

C++ Continue Statement

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;

C++ Continue Statement Example

1. #include <iostream>

2. using namespace std;

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 with Inner Loop

C++ Continue Statement continues inner loop only if you use continue statement inside the inner loop.

1. #include <iostream>

2. using namespace std;

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. }

10. cout<<i<<" "<<j<<"\n";

11. }

12. }

13. }

Output:

22.5M

522

Features of Java - Javatpoint


11

12

13

21

23

31

32

33

C++ Goto Statement

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.

C++ Goto Statement Example

Let's see the simple example of goto statement in C++.

1. #include <iostream>

2. using namespace std;

3. int main()

4. {

5. ineligible:

6. cout<<"You are not eligible to vote!\n";

7. cout<<"Enter your age:\n";

8. int age;
9. cin>>age;

10. if (age < 18){

11. goto ineligible;

12. }

13. else

14. {

15. cout<<"You are eligible to vote!";

16. }

17. }

Output:

You are not eligible to vote!

Enter your age:

16

You are not eligible to vote!

Enter your age:

You are not eligible to vote!

Enter your age:

22

You are eligible to vote!

#include <iostream>

using namespace std;

int main(){

int num; cout<<"Enter a number: "; cin>>num;

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.

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

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

 myFunction() is the name of the function


 void means that the function does not have a return value. You will learn more about return
values later in the next chapter

 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

Inside main, call myFunction():

// Create a function
void myFunction() {
cout << "I just got executed!";
}

int main() {
myFunction(); // call the function
return 0;
}

// Outputs "I just got executed!"

Advantage of functions in C

There are many advantages of functions.

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

It makes the code optimized, we don't need to write much code.

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

There are two types of functions in C programming:

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

The syntax of creating function in C++ language is given below:

1. return_type function_name(data_type parameter...)

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

Variables defined within a function or block are said to be local to those


functions.
 Anything between ‘{‘ and ‘}’ is said to inside a block.

 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.

 Declaring local variables: Local variables are declared inside a block.

#include<iostream>

using namespace std;

void func()

{
// this variable is local to the

// function func() and cannot be

// accessed outside this function

int age=18;

cout<<age;

int main()

cout<<"Age is: "<<age;

return 0;

Global Variables

 They are available through out the life time of a program.


 They are declared at the top of the program outside all of the functions or
blocks.
 Declaring global variables: Global variables are usually declared outside of all
of the functions and blocks, at the top of the program. They can be accessed
from any portion of the program

#include<iostream>

using namespace std;

// global variable

int global = 5;
// global variable accessed from

// within a function

void display()

cout<<global<<endl;

// main function

int main()

{ display();

// changing value of global

// variable from main function

global = 10;

display();

 Output:
 5
 10

Parameter Passing Techniques

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.

Call by value in C++

In call by value, original value is not modified.

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>

2. using namespace std;

3. void change(int data);

4. int main()

5. {

6. int data = 3;

7. change(data);

8. cout << "Value of the data is: " << data<< endl;

9. return 0;

10. }

11. void change(int data)

12. {

13. data = 5;

14. }

Call by reference in C++


In call by reference, original value is modified because we pass reference
(address).
Here, address of the value is passed in the function, so actual and formal
arguments share the same address space. Hence, value changed inside the
function, is reflected inside as well as outside the function.

1. #include<iostream>

2. using namespace std;

3. void swap(int *x, int *y)

4. {

5. int swap;

6. swap=*x;

7. *x=*y;

8. *y=swap;

9. }

10. int main()

11. {

12. int x=500, y=100;

13. swap(&x, &y); // passing value to function

14. cout<<"Value of x is: "<<x<<endl;

15. cout<<"Value of y is: "<<y<<endl;

16. return 0;

17. }

You might also like