Beruflich Dokumente
Kultur Dokumente
Objectives
To understand how to construct programs
modularly from small pieces called functions
To different system-defined functions and
programmer-defined functions
To be able to create new functions
To determine the components and
requirements needed to create functions
To differentiate between void functions and
functions that return a value
Introduction
Programs that solve real-world problems
are much larger than the programs solved in
the laboratory exercises.
Large programs are hard to develop and
maintain.
Dividing a large program into smaller pieces
or modules will lighten up the load.
[remember the Divide-and-Conquer
approach?]
Functions : the answer to that need
What is a function?
Definitions
a block of statements [declarations &
executable statements] that perform a certain
task
a block of code that performs a number of predefined commands to accomplish something
productive > www.cprogramming.com
System-defined functions
These are functions that already made
and stored in the standard library of the
programming language.
They are stored in header files.
Examples:
math.h sqrt(), exp(), log(), fabs(), pow()
stdio.h printf(), scanf()
conio.h getch()
System-defined functions
These functions are ready-to-use.
All the programmer needs to do is to
include the appropriate header file
associated with that function.
Programmer-defined functions
These are functions that are created by the
programmer.
Certain problems cannot be solved by
system-defined functions alone.
Programmers need to create their own
function to solve problems the way they
want them to be solved.
The programmer becomes a creator, and
has full control over the things happening in
the program.
Function prototypes
Before creating something, a plan or a blueprint
has to be created first.
In functions, a function prototype is needed before
the function can be formally created.
A function prototype tells the compiler
The type of data returned by the function
The number of parameters the function expects to receive
The types of the parameters
The order in which these parameters are expected.
Function prototypes
Remember int main()?
Its prototype would have been
int main();
But of course, this is NOT necessary for main()
anymore.
But for other functions, it is.
Function prototypes
Return_Type?
Refers to the data type of the value to be returned by the
function. [int, char, float, double]
int main()
FunctionName?
Any name will do, just like how you name variables.
Recommended: use names that describe its purpose.
Parameters?
Next slide please
Function definitions
When you write the source code for a
certain function, that block of code lines is
called a function definition.
A function definition contains 2 parts:
Function Header
Function Body
Function Definition
Function header
It is exactly the same with the function
prototype, except the semicolon (;).
Function body
It is consists of a compound statement or a
block of code that actually does the task the
function was created for.
Sample
int main()
{
printf(Hello World);
getch();
}
void add(void)
{
int sum = 0;
sum = p + q;
printf(Sum: %d,sum);
}
Function Calls
After everything is set, a function can now
be used.
Using a function requires that you call it.
A function call consists of:
Function name use the name that was given
during function prototype & definition
Arguments these are the actual values that
the function will be using during execution.
Parameters VS Arguments
Parameters
int add(int p, int q)
{
int sum = 0;
sum = p + q;
return sum;
}
Arguments
int main( )
{
int x ;
x = add(9, 3);
printf(%d, q);
getch();
}
DEMO - FUNCTIONS
C Functions
Constructing programs modularly
The End.