Sei sulla pagina 1di 32

C Functions

Constructing programs modularly

CProg1-121 Computer Programming 1

Mr. Exander T. Barrios, MIM

UIC - ITE Instructor


AY 2008 - 2009

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

A function can be composed of a single


or several lines or blocks of codes.

Why use functions?


Functions are reusable:
You can write functions once and use it as
many times as necessary without having to
rewrite the same source code.

Why use functions?


Programs become smaller with
functions
We eliminate lines of codes by calling
functions instead of replicating code lines
again.

Why use functions?


Programs are easier to write when
segmented into functions
With functions, you can organize
programs into manageable sections or
segments.

Why use functions?


Functions are easier to read than one
big block of program.
With functions you can distinguish which
block codes does a certain task, avoids
complexity and provides individuality
between blocks of codes.

Why use functions?


Programs are easier to maintain with
functions
It is easier to understand and modify
smaller group or block of codes which
performs a common task if grouped
together, through functions.

Two types of functions


Functions are recognizable because of the
presence of parentheses (), as in the
main() function.
Two types of functions
System-defined functions
Programmer-defined functions

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.

How to create functions?


Before we can start creating functions, we
need to know first these important
concepts.
Function prototypes
Function definition
Function calls

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.

Format of a function prototype:


Return_Type FunctionName(parameter list);

Function prototypes
Return_Type?
Refers to the data type of the value to be returned by the
function. [int, char, float, double]
int main()

If your function does not return a value, use void.

FunctionName?
Any name will do, just like how you name variables.
Recommended: use names that describe its purpose.

Parameters?
Next slide please

Now what is a Parameter?


A parameter is a dummy variable that is
used as a container, for a value that will
be passed during function calls.
How come int main() has no parameters?
Only use parameters when you intend
your function to receive value from other
functions.

Examples of function prototypes


without parameters
A function that will add two integers but
does not return a value.
void addTwoInt();

A function that will add two integers and


returns the result to the calling entity.
int addTwoInt();

Examples of function prototypes


with parameters
A function that receives two integers,
computes the sum, but does not return a
value.
void addTwoInt(int a, int b);

A function that receives two integers,


computes the sum, and returns a value.
int addTwoInt(int a, int b);

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.

General Format of function definitions


Format
Function Header
{
Function Body
}

Sample
int main()
{
printf(Hello World);
getch();
}

The return statement


A function returns when it encounters the
closing brackets or when it executes the
return statement.
Example: int addTwoInt(int m, int n)
{
int sum = 0;
sum = m * n;
return sum;
}

The return statement


To return or not to return? that is the
question.
A function may or may not return a
specific value of a specific data type.
If a function returns a value, the function
must have a return data type that
matches the data type of the returned
value.

The return statement


If the function does not return any value,
then the function must have a void return
type. Which means that it does not return
anything in specific.

Example: (Spot the difference?)


int add(int p, int q)
{
int sum = 0;
sum = p + q;
return sum;
}

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 and Arguments


(Separated at birth?)

The terms, parameters (sometimes known


as formal arguments) and arguments
(sometimes referred to as actual
arguments) refer to the same values in
different places..
Meaning, arguments are found in a
function call while parameters are the
values received by the function in the
function definition & function prototype.

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

CProg1-121 Computer Programming 1

The End.