Professional Documents
Culture Documents
Chap 5.1 2
Chap 5.1 2
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.
{
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)
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
void displayLine( );
main()
main( ) function
{
displayLine( );
}
void displayLine( )
{
printf (“Politeknik Port Dickson\n“);
} displayLine( ) function
Example:
#include <stdio.h>
#include <conio.h>
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( );
void displayLine( )
{
printf (“Politeknik Port Dickson\n“);
}
Example:
#include <stdio.h>
#include <conio.h>
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
int main( )
{ int rectWidth, rectHeight ;
scanf (“%d %d”, &rectWidth, &rectHeight) ;
computeAndOutputArea (rectWidth, rectHeight);
}
int main( )
{ int rectWidth, rectHeight ;
scanf (“%d %d”, &rectWidth, &rectHeight) ;
computeAndOutputArea (rectWidth, rectHeight);
}
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) ;
}