by Dinesh Thakur Category: Functions

A function contains a set of statements which can be invoked from any part of the program. Function plays an important role whenever a sequence of instructions is to be repeated in a program. Thus it helps in reducing the size of the program. The function code is stored at only one place in the memory, though the function can be executed any number of times. Main () is also a function. Every program must contain the main () function. A function mayor may not return values to the calling function.

void Functions

Functions which do not return values are called void functions. The general syntax of void function is

 

void f1(list of arguments)

{

statement(s);

return;

}

 

Where f1 denotes the function name. The lists of arguments denote the number and type of values which are passed on to the function. The return statement indicates the end of the function and it is optional. The set of statements are enclosed within curly brackets. Void functions mayor may not have arguments.

void Functions with no arguments

An example of a void function which does not have any arguments is as follows:

 

void display()

{

cout<<"Functions with no arguments ";

}

 

A function prototype informs the compiler that the function would be referenced at a later point in a program. It is necessary to give the prototype of a function if the function definition is given after the call to the function. Function prototype is not required if the function definition is given prior to the function call.

 

void main()

{

void display();

display();

}

//prototype

//call to display function

void display()

{

cout<<"use prototype";

}

Usually the prototype is defined in the main() function.

 

Example

 

The program illustrates a function which does not have any arguments and does not return any value. Function fib() is used for generating the first n elements in the Fibonacci series.

 

#include <iostream.h>

void main( )

{

void fib();

fib();

}

void fib()

{

int i=1;

int n;

cout<<"enter total no. of terms in the series ";

cin>>n;

float f0=1, f1=1, f2;

cout<<"Fibonacci Series\n";

cout<<f0<<" "<<n <<"\n";

while(i<n)

{

f2=f0+n;

i=i+ 1;

cout<<f2<<"\n";

f0=f1;

f1=f2;

}

}

 

In this program, the prototype of the function fib() is defined in the main() function as follows :

 

void fib();

 

Later it is invoked in the main() function using Fib();

void Functions with arguments

An example of a function with arguments is :

 

void show(int a, int b)

{

int sum;

sum = a + b;

cout<<"Sum "<<sum;

}

 

This function show() does not return any value to the calling program but the results are computed and displayed on the execution of the function. The function prototype for the function show() given above with arguments is given in the main() function as shown below:

 

void main()

{

void show(int a, int b);

show(m,n);

}

Here,

                                           void show(int a, int b);

is a prototype of the function show(). Instead of this statement, the following form of prototype can also be given.

                                             void show(int, int );

The function show() is invoked in the main() function using :

                                             show(m,n);

Example

The program illustrates a function fib() which takes an argument of int type. This function does not return any value. This program also finds the first n elements in a fibonacci series.

#include <iostream.h>

void main()

{

int n;

void fib(int);

cout<<"enter total no. of terms in the series ";

cin>>n;

cout<<"Fibonacci Series"<<"\n";

fib(n);

}

void fib(int n)

{

int i=l;

float f0=l, f1=1, f2;

cout<<f0<<"\n "<<f1 <<"\n";

while(i<n)

{

f2=f0+f1;

i=i+l;

cout<<f2<<"\n";

f0=f1;

f1=f2;

}

}



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles