VC++ tutorial

Visual C++ Examples

How To Add Menu In VC++

adplus-dvertising
Functions In VC++
Previous Home Next

In C++ we can subdivide the functional features of a program into blocks of code known as functions.In effect these are subprograms that can be used to avoid the repetition of similar code and allow complicated tasks to be broken down into parts, making the program modular. now you have encountered programs where all the code (statements) has been written inside a single function called main().

Every executable C++ program has at least this function.a function is a group of statements that is given a name, and which can be called from some point of the program. function are used to provide a modularity to a program. using function makes it easier to understand creating application.

Format of Function:

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

The description of above code is given below

  1. return_type: specifies the type of data that the function returns.
  2. Function_name: it is the identifier by which it will be possible to call the function.
  3. Parameters list: variables to hold values of arguments passed while function it called. A function may not contain parameter list.

Function Example:

#include <iostream>
using namespace std;
int multiplication (int j, int k)
{
	int r;
		r=j*k;
		return r;
}
int main ()
{
	int i;
	i = multiplication(19,15);
	cout<<"The result is"<<i;
	return 0;
}

calling function: the act of transferring control to a function is known as calling the function. when a function is called, you supply a function name and a list of parameters, if any.

following steps are function called:

  1. The compiler makes a note of the location from which the function was called and makes a copy of the parameter list, if any
  2. Any storage required for the function to execute is temporarily created.
  3. The called function starts executing, using copies of the data that was supplied in the parameter list.
  4. the function has finished executing, control is returned to the calling function, and memory used by the function is released.
ways a calling function:

Call by value: The called function creates a new set of variable in stack and copies the values of arguments into them.

#include <iostream>
using namespace std;

void swap(int x, int y)
  	 { 
	   int z;
  	   z = x;
  	   x = y;
  	   y = z;
  	   printf("Swapped values are a = %d and b = %d", x, y);
  	 }
  	 
  	 int main (int argc, char *argv[])
 	 {
  	   int a = 7, b = 4;
 	   printf("Original values are a = %d and b = %d", a, b);
	   swap(a, b);
  	   printf("The values after swap are a = %d and b = %d", a, b);
  	   return 0;
  	 }

Call by Reference: Its another way of passing arguments to a subroutine. In this method, the address of an argument is copied into the parameter. Inside the subroutine the address is used to access the actual argument used in the call its means that change made to the parameter affect the argumment.

#include <iostream>
using namespace std;
void swap(int *x, int *y)
  	 {
  	   int z;
  	   z = *x;
  	   *x = *y;
  	   *y = z;
  	   printf("Swapped values are a = %d and b = %d", *x, *y);
  	 }
  	 int main (int argc, char *argv[])
  	 {
  	   int a = 7, b = 4;
  	   printf("Original values are a = %d and b = %d", a, b);
  	   swap(&a, &b);
  	   printf("The values after swap are a = %d and b = %d", a, b);
	   return 0;
  	 }
  	 
Previous Home Next