Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Functions Part 1-Fundamentals of Computer-Lecture Slides, Slides of Computer Fundamentals

The course covers important and advance elements of C and C plus plus programming language. This course provides the student with the skills required to design, code, and test and execute programs of simple to intermediate complexity. It includes: Modules, Math, Library, Functions, Prototypes, Header, Files, Calling, Random, Number, Generation

Typology: Slides

2011/2012

Uploaded on 07/31/2012

karthik
karthik 🇮🇳

4.6

(16)

95 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
.
Chapter 5 - Functions
Outline
5.1 Introduction
5.2 Program Modules in C
5.3 Math Library Functions
5.4 Functions
5.5 Function Definitions
5.6 Function Prototypes
5.7 Header Files
5.8 Calling Functions: Call by Value and Call by Reference
5.9 Random Number Generation
5.10 Example: A Game of Chance
5.11 Storage Classes
5.12 Scope Rules
5.13 Recursion
5.14 Example Using Recursion: The Fibonacci Series
5.15 Recursion vs. Iteration
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Functions Part 1-Fundamentals of Computer-Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!

.

Chapter 5 - Functions

Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions 5.5 Function Definitions 5.6 Function Prototypes 5.7 Header Files 5.8 Calling Functions: Call by Value and Call by Reference 5.9 Random Number Generation 5.10 Example: A Game of Chance 5.11 Storage Classes 5.12 Scope Rules 5.13 Recursion 5.14 Example Using Recursion: The Fibonacci Series 5.15 Recursion vs. Iteration

.

Objectives

  • In this chapter, you will learn:
    • To understand how to construct programs modularly

from small pieces called functions..

  • To introduce the common math functions available in

the C standard library.

  • To be able to create new functions.
  • To understand the mechanisms used to pass information

between functions.

  • To introduce simulation techniques using random

number generation.

  • To understand how to write and use functions that call

themselves.

.

5.2 Program Modules in C

  • Functions
    • Modules in C
    • Programs combine user-defined functions with library functions
      • C standard library has a wide variety of functions
  • Function calls
    • Invoking functions
      • Provide function name and arguments (data)
      • Function performs operations or manipulations
      • Function returns results
  • Function call analogy:
  • Boss asks worker to complete task
  • Worker gets information, does task, returns result

.

5.2 Program Modules in C

Fig. 5.1 Hierarchical boss function/worker function relationship.

main

Worker 1 Worker 2^ Worker 3

Worker 4 Worker 5

.

5.3 Math Library Functions

.

5.4 Functions

  • Functions
    • Modularize a program
    • All variables defined inside functions are local variables
      • Known only in function defined
    • Parameters
      • Communicate information between functions
      • Local variables
  • Benefits of functions
    • Divide and conquer
      • Manageable program development
    • Software reusability
      • Use existing functions as building blocks for new programs
      • Abstraction - hide internal details (library functions)
    • Avoid code repetition

.

5.5 Function Definitions

  • Function definition format (continued)

return-value-type function-name( parameter-list ) { declarations and statements }

  • Definitions and statements: function body (block)
    • Variables can be defined inside blocks (can be nested)
    • Functions can not be defined inside other functions
  • Returning control
    • If nothing returned
      • return;
      • or, until reaches right brace
    • If something returned
      • return expression ;

Outline

.

fig05_03.c (Part 1 of 2)

Outline

.

fig05_04.c (Part 1 of 2)

Outline

.

fig05_04.c (Part 2 of 2)

Program Output

Enter three integers: 22 85 17 Maximum is: 85 Enter three integers: 85 22 17 Maximum is: 85 Enter three integers: 22 17 85 Maximum is: 85