Home » Learn C++ » Passing Arguments to Function

Passing Arguments to Function

Passing Arguments to Functions:

            If a function needs data to perform a specific task, this data is provided through the arguments of the function. The arguments are placed in parentheses. The arguments are either constants or variables. They are written in the same sequence in which they are defined in the function declaration. The data type of an argument in the function call must also match the corresponding data types in the function declaration.

            For example, in the following program, function “sum” adds two integer numbers. The numbers whose sum is to be calculated are passed to the function as arguments.
Program:

 

#include <iostream.h>
#include <conio.h>
main ( )
{
void sum (int, int);
clrscr ( );
sum (10, 15);
cout << “Ok”;
}
void sum (int x, int y)
{
int s;
s =  x + y;
cout << “Sum = << s << endl;
}

Other Topics

comment closed

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Copyright © 2010 Information Village. All rights reserved.