Loading....
Coupon Accepted Successfully!

 

Accessing a Function

  • A defined function can be called from other functions by specifying its name followed by list of arguments enclosed within parentheses.

  • The general form is

Function name (list of argument);

Where
Function name - name of a already defined function
List of arguments - actual arguments.
For example

#include<iostream.h>

main()

{

clrscr();

float cube(float);

float x,y;

cout <<"enter the number for form cube";

cin >>x;

y=cube(x);

cout<<"the value of "<<x<<"is"<<y<<"\n";

getch();

}

float cube(float i)

{

float n;

n=i * i * i;

return(n);

}

  • Accessing function can be done using
    • Formal arguments
    • Actual arguments
    • Default arguments
    • Constant arguments

Formal Arguments

  • This arguments present in the function definition called formal argument.
  • These are also called as dummy arguments because it receives values only from the calling function.
  • Fox example consider the function definition
 

int abc(int I, int j)

{

int k;

k= i + j;

return(k);

}

  • In this function i and j are called formal or dummy arguments because the values for the arguments i and j are not available.
  • So we cannot execute this function.

Actual Arguments

  • A defined function is called by specifying the name followed by a list of arguments or parameters.
  • These arguments are called actual arguments, because through these arguments only the arguments in the function definition receive values.
  • For example consider the program.

void main()

{

int a.b.z;

a=10;

b=5;

z=abc(a,b);

cout<<z;

}

  • Here a and b are called actual arguments because through this only the called function abc receives the values of the formal arguments i and j as 10 and 5.
  • Now the function is executed and the result 15 is returned to the calling function and it is printed.

Default Arguments

  • We can call a function with out specifying all its actual arguments.
  • This missed argument values must be specified in the function declaration.
  • The compiler takes the missed values from the function declaration.
  • This type of calling is called default arguments.

#include<iostream.h>

void main()

{

float volume(float r=2, float h = 3.5);

cout<<"volume 2=\n"<<volume (3);

cout<<"volume 3 = \n"<<volume (3,2);

}

float volume(float r2, float h2)

{

return (3.14 * r2*r2*h2);

}

Rules for default arguments
  • The default arguments are assigned from right to left manner.
  • It is not possible to provide default value to the middle of arguments list.

 

Constant Arguments

  • User can define arguments to the functions as a constant.

  • This is done during function declaration.

  • Once declared as constant that argument cannot be modified.

  • If modified the compiler given an error.

  • The general form is

datatype function_name(constdatatype arg1, const datatype arg2,....);

  • Example

int xyz(const int a);





Test Your Skills Now!
Take a Quiz now
Reviewer Name