5.
1: Functions
Knowledge
Understanding of the modular design using function
Skills
Writing programs using user-defined functions and
library functions
1
Functions
Functions are the building blocks of any C
program.
Each function is usually designed to do one
task, because a shorter and highly focused
function would be easier to program and
debug.
When a function is called, program control is
transferred to that function to perfom its
spesific job, and when finished, control is
tranfered back to the calling function.
DEC20012 –Programming Fundamental 2
Functions
Types of functions :
Predefined Function
User-defined Function
An approach used to handle complexity in solving
problems based on the “divide and conquer” strategy
A larger problem (more complex) is broken up into
smaller problems (less complex)
The solution to the problem (a module) consists of a
combination of solutions for the smaller problems (sub-
modules)
Each sub-module also has its own input(s) and output(s)
DEC20012 –Programming Fundamental 3
The Use of Functions
Dividing a large program into Functions improves the
understanding of the problem at hand. Thus, it helps to find
a better solution for the problem and makes it easier to
implement the solution.
Helps program organization and make it easier to keep track
of what each function is doing.
Functions usually small blocks of code, thus, they are
manageable and easier to maintain.
Functions are reusable code. Once written, they can be used
many times in the same and other programs.
Top-Down Design
Top-down design is a method of building
programs based on solution structures derived
from structured decomposition
Solution to every sub-problem in the structure
chart is usually coded as a function in a program
Function is a subprogram which carries out a
specific task
Pre-Defined Functions
Are functions that are already written and are grouped
according to their functionality and usage in files
called libraries.
Each library is associated with a header file (.h
extension).
To use predefined functions, the appropriate header files
are included to provide input/output functions.
Example :
the header file stdio.h is included to provide
input/output functions
User-Defined Functions
A function that is designed to do a specific task that
eventually becomes part of the overall design and
code of the program.
Functions designed, written or defined by
programmers
3 steps necessary for creating and using a function :
Step 1 : Function prototype (declaring a function)
Step 2 : Function Call (Using a function)
Step 3 : Function Definition (writing the function code)
Function Prototypes
A function prototype declares a certain information pertaining to the function
C compiler uses the information in the function prototype for compiling
function calls in a program
This form is similar to the function definition head
Syntax for function prototyping:
function_type function_name (parameter_definition_list);
• function_type is the type of data item that is returned to the caller
(i.e: int, float)
• function_name is the name of the function
• Parameter_definition_list specifies the type of data items passed to the
function. The data types are placed between parentheses and if there is more
than 1 item, they are separated by commas (,)
Function Definition
A function Definition consists of TWO parts :
Function header
Function
Function body header
The general form for function definition is
function_type function_name (parameter list)
{
function body
} Function
body
Function Call
A function Call is a statement in the program that identifies by name the function to be called
and the arguments to be passed.
To execute a function, it must first be called with the following information:
Stating the function name
Providing a certain information (parameter)
The general format for a function call is :
function_name (parameter list);
* function_name is the name of the function called
* parameter list is the list of expressions that identifies arguments to be passed to
the function. The number of parameters provided must be the same as defined in the
function definition
The Return Statement
A function returns a value via a return
statement
When a return statement is executed, program
control is returned to the caller along with the
return value
Example:
int computeRectArea (int width, int height)
{ int area ;
area = width * height ;
return area ;
}
The Return Statement
The return statement has TWO use :
It causes an immediate exit from the function whenever the return
statement is executed.
It can be used to return a value back to the caller. The return value
data type must match the function return type
Example :
function_type function_name (parameter list)
{
function body
return (value); The return value
data type must
}
match the function
type
The Return Statement
In a function which does not return any
value, the return statement (without
expression) will result in the program
control being returned to the caller.
The Return Statement
Example:
void displayLine (int length)
{ int i = 0;
while (1)
{ if (i == length)
{ cout <<“\n”;
return;
}
cout << “*”;
i++;
}
}
Example:
Program to display
lines of characters
Display character line
Example:
#include <stdio.h>
#include <conio.h>
void displayLine( );
main()
main( ) function
{
displayLine( );
}
void displayLine( )
{
printf (“Politeknik Port Dickson\n“);
} displayLine( ) function
Example:
#include <stdio.h>
#include <conio.h>
void displayLine( ); displayLine( ) function prototype
main()
{
displayLine( );
Function call
} - A call to displayLine( ) function
void displayLine( )
displayLine( ) function
{
printf (“Politeknik Port Dickson\n”);
}
Example:
#include <stdio.h>
#include <conio.h>
void displayLine( );
Function head
main()
{
displayLine( );
}
void displayLine( )
{
printf (“Politeknik Port Dickson\n“);
}
Example:
#include <stdio.h>
#include <conio.h>
void displayLine( );
main() Function body
{
displayLine( );
}
void displayLine( )
{
printf (“Politeknik Port Dickson\n“);
}
Example:
#include <stdio.h>
#include <conio.h>
void displayLine( ); Function call
- A call to displayLine( )
main() function
{
displayLine( );
}
void displayLine( )
{
printf (“Politeknik Port Dickson\n“);
}
Example:
#include <iostream.h>
void displayLine( );
main()
{
displayLine( );
}
void displayLine( )
{
printf (“Politeknik Port Dickson\n”);
}
Example:
#include <iostream.h>
void displayLine( );
main()
{
displayLine( );
}
void displayLine( )
{
printf (“Politeknik Port Dickson\n”);
}
Example:
Politeknik Port Dickson
#include <iostream.h> _
void displayLine( );
main()
{
displayLine( );
}
void displayLine( )
{
printf (“Politeknik Port Dickson\n”);
}
Example:
Politeknik Port Dickson
#include <iostream.h> _
void displayLine( );
main()
{
displayLine( );
}
void displayLine( )
{
printf (“Politeknik Port Dickson\n”);
}
Function Characteristics
Function characteristics:
With parameters?
Returns a value?
Function Without Parameter
and Not Returning Any Value
A function which has no parameter and
return value may be defined as follows:
voidvoid function_name( )( )
{ local_variable _declaration;
executable_function_statements;
} Function without any
Function which does not
parameter
return any value
Example:
#include <stdio.h>
void inputComputeAndOutputArea( );
int main()
{ inputComputeAndOutputArea( );
}
void inputComputeAndOutputArea( )
{ int width, height, area;
scanf (“%d %d”, &width, &height);
area = width * height;
printf (“Area: ”, area);
}
Example:
#include <stdio.h>
void inputComputeAndOutputArea( );
int main( )
{ inputComputeAndOutputArea( );
}
void inputComputeAndOutputArea( )
{ int width, height, area;
scanf (“%d %d”, &width, &height);
area = width * height;
printf (“Area: ”, area);
}
Example:
#include <stdio.h>
void inputComputeAndOutputArea( );
int main( )
{ inputComputeAndOutputArea( );
}
void inputComputeAndOutputArea( )
{ int width, height, area;
width ???
scanf (“%d %d”, &width, &height);
area = width * height; ???
printf (“Area: ”, area);
height
} area ???
_
Example:
#include <stdio.h>
void inputComputeAndOutputArea( );
int main( )
{ inputComputeAndOutputArea( );
}
void inputComputeAndOutputArea( )
{ int width, height, area;
width 12
scanf (“%d %d”, &width, &height);
area = width * height; 8
printf (“Area: ”, area);
height
} area ???
12 8
_
Example:
#include <stdio.h>
void inputComputeAndOutputArea( );
int main( )
{ inputComputeAndOutputArea( );
}
void inputComputeAndOutputArea( )
{ int width, height, area;
width 12
scanf (“%d %d”, &width, &height);
area = width * height; 8
printf (“Area: ”, area);
height
} area 96
12 8
_
Example:
#include <stdio.h>
void inputComputeAndOutputArea( );
int main( )
{ inputComputeAndOutputArea( );
}
void inputComputeAndOutputArea( )
{ int width, height, area;
width 12
scanf (“%d %d”, &width, &height);
area = width * height; 8
printf (“Area: ”, area);
height
} area 96
12 8
Area: 96
_
Example:
#include <stdio.h>
void inputComputeAndOutputArea( );
int main( )
{ inputComputeAndOutputArea( );
}
void inputComputeAndOutputArea( )
{ int width, height, area;
scanf (“%d %d”, &width, &height);
area = width * height;
printf (“Area: ”, area);
}
12 8
Area: 96
_
Function With Parameters
For a C function, a parameter needs to be
defined for each data to be handed by a caller
void outputFirstLetter(char firstLetter)
{
printf (“First letter:”, firstLetter;
}
The caller to this function needs to hand a data
of type char. In this function, the data is kept in a
variable named firstLetter
Function With Parameters
Parameter’s name is optional in function prototype
void displayArea (int width, int height)
{
printf (“Area: ”, width * height);
}
The following function prototypes are valid:
void displayArea(int width, int height);
void displayArea(int, int);
Example:
#include <stdio.h>
void computeAndOutputArea (int width, int height);
int main( )
{ int rectWidth, rectHeight ;
scanf (“%d %d”, &rectWidth, &rectHeight) ;
computeAndOutputArea (rectWidth, rectHeight);
}
void computeAndOutputArea (int width, int height)
{ printf (“Area: ”, width * height) ;
}
Example:
#include <stdio.h>
void computeAndOutputArea (int width, int height);
???
int main( ) rectWidth
{ int rectWidth, rectHeight ;
scanf (“%d %d”, &rectWidth, &rectHeight) ;
???
computeAndOutputArea (rectWidth, rectHeight); rectHeight
}
void computeAndOutputArea (int width, int height)
{ printf (“Area: ”, width * height) ;
}
_
Contoh:
#include <stdio.h>
void computeAndOutputArea (int width, int height);
7
int main( ) rectWidth
{ int rectWidth, rectHeight ;
scanf (“%d %d”, &rectWidth, &rectHeight) ;
8
computeAndOutputArea (rectWidth, rectHeight); rectHeight
}
void computeAndOutputArea (int width, int height)
{ printf (“Area: ”, width * height) ;
}
78
_
Example:
#include <stdio.h>
void computeAndOutputArea (int width, int height);
7
int main( ) rectWidth
{ int rectWidth, rectHeight ;
scanf (“%d %d”, &rectWidth, &rectHeight) ;
8
computeAndOutputArea (rectWidth, rectHeight); rectHeight
}
void computeAndOutputArea (int width, int height)
{ printf (“Area: ”, width * height) ;
}
78
_
Example:
#include <stdio.h>
void computeAndOutputArea (int width, int height);
7
int main( ) rectWidth
{ int rectWidth, rectHeight ;
scanf (“%d %d”, &rectWidth, &rectHeight) ;
8
computeAndOutputArea (rectWidth, rectHeight); rectHeight
}
void computeAndOutputArea (int width, int height)
7
{ printf (“Area: ”, width * height) ;
} width
78 8
_ height
Example:
#include <stdio.h>
void computeAndOutputArea (int width, int height);
int main( )
{ int rectWidth, rectHeight ;
scanf (“%d %d”, &rectWidth, &rectHeight) ;
computeAndOutputArea (rectWidth, rectHeight);
}
void computeAndOutputArea (int width, int height)
7
{ printf (“Area: ”, width * height) ;
} width
78 8
Area: 56 height
_
Example:
#include <stdio.h>
void computeAndOutputArea (int width, int height);
7
int main( ) rectWidth
{ int rectWidth, rectHeight ;
scanf (“%d %d”, &rectWidth, &rectHeight) ;
8
computeAndOutputArea (rectWidth, rectHeight); rectHeight
}
void computeAndOutputArea (int width, int height)
{ printf (“Area: ”, width * height) ;
}
78
Area: 56
_
Function That Returns a Value
Besides receiving data from the caller, a function
can also return a value to the caller.
Example:
int computeRectArea (int width, int height)
{ int area ;
area = width * height ;
return area ;
}
Function That Returns a Value
The data type of the value being returned must
be specified in the function definition.
return_type function_name (parameter_list)
return_type
{ local_variable_definition;
statements;
}
Value type being returned by the function
Example:
#include <iostream.h>
int computeArea (int width, int height) ;
void outputArea (int area) ;
int main ( )
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight);
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ;
}
int computeArea (int width, int height)
{ return width * height ;
}
void outputArea (int area)
{ printf (“Rectangle area:”, area) ;
}
Example:
#include <stdio.h>
???
int computeArea (int width, int height) ; rectWidth
void outputArea (int area) ; ???
int main ( )
{ int rectWidth, rectHeight, rectArea ;
rectHeight
scanf (“%d %d”, &rectWidth, &rectHeight); ???
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ;
rectArea
}
int computeArea (int width, int height)
{ return width * height ;
}
void outputArea (int area)
{ printf (“Rectangle area:”, area) ;
}
_
Example:
7
#include <stdio.h>
int computeArea (int width, int height) ; rectWidth
void outputArea (int area) ; 11
int main ( )
rectHeight
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight); ???
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ; rectArea
}
int computeArea (int width, int height)
{ return width * height ;
}
void outputArea (int area)
{ printf (“Rectangle area:”, area) ;
}
7 11
_
Example:
7
#include <stdio.h>
int computeArea (int width, int height) ; rectWidth
void outputArea (int area) ; 11
int main ( )
rectHeight
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight); ???
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ; rectArea
}
int computeArea (int width, int height)
{ return width * height ;
}
void outputArea (int area)
{ printf (“Rectangle area:”, area) ;
}
7 11
_
Example:
7
#include <stdio.h>
int computeArea (int width, int height) ; rectWidth
void outputArea (int area) ; 11
int main ( )
rectHeight
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight); ???
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ; rectArea
}
int computeArea (int width, int height)
{ return width * height ;
}
void outputArea (int area) 7 11
{ printf (“Rectangle area:”, area)width
; height
}
7 11
_
Example:
#include <stdio.h>
int computeArea (int width, int height) ;
void outputArea (int area) ;
int main ( )
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight);
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ;
}
int computeArea (int width, int height)
{ return width * height ;
}
77 7 11
void outputArea (int area)
{ printf (“Rectangle area:”, area); width height
}
7 11
_
Example:
7
#include <stdio.h>
int computeArea (int width, int height) ; rectWidth
void outputArea (int area) ; 11
int main ( )
rectHeight
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight); 77
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ; rectArea
}
int computeArea (int width, int height)
{ return width * height ;
}
void outputArea (int area)
{ printf (“Rectangle area:”, area) ;
}
7 11
_
Example:
7
#include <stdio.h>
int computeArea (int width, int height) ; rectWidth
void outputArea (int area) ; 11
int main ( )
rectHeight
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight); 77
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ; rectArea
}
int computeArea (int width, int height)
{ return width * height ;
}
void outputArea (int area)
{ printf (“Rectangle area:”, area) ;
}
7 11
_
Example:
7
#include <stdio.h>
int computeArea (int width, int height) ; rectWidth
void outputArea (int area) ; 11
int main ( )
rectHeight
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight); 77
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ; rectArea
}
int computeArea (int width, int height)
{ return width * height 7;11
} _
void outputArea (int area)
{ printf (“Rectangle area:”, area) ;
}
77
area
Example:
#include <stdio.h>
int computeArea (int width, int height) ;
void outputArea (int area) ;
int main ( )
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight);
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ;
}
int computeArea (int width, int height)
{ 7711
return width * height 11;
} _Rectangle area: 77
void outputArea (int area)_
{ printf (“Rectangle area:”, area) ;
}
77
area
Example:
#include <stdio.h>
7
int computeArea (int width, int height) ; rectWidth
void outputArea (int area) ; 11
int main ( )
rectHeight
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight); 77
rectArea = computeArea (rectWidth, rectHeight) ;
outputArea (rectArea) ; rectArea
}
int computeArea (int width, int height)
{ 7 11;
return width * height
} Rectangle area: 77
void outputArea (int area)_
{ printf (“Rectangle area:”, area) ;
}
Example:
#include <stdio.h>
7
int computeArea (int width, int height) ; rectWidth
void outputArea (int area) ; 11
int main ( ) rectHeight
{ int rectWidth, rectHeight, rectArea ;
scanf (“%d %d”, &rectWidth, &rectHeight); 77
rectArea = computeArea (rectWidth, rectHeight) ;
rectArea
outputArea (rectArea) ;
}
int computeArea (int width, int height)
{ 7 11;
return width * height
} Rectangle area: 77
void outputArea (int area)_
{ printf (“Rectangle area:”, area) ;
}