Sunteți pe pagina 1din 15

CSC128

Mahfudzah Othman
UiTM Perlis

CHAPTER 5
FUNCTIONS

 Function is also called as subprogram, procedure/module.


 It has a specific task.
 Function: is a block of instructions that is executed when it is called from
some other point of the program.
 Why it is used?
o Breaking program to smaller items which will be easier to code, test
& debug.
o Reusable of certain functions.
 There are TWO categories of functions:
o Predefined function.
o User defined function.

 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

Name Description Type of Type of Example Value


Arguments Value
Returned
sqrt(x) Squareroot, Double Double sqrt(4.0) 2.0
√x
pow(x, y) Powers, xy Double Double pow(2.0, 8.0
3.0)
fabs(x) Absolute Double Double fabs(-3.5) 3.5
value for fabs(3.5)
double
ceil(x) Ceiling Double Double ceil(3.4) 3.4
(round up) ceil(3.8) 3.4
floor(x) Floor (round Double Double floor(3.1) 3.0
down) floor(3.8) 3.0

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

Name Description Example Value


abs(x) Absolute value for x, x an abs(-5) 5
integer abs(5) 5
labs(x) Absolute value for x, x a labs(-50000) 50000
long integer labs(50000) 50000
rand() Random integer rand() Any number

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];

cout<<"Enter your name:";


cin>>ws;
cin.getline(name1, 30);
cout<<"Enter your name:";
cin>>ws;
cin.getline(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

 USER DEFINED FUNCTION


o There are 2 types of user defined functions:
 Independent functions
 Member functions (belongs to certain object)
 Advantages of using functions:
 Problems can be factored into understandable & manageable
steps.
 Provide a way to reuse code that is required in more than one place
in a program.
 The reusing of the standard library.
 Use a function to protect data.

USER DEFINED FUNCTION

 When to use independent function in a program, it must have 3 important


things:
o Function Prototype
o Calling Function Statement
o Function Definition

 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:

function_return_type function_name (parameter_type_list)

Void: when no data formal_parameter-type_1, formal_parameter-


will be returned to type_2,…,formal_parameter-type-n
calling function
Formal parameter: is used as a kind of
Data type: int, float, blank, or place holder to stand in for the
double, char, char*, argument. When we write a function
class_name prototype, we don’t know what the argument
will be, so we use the formal parameters in
place of the arguments.

Example:
o Float calcAvg(int, int, int);//returns the average of 3 numbers
o Char findGrade(float);//find grade based on marks

4
CSC128
Mahfudzah Othman
UiTM Perlis

 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.

 Syntax: function_return_type function_name(parameter-list)


 Return Statement:
 When the function is called, the argument values are plugged in
for the formal parameters (if any) & then the statements in the
body are executed.
 The value returned by the function (if any) is determined when
the function executes a return statement.
 Local Variable:
 Variables that are declared within the body of a function
definition/main().
 If a variable is local to a function, then you can have another
variable with the same name that is declared in the main() or in
another function definitions and these will be TWO different
variables, even though they have the same name.
 Global variable:
 Variables that are declared OUTSIDE main()/other function
definitions, which are accessible to all function definitions in the
file.
 Parameter:
 A way how calling function & called function communicate.
 Thru parameter, data can pass from calling function to called
function & from called function to calling function.
 Formal parameters: variables that are declared in the header of
the function definition.
 Actual parameters: are expressions in the calling function
statement.

5
CSC128
Mahfudzah Othman
UiTM Perlis

EXAMPLE: DIFFERENCES BETWEEN PROGRAM USING FUNCTION &


PROGRAM WITHOUT FUNCTION

Write a program that will calculate the SUM of two numbers. The function given
is int CalSum(int, int)

Program WITHOUT function Program WITH function


void main() #include<iostream.h>
{ #include<conio.h>
int no1, no2, sum;
int CalSum (int, int);
cout<<"Enter 2 integer numbers:";
cin>>no1>>no2; void main()
{
sum=no1+no2; int no1, no2, sum;
cout<<"Enter 2 integers";
cout<<"Sum="<<sum; cin>>no1>>no2;
}
sum=CalSum(no1, no2);

cout<<"Sum="<<sum;
getch();
}

int CalSum (int x, int y)


{
int Jum;
Jum=x+y;
return Jum;
}

6
CSC128
Mahfudzah Othman
UiTM Perlis

ANALYSIS I

#include<iostream.h>
#include<conio.h>

int CalSum (int, int); //FUNCTION PROTOTYPE

void main()
{
int no1, no2, sum;
cout<<"Enter 2 integers";
cin>>no1>>no2;

sum=CalSum(no1, no2); //CALLING FUNCTION STATEMENT

cout<<"Sum="<<sum;
getch();
}

int CalSum (int x, int y) //FUNCTION HEADER


{
int Jum; FUNCTION FUNCTION
Jum=x+y;
BODY DEFINITION
return Jum;
}

7
CSC128
Mahfudzah Othman
UiTM Perlis

ANALYSIS II

#include<iostream.h>
#include<conio.h>

int CalSum (int, int);

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();
}

int CalSum (int x, int y)

FORMAL PARAMETER
{
int Jum; //LOCAL VARIABLE
Jum=x+y;
return Jum; //RETURN STATEMENT
}

8
CSC128
Mahfudzah Othman
UiTM Perlis

 BASIC TYPES OF FUNCTIONS

1. FUNCTION WITHOUT VALUE RETURN & PARAMETER

 Type return for this function is VOID.


 Normally, use this function to display message.
 The function that is called receives NOTHING & returns NOTHING.
 The call still requires parentheses even when there are NO actual
parameters.
 Example:

#include<iostream.h>
#include<conio.h>

void DisplayName ();//type return is VOID, parentheses empty.Means


that, function receives nothing & returns nothing

void main()
{
DisplayName();//calling function statement has no actual
parameter
}

void DisplayName() //function header has no formal parameter


{
cout<<"Mahfudzah Othman";
}

2. FUNCTION HAS VALUE RETURNED BUT WITHOUT PARAMETER

 Return a value (normally the result of calculation) to main() program or


calling function.
 Normally, we will use this function to display menu.
 It will get/read the data for calculation (if any) within its body.
 Example:

9
CSC128
Mahfudzah Othman
UiTM Perlis

#include<iostream.h>
#include<conio.h>

int CalSum ();//type return is INT, parentheses empty.Means


that, function receives nothing but returns value

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
}

3. FUNCTION WITHOUT VALUE RETURNED BUT WITH PARAMETER

 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>

void CalAve (float,float,float); //type return is VOID, parentheses


has parameters. Means that, function receives
values but returns nothing
void main()
{
float no1, no2, no3;

cout<<"Enter 3 numbers:"<<endl;
cin>>no1>>no2>>no3;
CalAve(no1, no2, no3);//calling function statement sends values thru
actual parameters
}

void CalAve(float num1, float num2, float num3)//function definition


receives values thru
formal parameters
{
float average;
average=(num1+num2+num3)/3;
cout<<"Average of 3 numbers are="<<average;//result is not returned
to main() & displayed
within the function body
getch();
}

4. FUNCTION WITH VALUE RETURNED & WITH PARAMETER

 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>

float CalAve (float,float,float); //type return is FLOAT, parentheses


has parameters. Means that, function
receives values & returns the values

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();
}

float CalAve(float num1, float num2, float num3)//function definition


receives values thru formal parameters &
will returns result
{
float average;
average=(num1+num2+num3)/3;
return average; //result is returned to main() program
}

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>

int CalSum (int, int);

void main()
{
int no1, no2, sum;
cout<<"Enter 2 integers";
cin>>no1>>no2;

sum=CalSum(no1, no2);

cout<<"Sum="<<sum;
getch();
}

int CalSum (int x, int y)


{
int Jum;
Jum=x+y;
return Jum;
}

 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 CalSum (int, int, int&);

void main()
{
int no1, no2, Sum;

cout<<"Enter 2 integers"<<endl;
cin>>no1>>no2;
CalSum(no1, no2, Sum);
cout<<"Sum="<<Sum;
getch();
}

void CalSum(int x, int y, int&Jum)


{
Jum=x+y;
}

 Example 2:

#include<iostream.h>
#include<conio.h>

int CalSum (int, int, int&);

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

int CalSum(int x, int y, int&Jum)


{
Jum=x+y;
return Jum;
}

 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

S-ar putea să vă placă și