Sei sulla pagina 1di 11

USER-DEFINED FUNCTIONS (UDF)

FUNCTION- is a subprogram or module (a section of a program) that


performs a specific task. It is defined outside the main function and
declared (or prototype ) above the main function. When a function is called,
the statements inside the function body are executed, then, it returns back
to the point in the calling program where it is called, and if it has a return
value, that value is stored in the variable for which the function call is
assigned by the assignment ( = ) operator.
CREATING USER-DEFINED FUNCTION
1.) FUNCTION DECLARATION
Function prototype- ( or declaration of the function) specifies the type of
return value of the function, the name of the function and its formal
parameters (arguments). It is the same as the Function Header terminated
by a semi-colon.
DECLARING (OR PROTOTYPING A FUNCTION) :

SYNTAX : type Function_Name( formal parameters );

where: a.) type is the data type of the function which is the same as the
data type of the value returned by the function. A function that is declared
to be of type void has no return value. It simply prints its output to screen
or execute whatever statements inside the functions body.
b.) Function_Name is the name you give to the function which can be
any valid identifier
c.) FORMAL PARAMATERS are the functions arguments
separated by comma, they are the variables that will receive the
copy of the values passed to the function by the calling program
so the function can process them.

A function can be declared only outside the main function.

2.)FUNCTION DEFINITION consists of Function Header and Function
body

SYNTAX : type Function_Name( formal parameters ) Function Header
{
local variables declarations;
statements ; FUNCTION BODY
statements;

return(value);
}




NOTE :
1.) Function Prototype is not needed if the function is defined above the
main function.

2.) In a function prototype, the variables name is optional, you can just
specify the formal parameters data type. In a Function Header, the
variables name and its data type must be both provided.
For example, the function declaration : void try( int x, float y );
can also be written as : void try( int, float );

3.) The variables declared inside the User-Defined Function(UDF) is known
only inside that function, they are not known inside the main() function.
Similarly, all variables declared inside the main() function are known only
inside the main function, they are unknown inside the UDF. The function
passes its computed value to the main function ( so that the main function
will know its computed/processed value) through its return statement or
through its reference parameter/s .

4.)The identifier names of the actual parameters need not be similar to the
corresponding identifier names of the formal parameters. They can be
made similar, but if changes are made to the values of the formal
parameters inside the function body, those changes will not affect the
original values of the actual parameters inside the main function.

SYNTAX FOR CALLING A FUNCTION OF TYPE VOID (no return value):

Function_Name( actual parameters );

Where : ACTUAL PARAMETERS are the actual values passed to the
function by the calling program or routine. NOTE that the data type of the
return value as well as the data types of the parameters are
not specified during the function call.

SYNTAX FOR CALLING A FUNCTION THAT HAS A RETURN VALUE:

variable_name = Function_Name(actual parameters);

where : variable _name is the name of the variable in the
calling program that will receive or store the return value processed by the
function. ( like y = pow(2,3 );

NOTE : The return value of the function can also be printed directly as
follows :
cout<< FunctionName(actual parameters); // like cout<<pow(2,3);
The return value can also be used directly in equation as follows :
X = y * FunctionName( actual parameters) 56; //like x = y *pow(3,5) 56;

REQUIREMENTS IN CALLING A FUNCTION :

1.) The number of actual parameters should exactly match the number
of formal parameters.
For ex., if a function is declared as void show (int x , int y, int z );
It is an error to call this function if you provide only 1 or 2 actual
parameters like :
show ( 5 , 3); // Error, there are only 2 actual parameters
show(5); // error. There is only 1 actual parameter
show(5,3,8 ); // correct, there are exactly 3 actual parameters

2.) The data types of each actual parameters should match the data
types of each corresponding formal parameters. For example : If a
function is declared as void show (int x, float y , char c );
It is an error to call this function if the data types of the actual
parameters are different from the data types of the formal parameters,
for example :
show(5.7 , 3, 2); // error, the 1
st
parameter is of type float, it
// should be of type int, the 2
nd
parameter is of
// type int it should be of type float.
// also the 3
rd
parameter is of type int it should
// be of type char
show( 4, 7, 8 ); // error, the data types of the 2
nd
and 3
rd

