Fundamental Of Functions:-

There are number of fundamentals associated with the functions. To understand these fundamentals let us consider the following example.

# include

void main ( )

{

int x, y, z;

int add (int, int);       // function prototype

cout << “Enter two integers=”;

cin >> x >>y;

z = add (x, y);

cout << “Answer=” << z << ‘\n’;

}

return ptg;

}

1.Function Declaration or function prototype:-

consider the following statement

int add (int, int);

Using he above line we declare to the compiler that add ( ) is a function that accepts two integers and returns one integer.

2. Function call:-

z = add (x, y);

Using this statement we are calling the function while call in the function the values of x and y are passed and are copied into p and g resply. The function will add these vales and will return the answer of addition. The returned answer will be assigned to z and will then be printed using cout.

3.Function header, body and definition:-

Consider the following example.

int add (int p, int q )

{

return p + q ;

}

The first line in the above example is called as Function Header. The        general syntax of the function header is given as,

return-type function name (optional list of parameters)

The code of the function enclosed with the pair of curly brackets is called as function body. The function header and the Function Definition.

4. Actual Parameters:-

The parameters which are used in the function call are called as actual parameters. For e.g. x & y are actual parameters.

5. Formal parameters:-

The parameters which are used in the function header are called as formal parameters. For e.g. p and q are formal parameters.

Note: Sometimes another pair of terms is used and that is Arguments      and parameters instead o using the terms Actual parameters and formal parameters respectively.

 

CONCLUSION:-

1        When the function is called, the values of actual parameters are copied into the corresponding formal parameters.

2        The number of actual parameters and formal parameters must be same.

3        The actual parameters and the corresponding formal parameters must     be same type.

4        In the above points we can use the terms Arguments and parameters       instead of Actual parameters and formal parameters, resply.

 

 

1.Write a function that accepts three integers and returns the highest out     of them. Write a pgm that will accept 9 integers from user and will print the highest out of them using the given function.

→      # include

# include

void main ( )

{

int a, b, c, d, e, f, g, h, i, m1, m2, m3, m ;

int max(int, int, int);

clrscr ( );

cout << “Enter 9 integer =”;

cin >> a >> b >> c >> d >> e >> f>> g >> h >> i;

m1 = max (a, b, c); // find out highest out of 1st three int

m2 = max (d, e, f); // find out highest out of next three int

m3 = max (m1, m2, m3); // find out ultimate highest

cout << “Highest =” << m << ‘\n’;

getch ( );

}

int max (int x, int y, int z)

{

if ( x > y && x > 2)

return x;

else

if (y > x & y >z )

return y;

else

return z;

}

2) Write a function that accepts one integer and calculates and returns its factorial. Write a suitable main function to accept the values of n and r from user and will calculate and print the value of ncr using above function.

 # include

# include

void main( )

{

int n.r;

unsigned long int nf. rf, nrf;

unsigned long int fact (int) // Function declaration

clrscr( );

cout << “Enter two negative integer =”;

cin >>n >>r;

nf = Fact (n) // calculate n;

rf = Fact (r) // calculate r;

nrf = Fact (n-r) // calculate (n-r);

cout << “Answer = “<< (nF / rf) * nrf << ‘in’;

getch ( ) ;

}

unsigned long int Fact (int x)

{

int i ;

unsigned long int F = 1 ;

for (i = 1; i < = x; i + +)

F = f *i   ;

return f ;

}

3.                Write a Function that accepts three digit integer numbers. The function should return the answer indicating whether the number is Armstrong number or not. Write a suitable main function that will print the entire three digits Armstrong numbers using the above function.

→      # include

# include

void main ( )

{

 

int i, ans;

int armstrong (int); // Function declaration clrscr ( );

cout << “following are three digit Armstrong numbersin”;

for (I = 100; I < = 999; i++)

{

ans = armstrong (i) // Function call

if (ans = = 1)

cout << i ;

}

getch ( ) ;

}

int armstrong (int n)

{

int x, y, z, w ;

x = n/100 ;

w = n % 100 ;

y = w/10 ;

z = w % 10 ;

if (x * x* x + y * y * y + z * z* z = = n)

return 1 ;

else

return 0 ;

}

 


Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet