Topics discussed in this section:

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

Topics discussed in this section:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
Introduction to Methods
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Python quick start guide
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Computer Science: A Structured Programming Approach Using C1 4-6 Scope Scope determines the region of the program in which a defined object is visible.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Computer Science: A Structured Programming Approach Using C1 4-5 Standard Functions C provides a rich collection of standard functions whose definitions.
SubPrograms Top-Down Design using stepwise refinement leads to division of tasks.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Introduction to Programming
Functions and an Introduction to Recursion
Operating Systems (CS 340 D)
Functions Review.
Iterative Constructs Review
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
C Short Overview Lembit Jürimägi.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
FIGURE 9-5 Integer Constants and Variables
Chapter 8 Arrays Objectives
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Chapter 4 Functions Objectives
Topics discussed in this section:
Chapter 8 Arrays Objectives
Functions October 23, 2017.
Topics discussed in this section:
Topics discussed in this section:
Iterative Constructs Review
Assignment Operators Topics Increment and Decrement Operators
Topics discussed in this section:
Pointers.
Topics discussed in this section:
Assignment Operators Topics Increment and Decrement Operators
Chapter 9: Value-Returning Functions
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Topics discussed in this section:
Chapter 8 Arrays Objectives
CS149D Elements of Computer Science
Introduction to C++ Programming Language
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Topics discussed in this section:
6 Functions.
Assignment Operators Topics Increment and Decrement Operators
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Topics discussed in this section: 4-4 Inter-Function Communication Although the calling and called functions are two separate entities, they need to communicate to exchange data. The data flow between the calling and called functions can be divided into three strategies: a downward flow, an upward flow, and a bi-directional flow. Topics discussed in this section: Basic Concept C Implementation Computer Science: A Structured Programming Approach Using C

a calling and a called function. Note The C language uses only call by value and return to achieve different types of communications between a calling and a called function. Call by value: value of parameter is passed, so changes made to parameter in function are not reflected in the calling function. Computer Science: A Structured Programming Approach Using C

FIGURE 4-17 Downward Communication in C Computer Science: A Structured Programming Approach Using C

FIGURE 4-18 Downward Communication Computer Science: A Structured Programming Approach Using C

FIGURE 4-19 Upward Communication in C Computer Science: A Structured Programming Approach Using C

FIGURE 4-20 Upward Communication Computer Science: A Structured Programming Approach Using C

To send data from the called function to the calling function: Note To send data from the called function to the calling function: 1. We need to use the & symbol in front of the data variable when we call the function. 2. We need to use the * symbol after the data type when we declare the address variable 3. We need to use the * in front of the variable when we store data indirectly Computer Science: A Structured Programming Approach Using C

FIGURE 4-21 Bi-directional Communication in C Computer Science: A Structured Programming Approach Using C

1 FIGURE 4-22 Bi-directional Communication Computer Science: A Structured Programming Approach Using C

FIGURE 4-23 Exchange Function Computer Science: A Structured Programming Approach Using C

FIGURE 4-24 Calculate Quotient and Remainder Computer Science: A Structured Programming Approach Using C

Quotient and Remainder PROGRAM 4-8 Quotient and Remainder Computer Science: A Structured Programming Approach Using C

Quotient and Remainder PROGRAM 4-8 Quotient and Remainder Computer Science: A Structured Programming Approach Using C

Quotient and Remainder PROGRAM 4-8 Quotient and Remainder Computer Science: A Structured Programming Approach Using C

Quotient and Remainder PROGRAM 4-8 Quotient and Remainder Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 4-5 Standard Functions C provides a rich collection of standard functions whose definitions have been written and are ready to be used in our programs. To use these functions, we must include their function declarations. Topics discussed in this section: Math Functions Random Numbers Computer Science: A Structured Programming Approach Using C

FIGURE 4-26 Library Functions and the Linker Computer Science: A Structured Programming Approach Using C

Some functions #include<stdlib Some functions #include<stdlib.h> abs() int = abs(int) rand() srand() Others: malloc, qsort, etc. Computer Science: A Structured Programming Approach Using C

Math functions #include <math. h> trig: cos(), etc Math functions #include <math.h> trig: cos(), etc. pow(base, exp); log, exp, etc. sqrt() ceil(), floor(), trunc(), round(), etc. Computer Science: A Structured Programming Approach Using C

FIGURE 4-27 Ceiling Function Computer Science: A Structured Programming Approach Using C

FIGURE 4-28 Floor Function Computer Science: A Structured Programming Approach Using C

FIGURE 4-29 Random Number Generation Computer Science: A Structured Programming Approach Using C

FIGURE 4-30 Generating a Random Number Series Computer Science: A Structured Programming Approach Using C

srand must be called only once for each random number series. Note srand must be called only once for each random number series. Computer Science: A Structured Programming Approach Using C

Creating Temporal Random Numbers PROGRAM 4-9 Creating Temporal Random Numbers Computer Science: A Structured Programming Approach Using C

Creating Temporal Random Numbers PROGRAM 4-9 Creating Temporal Random Numbers Computer Science: A Structured Programming Approach Using C

Creating Pseudorandom Numbers PROGRAM 4-10 Creating Pseudorandom Numbers Computer Science: A Structured Programming Approach Using C

Creating Pseudorandom Numbers PROGRAM 4-10 Creating Pseudorandom Numbers Computer Science: A Structured Programming Approach Using C

FIGURE 4-31 Random Number Scaling for 3–7 Computer Science: A Structured Programming Approach Using C

Generating Random Numbers in the Range 10 to 20 PROGRAM 4-11 Generating Random Numbers in the Range 10 to 20 Computer Science: A Structured Programming Approach Using C

Generating Random Numbers in the Range 10 to 20 PROGRAM 4-11 Generating Random Numbers in the Range 10 to 20 Computer Science: A Structured Programming Approach Using C

Generating Random Real Numbers PROGRAM 4-12 Generating Random Real Numbers Computer Science: A Structured Programming Approach Using C

Generating Random Real Numbers PROGRAM 4-12 Generating Random Real Numbers Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 4-6 Scope Scope determines the region of the program in which a defined object is visible. Scope pertains to any object that can be declared, such as a variable or a function declaration. Topics discussed in this section: Global Scope Local Scope Computer Science: A Structured Programming Approach Using C

FIGURE 4-32 Scope for Global and Block Areas Computer Science: A Structured Programming Approach Using C

Variables are in scope from declaration until the end of their block. Note Variables are in scope from declaration until the end of their block. Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 4-7 Programming Example— Incremental Development Top–down development, a concept inherent to modular programming, allows us to develop programs incrementally. By writing and debugging each function separately, we are able to solve the program in smaller steps, making the whole process easier. Topics discussed in this section: First Increment: main and getData Second Increment: add Final Increment: Print ResultsThe Computer Science: A Structured Programming Approach Using C

FIGURE 4-33 Calculator Program Design Computer Science: A Structured Programming Approach Using C

Calculator Program—First Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—First Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—First Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—Second Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—Second Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—Second Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—Second Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—Final Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—Final Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—Final Increment Computer Science: A Structured Programming Approach Using C

Calculator Program—Final Increment Computer Science: A Structured Programming Approach Using C