// parameters did not match the data types of the
// corresponding formal parameters.
show ( 3, 3.14, w); // correct, the data types of the 3 actual
// parameters matched the corresponding
// data types of the formal parameters

3.) The data type of the variable that will receive or store the return
value of the function must be similar to the data type of the function.
For Example , In Sample Program1 shown below, it is an error to
assign the return value of the function ave to variable x because
variable x is of type integer.


SAMPLE PROGRAM 1
#include <iostream>
using namespace std;
float ave ( int x, int y, int z ); // FUNCTION PROTOTYPE

int main ( )
{ int a = 1, b= 2 , c = 3, X ;
float y;

// X = ave( a, b, c) ; Error the variable X is of type int, the
// return value of function ave is of type float,
// the return value should be stored to y

y = ave(a,b,c); // FUNCTIONCALL; a is assigned to x, b is assigned to y,
// and c is assigned to z ,the result of computation
// inside the functions body is assigned or stored to
// variable Y, also of type float
cout<<y = << y<<endl; // y = 2.0
system(pause);
return 0;
}
// FUNCTION DEFINITION of function ave

float ave ( int x, int y, int z )
{ float A;
A= ( x + y + z)/3.0 ;
return A;
}

Note : Variables a, b and c are called the actual parameters, copy of their
respective values are passed to the function ave during the function call.
Variables x, y, and z are called the formal parameters, they receive the values
passed to the function, so the function can make use of these values.

Execute the statements
inside the functions body,
then, return to the point
in the main function
where it is called, and
store its return value to
variable ,Y.





SAMPLE PROGRAM 2
#include<iostream>
using namespace std:
void greetings(void);
int sum(int x,int y); FUNCTION PROTOTYPES
int main()
{ int a, b,z;
greetings(); // prints Welcome !
cout <<Enter the value for x:; cin>> a;
cout <<Enter the value for y:; cin>> b;
z= sum(a, b); // a and b are the actual parameters that will be
//passed to the functions formal
// parameters x and y, that is, x will receive the
// value of a, and y will receive the
// value of b, z will receive or store the return
// value computed by the function
cout<<z = << z<<endl;
a = sum(z, a);
cout<<a= << a<<endl;
z= sum(a,b);
cout<<z = << z<<endl;

system(pause);
return 0;
}

// FUNCTION DEFINITIONS
//=======================================================
void greetings(void)
{
cout<<Welcome !<<endl;

return ;
}
//=======================================================

int sum(int x, int y)
{
int S;
S = x + y ;

return (S);
}



// OUTPUT OF THIS PROGRAM
Welcome !
Enter the value for x : 3
Enter the value for y : 4
z = 7
a= 10
z = 14

SAMPLE PROGRAM 3 : CALL OR PASS BY VALUE, FORMAL PARAMETER AND
ACTUAL PARAMETER NAMES MADE SIMILAR
#include <iostream>
using namespace std;
void swap(int x, int y );
int main ( )
{ int x = 10, y = 20;
swap (x,y); // a void function is not assigned to any
// variable because it has no return value
cout<<INSIDE MAIN FUNCTION :\n;
cout<<x = <<x<< y= <<y<<endl;
swap(100,200);
cout<<INSIDE MAIN FUNCTION :\n;
cout<<x = <<x<< y= <<y<<endl;
system(pause);
return 0;
}
void swap(int x, int y )
{ int temp;
temp = x;
x = y; y = temp;
cout<< INSIDE the SWAP FUNCTION : <<endl:
cout<<x = <<x<< y= <<y<<endl:
return; // void function returns nothing, return statement can be omitted
}




TWO TYPES OF FUNCTION CALL:

1.) CALL-BY-VALUE ( OR PASS BY VALUE) -when a variable is passed by the calling
program to the function, only a copy of the variables value is passed to the
function, so when the function changes this value inside the functions body, the
change occurs only inside the functions body, it does not change the original
value of the variable (actual parameter) inside the calling routine (main function)
even though the actual parameter and the formal parameter has the same
identifier name.

