Functions in C++

A function is a block of statements which is used to perform a task. Any C++ program has at least one function which is main().  When you have a piece of code that is often repeated you should put it into a function and call that function instead of repeating the code.

Function Definition

You define a function in similar way as you define the main function. First put return type then the function name and function parameters in brackets. The function code goes between curly brackets.

returnType functionName( parameters ) {

statement(s);

}

Example

void PrintHello()
{

cout << “Hello\n”;

}

Calling a Function

Once you have created the function you can call it from the main program. Here is an example of how to call the PrintHello function.

void PrintHello()
{

cout << “Hello\n”;

}

void main()
{

PrintHello();

}

Local and Global Variables

If you declare a variable inside a function it is only accessible by that function and is called a local variable. If you declare a variable outside of all functions it is accessible by any function and is called a global variable.

Example

int g; // Global variable

void MyFunction()
{

int l; // Local variable
l = 5;
g = 7;

}

void main()
{

g = 3;

}

Function Arguments

The arguments are used to pass values to a function. Arguments goes between the brackets after the function name. You must choose the datatype of all parameters. Here is a function that receives a number as a parameter and then prints it.

void PrintNumber(int n, int m)
{

cout << n << m;

}

void main()
{

PrintNumber(5, 6);

}

Passing Parameters by Reference

You can either pass a parameter by reference or by value. The default is by value which means that a copy of the variable is made for that function. If you use a * in front of the parameter then you will be passing only a pointer to that variable instead of making another copy of it.

void PrintNumber(int *n)
{

cout << *n;

}

void main()
{

int i = 5;
PrintNumber(&i);

}

Returning Values

A function can return a value that you can store in a variable. We have been using void in the place of the return variable until now. Here is an example of how to return the number 5 from a function and store it in a variable.

int GetNumber()
{

return 5;

}

void main()
{

int i = GetNumber();

}

Functions in C

Function is a block of statements which perform some specific task and always return single value to the calling function. Functions in c are used to minimize the repetition of code.

Some languages distinguish between functions which return variables and those which don’t. C assumes that every function will return a value. If the programmer wants a return value, this is achieved using the return statement. If no return value is required, none should be used when calling the function.

There are two types of functions in c language.

Library Functions

A function which is predefined in c language is called library function. printf(), scanf(), getch() etc are examples of built-in or library functions.

User Defined Functions

A function written by a programmer is called user defined function.

User Defined Function Example

int add (int x, int y) {

int z;
z = x + y;
return (z);

}

main ()
{

int i, j, k;
i = 15;
j = 5;

k = add(i, j);
printf (“The value of k is %d\n”, k);

}

Output

The value of k is 30

Scope of Function

Only a limited amount of information is available within the body of each function. Variables declared within the calling function can’t be accessed from the outside functions unless they are passed to the called function as arguments.

Global Variables

A variable that is declared out side all functions is called Global variable. Global variables don’t die on return from a function. Their value is retained, and is available to any other function in whole program.

Local Variables

A variable that is declared within a function is called Local variable. They are created each time the function is called, and destroyed on return from the function. The values passed to the functions (arguments) are also treated like local variables.

Static Variables

Static variables are like local variables but they don’t die on return from the function. Instead their last value is retained, and it becomes available when the function is called again.