C++ function


A function is a subprogram that acts on data and often returns a value.

A program written with numerous functions is easier to maintain, update and debug than one very long program. By programming in a modular (functional) fashion, several programmers can work independently on separate functions which can be assembled at a later date to create the entire project. Each function has its own name.

When that name is encountered in a program, the execution of the program branches to the body of that function.

When the function is finished, execution returns to the area of the program code from which it was called, and the program continues on to the next line of code.


Defining a Function

Syntax

return_type function_name( parameter list )
{
   body of the function
}

A C++ function definition consists of a function header and a function body. Here are all the parts of a function:

  • Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
  • Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.
  • Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
  • Function Body: The function body contains a collection of statements that define what the function does.

Example

//Prototyping, defining and calling a function
#include <iostream>
using namespace std;
void jk201(); // prototype the function
int main()
{
jk201 ( ); // function call
return 0;
}
// function definition
void jk201()
{
cout<<  "Welcome to C++ Tutorial";
cout<<endl;
}

Argument To A Function

Sometimes the calling function supplies some values to the called function. These are known as parameters.

The variables which supply the values to a calling function called actual parameters.

The variable which receive the value from called statement are termed formal parameters.

Example

#include<iostream.h>
using namespace std;
void area(float);
int main()
{
float radius;
cin>>radius;
area(radius);
return 0;
}
void area(float r)
{
cout<< "the area of the circle is" << 3.14*r*r;
}

Return Type Of A Function

Example

#include<iostream.h>
using namespace std;
int times(int num); // function prototype
int main()
{
int number, response;
cout<<"Please enter a number:"; 
cin>>number;
response = times(number); //function call
cout<< "The answer is "<<response;
return 0;
}
//time function
int times (int num)
{
int answer; //local variable
answer = 2 * num;
return (answer);
}

Calling Of A Function

The function can be called using either of the following methods:

  • call by value
  • call by reference

Call By Value

In call by value method, the called function creates its own copies of original values sent to it. Any changes, that are made, occur on the function's copy of values and are not reflected back to the calling function.

Example

#include<iostream>
using namespace std;
void swap(int , int );
int main()
{
int a = 10;
int b = 20;
swap(a,b);
cout<<a<<" "<< b;
return 0;
}
void swap(int c, int d)
{
int s;
s = c;
c = d;
d = s;
}
Output
a=20 b=10

Call By Reference

In call be reference method, the called function accesses and works with the original values using their references. Any changes, that occur, take place on the original values are reflected back to the calling code.

Example

#include<iostream.h>
using namespace std;
void swap(int &, int &);
int main()
{
int a=10,b=20;
swap(a,b);
cout<< a  <<" "<< b;
return 0;
}
void swap(int &c, int &d)
{
int t
t=c;
c=d;
d=t;
}
Output
a=20 b=10

Share this article on