Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions I Creating a programming with small logical units of code.

Similar presentations


Presentation on theme: "Functions I Creating a programming with small logical units of code."— Presentation transcript:

1 Functions I Creating a programming with small logical units of code

2 Structured Programming
Structured programming is a problem solving strategy and a programming methodology that includes the following guidelines The flow of control in the program should be as simple as possible The construction of a program should embody top-down design

3 Top-Down Design Involves repeatedly decomposing a problem into smaller problems Eventually leads to a collection of small problems or tasks each can be easily coded The function construct in C is used to write code for these small, simple problems

4 Function Invocation 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 Program control passes to the function The function is executed Control is passed back to the calling environment

5 Example function call #include <stdio.h>
main ( ) printf is the name of a { function in the stdio library printf (“Hello World!\n”); this statement } is known as this is a string we are passing a function as an argument to the printf function call

6 Functions Programmers can write their own functions
We have used 3 functions so far printf ( ) scanf ( ) getchar ( ) Programmers can write their own functions

7 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”);

8 Examining PrintMessage
#include <stdio.h> void PrintMessage (void); function Prototype main ( ) { PrintMessage ( ); function call } void PrintMessage (void) function header printf (“A message for you:\n\n”); function printf (“Have a Nice Day!\n”); body

9 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); Needed because the function call is made before the definition -- the compiler uses it to see if the call is made properly

10 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 ( ); }

11 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”); } After the statements in the function have completed, control is passed back to the calling function... in this case main ( )

12 Syntax of a Function Definition
type FunctionName (list of formal parameters) { variable declarations statements } Everything before the first brace is the header Everything between the braces is the body

13 Another version of PrintMessage
void PrintMessage (int counter); main ( ) { int num; printf (“Enter an integer: “); scanf (“%d”, &num); PrintMessage (num); one argument matches the one } of type int formal parameter of type int void PrintMessage (int counter) int i; for (i = 0; i < counter; i++) printf (“Have a nice day\n\n”); }

14 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”); }


Download ppt "Functions I Creating a programming with small logical units of code."

Similar presentations


Ads by Google