Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.

Similar presentations


Presentation on theme: "Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers."— Presentation transcript:

1

2 Functions

3 Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers. Function that sorts a list of names.

4 Boss Analogy One function can delegate certain jobs to specific functions. Boss Function Max Function Sort Function

5 Important Concept #1 Divide and Conquer: break large programs into a series of smaller functions. –Helps manage complexity. –Makes it easier to build large programs. –Makes is easier to debug programs.

6 Important Concept #2 Abstraction: most of the time, you need to know what a function does, but not how it actually does it. –Also helps manage complexity. –You can use other people’s code, without understanding how it works.

7 Using Pre-Packaged Functions Standard C includes lots of pre-packaged libraries: –ctype.h: character manipulation –math.h: mathematical functions –stdio.h: standard input/output –stdlib.h: random numbers, memory handling –string.h: string manipulation –time.h: date/time functions All of these are detailed in the Book.

8 Function Concepts Function Prototype Function Definition Function Call

9 Function Prototype Tells you what type of data the function is expecting, and what type of data the function returns. –Represents a communication protocol that enables two functions to “talk.” –If you want to use a pre-built function, you need to learn to read prototypes.

10 sqrt Function double sqrt (double); This function therefore accepts one double value, and returns one double value. Return data type Function argument data type

11 Using sqrt() #include main () { double value; value = sqrt(9.0);/*function call*/ printf ("%f", value); }

12 Creating your own Functions

13 #include int square(int); main () { printf("%d ", square(5)); } int square(int y) { return y * y; } Function Prototype: must be declared at top of program. Function Definition: contains the actual function code. Returns a value back up to main. Function Call: invokes the function

14 Understanding the Program Main Function Square function “Take this Integer!” “Here’s the answer. It’s an integer!”

15 Function Template Use this template for adding functions to your program. return-value-type function-name (parameter-list); return-value-type function-name (parameter-list) { variable declarations; statements of work; return statement (optional outside of this class); } Function Prototype: must be declared at top of program. Function Definition: contains the actual function code.


Download ppt "Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers."

Similar presentations


Ads by Google