2.) CALL BY REFERENCE (OR PASS BY REFERENCE) when a variable is passed by
the calling program to the function, it passes the memory address of the variable
to the functions formal parameter, so when the function changes the value of the
variable inside the functions body, it also changes the actual value of the variable
inside the body of the calling program. The formal parameter of this function uses
the prefix, &, ( address-of operator), to receive the address of the variable and
manipulate the value stored in that memory address; this formal parameter that
uses the prefix, &, is called the REFERENCE PARAMETER.



OUTPUT OF THIS PROGRAM:
INSIDE the SWAP FUNCTION:
x=20 y= 10
INSIDE MAIN FUNCTION :
x = 10 y= 20
INSIDE the SWAP FUNCTION:
x=200 y= 100
INSIDE MAIN FUNCTION :
x = 10 y= 20

SAMPLE PROGRAM 4: CALL OR PASS BY REFERENCE
#include <iostream>
using namespace std;
void swap(int &x, int &y );
int main ( )
{ int x = 10, y = 20;
cout<<x = <<x<< y= <<y<<endl;
swap (x,y);
cout<<x = <<x<< y= <<y<<endl;
system(pause);
return 0;
}
void swap(int &x, int &y )
{ int temp;
temp = x;
x = y; y = temp;
return; // void function returns nothing, return statement can be omitted
}


PASSING VALUE THROUGH THE RETURN
STATEMENT
PASSING VALUE THROUGH THE
REFERENCE PAEAMETER
#include <iostream>
using namespace std;
int AREA(int L, int W );
int main ( )
{ int x = 10, y = 20, A;

A = AREA (x,y);
cout<<Area= <<A<<endl;
system(pause);
return 0;
}

int AREA(int L, int W )
{ int K;
K = L * W;

return K;
}

#include <iostream>
using namespace std;
void AREA(int L, int W, int &k );
int main ( )
{ int x = 10, y = 20, A=0;

AREA (x,y,A);
cout<<Area= <<A<<endl;
system(pause);
return 0;
}

void AREA(int L, int W, int &K )
{
K = L * W;

return ;
}


OUTPUT :
x = 10 y = 20
x= 20 y = 10
UDF_1.) Write a complete program that will call the 3 functions that you will
define. The 3 functions are:

a.) Function Factorial that will compute for the factorial of any number, N,
passed to it. Declare it to be of type void with the computed value of the factorial
passed to the calling program through its reference parameter.
b.) Function MYPow that will solve for x^y and return the computed value
through its return statement. ( the return value should be of type double.)
c.) Function YourChoice that will ask the user to enter his/her name, then, ask
him/her to select which one of the above 2 mathematical operations to perform.
The choice (1 or 2) will be returned by the function to the calling program.


Sample Input/Output :

Enter your name : Tomas
Hi, Tomas, please select :
1.) Solve for the factorial of N
2.) Solve for x^ y
Enter your choice (1,2) : 1 // 2
Enter the value for N : 4 // Enter the values for x and y : 2 3
4! = 24 // 2^ 3 = 8
Complete the program:
#include <iostream>
using namespace std:
// Write down all the function prototypes below :





int main( )
{
int choice, N;
double x, y, P, F;
// Call the function YourChoice, store the return value to variable choice:



switch(choice)
{
case 1 :cout<< Enter the value for N : ; cin>>N;
// Call the function Factorial, store the computed value to variable, F,
// using the reference parameter :


cout<<N<<! = <<F<<endl;
break;
case 2 : cout<< Enter the values for x and y : ; cin>>x>>y;
// Call the function MyPow, store the computed value to variable ,P,
// using the return statement :


cout<<x<< ^ <<y<< = <<P<<endl; break;
}
system(pause);
return 0;
}
// Write down all the function definitions below :
// definition for function YourChoice











// definition for function MyPow
















// definition for function Factorial

Potrebbero piacerti anche