Functions: Csc128 Mahfudzah Othman Uitm Perlis
Functions: Csc128 Mahfudzah Othman Uitm Perlis
Functions: Csc128 Mahfudzah Othman Uitm Perlis
Mahfudzah Othman
UiTM Perlis
CHAPTER 5
FUNCTIONS
PREDEFINED FUNCTION:
o Functions that have been defined by the producer of a compiler.
o Declaration of the functions are stored in header file ( .h ext).
o Programmer can uses these functions using calling function
statements.
o Just include header file in our program & use the functions declared
in that file.
o Some Predefined Functions
Math.h
1
CSC128
Mahfudzah Othman
UiTM Perlis
string.h
Name Description
strcmp(string_exp1, Returns TRUE (a nonzero integer) if the values of the
string_exp2) two string expressions are different. Otherwise,
returns FALSE
strcpy(string_variable, Changes the value of the string_variable to the value
string_expression) of the string_expression
strlen(string_expression Returns the length of the string_expression
)
stdlib.h
o Example:
math.h
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
double a;
a=sqrt(9.0);//calling function statement
cout<<"The result="<<a;
}
2
CSC128
Mahfudzah Othman
UiTM Perlis
string.h
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30];
char name2[30];
if(strcmp(name1, name2)==0)
{cout<<"These names are same";}
else
{cout<<"These names are difference";}
getch();
}
stdlib.h
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int k;
k=labs(-1000);
cout<<"The result is="<<k;
}
3
CSC128
Mahfudzah Othman
UiTM Perlis
Function Prototype:
o Describes how the function is called.
o Tells:
o Name of the function
o How many arguments the function needs
o What type the argument should be
o Syntax:
4
CSC128
Mahfudzah Othman
UiTM Perlis
Example:
o Float calcAvg(int, int, int);//returns the average of 3 numbers
o Char findGrade(float);//find grade based on marks
Function Definition
Describes how the function does it jobs.
Consists of:
o Function header: written the same way as the function
prototype but without the semicolon. Must include formal
parameter in the parameter-list.
o Function body: The function body follows the function header
& completes the function definition. Consists of declarations &
executable statements enclosed within a pair of braces.
5
CSC128
Mahfudzah Othman
UiTM Perlis
Write a program that will calculate the SUM of two numbers. The function given
is int CalSum(int, int)
cout<<"Sum="<<sum;
getch();
}
6
CSC128
Mahfudzah Othman
UiTM Perlis
ANALYSIS I
#include<iostream.h>
#include<conio.h>
void main()
{
int no1, no2, sum;
cout<<"Enter 2 integers";
cin>>no1>>no2;
cout<<"Sum="<<sum;
getch();
}
7
CSC128
Mahfudzah Othman
UiTM Perlis
ANALYSIS II
#include<iostream.h>
#include<conio.h>
void main()
{
int no1, no2, sum;//LOCAL VARIABLE
cout<<"Enter 2 integers";
cin>>no1>>no2;
sum=CalSum(no1, no2);
ACTUAL PARAMETER
cout<<"Sum="<<sum;
getch();
}
FORMAL PARAMETER
{
int Jum; //LOCAL VARIABLE
Jum=x+y;
return Jum; //RETURN STATEMENT
}
8
CSC128
Mahfudzah Othman
UiTM Perlis
#include<iostream.h>
#include<conio.h>
void main()
{
DisplayName();//calling function statement has no actual
parameter
}
9
CSC128
Mahfudzah Othman
UiTM Perlis
#include<iostream.h>
#include<conio.h>
void main()
{
int Jum;
Jum=CalSum(); //calling function statement will receive result
returned by called function
cout<<"Sum of 2 numbers:"<<Jum;
getch();
}
int CalSum()
{
int no1, no2, sum;
cout<<"Enter 2 numbers:"<<endl;
cin>>no1>>no2; //values are entered within the function body
sum=no1+no2; //values are calculated within the function body
return sum; //the result will be returned to main() program
}
Get data for calculation or processing from outside the function. Means
that, it will get data from the main() program or other function definitions.
But, the calculated value will not be returned to the main() program. It will
be displayed within the function itself.
Example:
10
CSC128
Mahfudzah Othman
UiTM Perlis
#include<iostream.h>
#include<conio.h>
cout<<"Enter 3 numbers:"<<endl;
cin>>no1>>no2>>no3;
CalAve(no1, no2, no3);//calling function statement sends values thru
actual parameters
}
Get the data for calculation/processing from outside the calling function
thru the parameters & then returns a value (normally the result of
calculation) to the main() program or the other calling functions.
Example:
11
CSC128
Mahfudzah Othman
UiTM Perlis
#include<iostream.h>
#include<conio.h>
void main()
{
float no1, no2, no3, Ave;
cout<<"Enter 3 numbers:"<<endl;
cin>>no1>>no2>>no3;
Ave=CalAve(no1, no2, no3); //calling function statement sends values
thru actual parameter & will receive result
returned by the called function
cout<<"Average of 3 numbers are="<<Ave; //the returned result will be
displayed in main() program
getch();
}
12
CSC128
Mahfudzah Othman
UiTM Perlis
PARAMETER PASSING
In all the functions we have seen, the parameters passed to the function have
been passed by value. Meaning that, we have passed to the function the
values but never the specified variable themselves.
Example:
#include<iostream.h>
#include<conio.h>
void main()
{
int no1, no2, sum;
cout<<"Enter 2 integers";
cin>>no1>>no2;
sum=CalSum(no1, no2);
cout<<"Sum="<<sum;
getch();
}
Explanation:
We call function CalSum passing the values of no1 and no2, that
means if we enter 5 for no1 and 10 for no2, we will pass the values of
5 and 10.
But, any modification of x and y within the function CalSum will not
affect the values of no1 and no2 outside it.
This is because; variables no1 and no2 were not passed themselves to
the function, only their values.
13
CSC128
Mahfudzah Othman
UiTM Perlis
But, there might be some cases where you need to manipulate from inside a
function the value of an external variable.
For that purpose, we have to use arguments passed by reference.
Example 1:
#include<iostream.h>
#include<conio.h>
void main()
{
int no1, no2, Sum;
cout<<"Enter 2 integers"<<endl;
cin>>no1>>no2;
CalSum(no1, no2, Sum);
cout<<"Sum="<<Sum;
getch();
}
Example 2:
#include<iostream.h>
#include<conio.h>
void main()
{
int no1, no2, Jum, Sum;
cout<<"Enter 2 integers"<<endl;
cin>>no1>>no2;
Jum=CalSum(no1, no2, Sum);
cout<<"Sum="<<Jum;
getch();
}
14
CSC128
Mahfudzah Othman
UiTM Perlis
Explanation:
In the declaration of CalSum, the type of each argument went followed
by an ampersand (&). This specify that the variable has to be passed
by reference instead of by value, as usual.
When passing a variable by reference, we are passing the variable
itself & any modification that we do to that parameter within the
function, will have effect in the passed variable outside it.
Passing by reference is an effective way to allow a function to return
more than one single value.
Rules for parameter using reference:
The corresponding actual & formal parameter must be of the same
data type.
The actual parameter in calling function statement must be variable.
The formal parameter must used the reference operator; ampersand
(&).
15