Functions in C++ with examples

A Functions in C++ is known as the block which contains a set of statements, which are executed when it is called in a Main method. In C++ they are defined by a unique name, these are the building blocks of the programming languages. They are used to encapsulate the block of statements, which are executed when it is called in the Main Method. When a Function is called the control of a program is moved to its block, when all the statements are executed the control is returned to the Main Method. They work on the principle of divide and conquer rule, they are useful when the programmer have to use the same code again and again.

Types of Functions in C++ Programming:-

There are of two types.

1. Built-in Functions.

2. User defined Functions.

Built-in or Library Functions:-

Built-in or a Library Functions are available as the part of a computer programming language. These are the ready-made codes available for the programmers, which makes programming much easier in C++. These are the parts of the Header files.

User Defined Functions in C++:-

These are defined by the programmer for his ease during the process of coding, they are consists upon two parts:

1. Functions Declaration.

2. Functions Definition.

Function Declaration or Prototype:-

Function declaration is also called its prototype. It consists upon three parts.

1. Functions return Type.

2. Functions Name.

3. Functions Parameters or Arguments.

During the process of Function declaration, first of all its return type is written then its unique name and then its arguments.

Return Type:-

It indicates the type of value returned by the corresponding Function.

Function Name:-

It indicates towards the unique name chosen for it.

Function Parameters:-

It indicates towards the Arguments which will be passed to it through the Main Method.

Syntax of a Function Declaration:-

(Return type) (Name) (Parameters)

For Example:-

Int random (float);

Above example indicates that the random Function will accept one float value as a argument and it will return the integer value.

User defined Functions are further divided into four types.

1. Functions which take arguments and return no value.

2. Functions which does not take any arguments but return a value.

3. The function which takes parameter and also returns the value.

4. The function which does not take any argument and does not return any value.

Functions Definition:-

Functions definitions are the collection of statements which tells about its working and purpose. It could be defined in three ways.

1. Before the Main() Method.

2. After the Main() Method.

3. In a Separate header files.

Function declaration is not necessary if it is defined before the Main Method.

Function declaration is essential if it is defined after the Main Function.

If Function definition is stored in a header file then it could be used anytime by including the particular header file in a program.

Syntax of a Function Definition:-

(Return type) (Name) (Arguments)

{

Collection of Statements about functionality

};

Scope of a Function in C++:-

Its scope depends upon the place where it is declared, in the terms of scope they are divided into two types:

1. Local Functions.

2. Global Functions.

Local Functions:-

They are declared into another Function. They could only be accessed inside the Function in which they are declared. In another words Functions which are declared inside the Main Method are called local Methods.

Global Functions:-

These are declared outside the Main Method, they could be accessed anywhere in the program.

Passing Parameters to a Function:-

Parameters or arguments are the values which are provided to the Function when it is called. They are given in the brackets, if there is more than one argument than these parameters are separated by the coma. If there is no argument then the Function is called without providing any values in the parentheses.

Arguments which are given during function call are called the Actual arguments.

Arguments which are used during Function declaration are called Formal parameters.

There are two ways to pass the Arguments to the Functions.

1. Pass by Value.

2. Pass by Reference.

Parameters Pass by Value:-

Parameters pass by value is a process in which values of Actual arguments are copied to Formal parameters. In this process if the value of formal Arguments is changed then it does not affect the values of actual Arguments. This is the most commonly used process of passing the parameters.

Example of Parameters pass by value:-

Following is the program of prime numbers which is done by the help of Methods to which Arguments are passed by value.


#include<iostream.h>
#include<conio.h>

void fprime(int);

main()
{
int i, j;
cout<<"Enter the number: ";
cin>>j;
fprime(j);

getch();
}

void fprime(int a)
{
int ans=0;
for(int i=2; i<a; i++)
{
if (a%i==0)
{
ans=1;
}
}
if(ans==1)
{
cout<<a<<" is not a Prime Number "<<endl;
}
else
cout<<a<<" is a Prime Number "<<endl;
}

Explanation:-

C++ functions exampleThe above program will declare the Function fprime. Then it will take the input from the user and assign it to the variable j. Then fprime is called and variable j is passed as argument. Therefore copy of the Actual parameter j is created into the Former argument a, then loop will come into action and check the condition, if entered number modulus I is equal to zero then the entered number is not a prime number, else entered number is a prime number.

Parameters Pass by Reference:-

Parameters pass by reference is a process in which actual Arguments and formal Arguments are points towards the same memory location. In this process if the value of formal arguments is changed then the value of actual parameters will also be changed.

Example of Parameters pass by reference:-

Now we will discuss the below program of prime numbers which is done by the help of Function to which Arguments are passed by reference.


#include<iostream.h>
#include<conio.h>

void fprime(int &q);

main()
{
int i, j;
cout<<"Enter the number: ";
cin>>j;
fprime(j);

getch();
}

void fprime(int &q)
{
int ans=0;
for(int i=2; i<q; i++)
{
if (q%i==0)
{
ans=1;
}
}
if(ans==1)
{
cout<<q<<" is not a Prime Number "<<endl;
}
else
cout<<q<<" is a Prime Number "<<endl;
}

Explanation:-

Functions in C++ exampleThe above program will declare the Function fprime. Then it will take the input from the user and assign it to the variable j. Then fprime is called and variable j is passed as Argument. As parameters are passed by reference, therefore the actual argument j and the former argument q will point towards the same memory location, then loop will come into action and check the condition, if modulus of entered number with I is equal to zero then the entered number is not a prime number, else entered number is a prime number.

[yop_poll id=”8″] If you like my article on functions in C++ and wanted to read my other articles then visit my Homepage.

@ 2014 HellGeeks.com

5/5 - (1 vote)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a Reply

HellGeeks
Logo