DOC PREVIEW
UMBC CMSC 104 - Functions, Part 1 of 3

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Functions, Part 1 of 3Review of Structured ProgrammingReview of Top-Down DesignFunctionsSample Function CallFunctions (cont.)Sample Programmer-Defined FunctionExamining printMessageThe Function PrototypeThe Function CallThe Function DefinitionGeneral Function Definition SyntaxUsing Input ParametersFinal “Clean” C CodeFinal “Clean” C Code (cont).Good Programming PracticeFunctions, Part 1 of 3TopicsUsing Predefined FunctionsProgrammer-Defined FunctionsUsing Input ParametersFunction Header CommentsReadingSections 5.1 - 5.8Review of Structured ProgrammingStructured programming is a problem solving strategy and a programming methodology that includes the following guidelines:The program uses only the sequence, selection, and repetition control structures.The flow of control in the program should be as simple as possible.The construction of a program embodies top-down design.Review of Top-Down DesignInvolves repeatedly decomposing a problem into smaller problemsEventually leads to a collection of small problems or tasks each of which can be easily codedThe function construct in C is used to write code for these small, simple problems.FunctionsA C program is made up of one or more functions, one of which is main( ).Execution always begins with main( ), no matter where it is placed in the program. By convention, main( ) is located before all other functions.When program control encounters a function name, the function is called (invoked).Program control passes to the function.The function is executed.Control is passed back to the calling function.#include <stdio.h> int main ( ) printf is the name of a predefined { function in the stdio library printf (“Hello World!\n”) ; this statement is return 0 ; is known as a} function call this is a string we are passing as an argument (parameter) to the printf functionSample Function CallFunctions (cont.)We have used three predefined functions so far:printfscanfgetcharProgrammers can write their own functions.Typically, each module in a program’s design hierarchy chart is implemented as a function.C function names follow the same naming rules as C variables.Sample Programmer-Defined Function#include <stdio.h>void printMessage ( void ) ;int main ( ){printMessage ( ) ; return 0 ;}void printMessage ( void ){printf (“A message for you:\n\n”) ;printf (“Have a nice day!\n”) ;}Examining printMessage#include <stdio.h>void printMessage ( void ) ; function prototypeint main ( ){printMessage ( ) ; function call return 0 ;}void printMessage ( void ) function header{printf (“A message for you:\n\n”) ; functionprintf (“Have a nice day!\n”) ; body} function definitionThe Function PrototypeInforms the compiler that there will be a function defined later that:returns this typehas this nametakes these argumentsvoid printMessage (void) ;Needed because the function call is made before the definition -- the compiler uses it to see if the call is made properlyThe Function CallPasses program control to the functionMust match the prototype in name, number of arguments, and types of argumentsvoid printMessage (void) ;int main ( ) same name no arguments{ printMessage ( ) ; return 0 ;}The Function DefinitionControl is passed to the function by the function call. The statements within the function body will then 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( ) . Note that the calling function does not have to be main( ) .General Function Definition Syntaxtype functionName ( parameter1, . . . , parametern ){variable declaration(s)statement(s)}If there are no parameters, eitherfunctionName( ) OR functionName(void)is acceptable.There may be no variable declarations.If the function type (return type) is void, a return statement is not required, but the following are permitted: return ; OR return( ) ;Using Input Parametersvoid printMessage (int counter) ;int main ( ){int num;printf (“Enter an integer: “) ;scanf (“%d”, &num) ;printMessage (num) ; one argument matches the one formal parameter return 0 ; of type int of type int}void printMessage (int counter){int i ;for ( i = 0; i < counter; i++ ){ printf (“Have a nice day!\n”) ;}}Final “Clean” C Code#include <stdio.h>void printMessage (int counter) ;int main ( ){int num ; /* number of times to print message */printf (“Enter an integer: “) ;scanf (“%d”, &num) ;printMessage (num) ; return 0 ;}Final “Clean” C Code (cont)./*************************************************************************** printMessage - prints a message a specified number of times** Inputs: counter - the number of times the message will be** printed** Outputs: None/*************************************************************************/void printMessage ( int counter ){ int i ; /* loop counter */ for ( i = 0; i < counter; i++ ) { printf (“Have a nice day!\n”) ; }}Good Programming PracticeNotice the function header comment before the definition of function printMessage.This is a good practice and is required by the 104 C Coding Standards.Your header comments should be neatly formatted and contain the following information:function namefunction description (what it does)a list of any input parameters and their meaningsa list of any output parameters and their meaningsa description of any special


View Full Document

UMBC CMSC 104 - Functions, Part 1 of 3

Download Functions, Part 1 of 3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions, Part 1 of 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions, Part 1 of 3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?