CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Guide To UNIX Using Linux Third Edition
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Programming With C.
Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions.
CMSC 1041 Functions II Functions that return a value.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
1 Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 6: User-Defined Functions I
Functions, Part 2 of 2 Topics Functions That Return a Value
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
User Defined Functions
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
UMBC CMSC 104 – Section 01, Fall 2016
Assignment Operators Topics Increment and Decrement Operators
A First Book of ANSI C Fourth Edition
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Functions that return a value
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments Header Files Reading Sections 3.1 – 3.3

CMSC 104, Version 8/062L18Functions1.ppt Review of Structured Programming Structured programming is a problem solving strategy and a programming methodology that includes the following guidelines: o The program uses only the sequence, selection, and repetition control structures. o The flow of control in the program should be as simple as possible. o The construction of a program embodies top- down design.

CMSC 104, Version 8/063L18Functions1.ppt 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.

CMSC 104, Version 8/064L18Functions1.ppt 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). o Program control passes to the function. o The function is executed. o Control is passed back to the calling function.

CMSC 104, Version 8/065L18Functions1.ppt #include 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 function Sample Function Call

CMSC 104, Version 8/066L18Functions1.ppt Functions (con’t) We have used three predefined functions so far: o printf o scanf o getchar There are 3723 predefined functions on the gl system. Don’t worry, nobody knows them all!

CMSC 104, Version 8/067L18Functions1.ppt Predefined Functions You can use the man pages for help with using the predefined functions. Some of the functions are also Linux commands or system calls, so to get the information on how to use them in your program, use: man 3 abs You will also need to use the man page to find out what you have to #include. There are over 900 libraries, so don’t expect to learn them all! The man page will also tell you what the arguments and return type are.

CMSC 104, Version 8/068L18Functions1.ppt Man Page Example ABS(3) Linux Programmer's Manual ABS(3) NAME abs, labs, llabs, imaxabs - compute the absolute value of an integer. SYNOPSIS #include ======================================================= To use the library function abs( ), you must include the stdlib.h file.

CMSC 104, Version 8/069L18Functions1.ppt Man Page Example (cont’d) In the case of the abs function, you will see: int abs(int j); This means that you must give it one integer argument and it will return an integer value.

CMSC 104, Version 8/0610L18Functions1.ppt Programmer-Defined Functions 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. All non-trivial programs use functions. You define all the parts of a programmer- defined function, the name, the behavior, the parameters (or arguments) and the return type.

CMSC 104, Version 8/0611L18Functions1.ppt Sample Programmer-Defined Function #include void printMessage ( void ) ; int main ( void ) { printMessage ( ) ; return 0 ; } void printMessage ( void ) { printf (“A message for you:\n\n”) ; printf (“Have a nice day!\n”) ; }

CMSC 104, Version 8/0612L18Functions1.ppt Examining printMessage #include void printMessage ( void ) ; function prototype int main ( void ) { printMessage ( ) ; function call return 0 ; } void printMessage ( void )function header { printf (“A message for you:\n\n”) ; function printf (“Have a nice day!\n”) ; body } function definition

CMSC 104, Version 8/0613L18Functions1.ppt The Function Prototype Informs the compiler that there will be 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

CMSC 104, Version 8/0614L18Functions1.ppt The Function Call Passes program control to the function Must match the prototype in name, number of arguments, and types of arguments void printMessage (void) ; int main ( void ) same name no arguments { printMessage ( ) ; return 0 ; }

CMSC 104, Version 8/0615L18Functions1.ppt 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( ).

CMSC 104, Version 8/0616L18Functions1.ppt General Function Definition Syntax type functionName ( parameter 1,..., parameter n ) { variable declaration(s) statement(s) } If there are no parameters, either functionName( ) 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( ) ;

CMSC 104, Version 8/0617L18Functions1.ppt Possibilities A function may: o Have no arguments and return nothing. o Have arguments and return nothing. o Have arguments and return one value. o Have no arguments and return one value. A function can never return more than one value!

CMSC 104, Version 8/0618L18Functions1.ppt Parameter List A function can have more than one parameter: int functionA( int a, int b, float c ) When the function is invoked, the parameters can be a variable, constant, or expression: result = functionA( 7, 3 + a, dollars); The value 7 is put into to local variable a, 3 + a is put into local variable b, and a copy of the value in dollars is put into c in this example.

CMSC 104, Version 8/0619L18Functions1.ppt Using Parameters void printMessage (int counter) ; int main ( void ) { 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”) ; }

CMSC 104, Version 8/0620L18Functions1.ppt Using Parameters (cont’d) In this example, we do not know in advance what the user will enter for the value of num, however, it is copied into the variable counter in the function printMessage( ). Both the variables counter and i are considered to be local variable, because they are only visible inside (or local to) the function printMessage( ).

CMSC 104, Version 8/0621L18Functions1.ppt Final “Clean” C Code #include void printMessage (int counter) ; int main ( void ) { int num ; /* number of times to print message */ printf (“Enter an integer: “) ; scanf (“%d”, &num) ; printMessage (num) ; return 0 ; }

CMSC 104, Version 8/0622L18Functions1.ppt Final “Clean” C Code (con’t) /************************************************************************* ** 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”) ; }

CMSC 104, Version 8/0623L18Functions1.ppt 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: o function name o function description (what it does) o a list of any input parameters and their meanings o a list of any output parameters and their meanings o a description of any special conditions

CMSC 104, Version 8/0624L18Functions1.ppt Header Files Header files contain function prototypes for all of the functions found in the specified library. They also contain definitions of constants and data types used in that library.

CMSC 104, Version 8/0625L18Functions1.ppt Commonly Used Header Files Header File Contains Function Prototypes for: standard input/output library functions and information used by them math library functions conversion of numbers to text, text to numbers, memory allocation, random numbers, and other utility functions manipulating the time and date functions that test characters for certain properties and that can convert case functions that manipulate character strings others see Chapter 5 of text

CMSC 104, Version 8/0626L18Functions1.ppt Using Header Files #include int main ( ) { float side1, side2, hypotenuse ; printf(“Enter the lengths of the right triangle sides: “) ; scanf(“%f%f”, &side1, &side2) ; if ( (side1 <= 0) || (side2 <= 0) { exit (1) ; } hypotenuse = sqrt ( (side1 * side1) + (side2 * side2) ) ; printf(“The hypotenuse = %f\n”, hypotenuse) ; return 0 ; }