L16FunctionsI.ppt

Download Report

Transcript L16FunctionsI.ppt

Functions I
Creating a programming
with small logical units of code
CMSC 104
1
Structured Programming

CMSC 104
Structured programming is a problem solving
strategy and a programming methodology
that includes the following guidelines
o The flow of control in the program should
be as simple as possible
o The construction of a program should
embody top-down design
2
Top-Down Design



CMSC 104
Involves repeatedly decomposing a problem
into smaller problems
Eventually leads to
o a collection of small problems or tasks
o each can be easily coded
The function construct in C is used to
write code for these small, simple
problems
3
Function Invocation



CMSC 104
A program is made up of one or more
functions, one of which is main( )
Execution always begins with main( )
When program control encounters a function
name, the function is called, or invoked
o Program control passes to the function
o The function is executed
o Control is passed back to the calling
environment
4
Example function call
#include <stdio.h>
main ( )
{
printf is the name of a
function in the stdio library
printf (“Hello World!\n”);
}
this is a string we are passing
as an argument to the printf function
CMSC 104
this statement
is known as
a function
call
5
Functions

We have used 3 functions so far
o printf ( )
o scanf ( )
o getchar ( )

Programmers can write their own
functions
CMSC 104
6
PrintMessage function
#include <stdio.h>
void PrintMessage (void);
main ( )
{
PrintMessage ( );
}
void PrintMessage (void)
{
printf (“A message for you:\n\n”);
printf (“Have a Nice Day!\n”);
}
CMSC 104
7
Examining PrintMessage
#include <stdio.h>
void PrintMessage (void);
main ( )
{
PrintMessage ( );
}
void PrintMessage (void)
{
printf (“A message for you:\n\n”);
printf (“Have a Nice Day!\n”);
}
CMSC 104
function Prototype
function call
function header
function
body
8
The function prototype

Informs the compiler that there will a function
defined later that :
returns this type
has this name
takes these arguments
void PrintMessage (void);

CMSC 104
Needed because the function call is made
before the definition -- the compiler uses it to
see if the call is made properly
9
The Function Call


Passes program control to the function
Must match the prototype in name and
number and types of arguments
void PrintMessage (void);
main ( ) same name no arguments
{
PrintMessage ( );
}
CMSC 104
10
The Function Definition

Control was passed to the function by the
function call, so the statements within the
function body will be executed
void PrintMessage (void)
{
printf (“A message for you:\n\n”);
printf (“Have a Nice Day\n”);
}

CMSC 104
After the statements in the function have
completed, control is passed back to the
calling function... in this case main ( )
11
Syntax of a
Function Definition
type FunctionName (list of formal parameters)
{
variable declarations
statements
}


CMSC 104
Everything before the first brace is the header
Everything between the braces is the body
12
Another version of
PrintMessage
void PrintMessage (int counter);
main ( )
{
int num;
printf (“Enter an integer: “);
scanf (“%d”, &num);
PrintMessage (num);
one argument
}
of type int
CMSC 104
void PrintMessage (int counter)
{
int i;
for (i = 0; i < counter; i++)
{
printf (“Have a nice day\n\n”);
}
}
matches the one
formal parameter
of type int
13
The Function Header Comment
/**********************************************
** PrintMessage
** prints out “Have a Nice Day” the number
** of times specified by the parameter ‘counter’
*************************************************/
void PrintMessage (int counter)
{
int i;
for (i = 0; i < counter; i++)
{
printf (“Have a nice day\n\n”);
}
}
CMSC 104
14