Docsity

Functions in Programming: Definition, Return Values, and Examples - Prof. Nelson Padua-Per, Study notes of Computer Science

Partial preview of the text

Download Functions in Programming: Definition, Return Values, and Examples - Prof. Nelson Padua-Per and more Study notes Computer Science in PDF only on Docsity!

ANNOUNCEMENTS

 Exam next Monday

 No posting of code in the forum.

 You must implement programming projects

by yourself.

INTRODUCTION TO FUNCTIONS

 General form of a function is: function name (<comma-separated list of parameters>) { statements }  Functions are invoked by using the () operator.  A function can receive values via parameters.  Some functions may not return a value.  Some functions may not take any parameters.  There are other approaches to define functions.

FUNCTIONS

 Let’s define a function that prints a restaurant menu (no parameters).  Let’s define a function where the restaurant name is a parameter.  Let’s define a function that prints the maximum of two values.  Let’s define a function that returns the maximum.  Let’s reduce the code of the maximum function.  What if we don’t return a value?  Let’s add a function that reads the values to compute the maximum.  What if we want to compute the maximum of more than two values?  Let’s use these examples to practice our testing, debugging techniques.