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.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Computer Science: A Structured Programming Approach Using C1 5-5 Incremental Development Part II In Chapter 4, we introduced the concept of incremental.
Computer Science: A Structured Programming Approach Using C1 5-5 Incremental Development Part II In Chapter 4, we introduced the concept of incremental.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
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.
Chapter 9: Value-Returning Functions
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 12 Enumerated, Structure, and Union Types Objectives
Functions Review.
Chapter 7 Text Input/Output Objectives
Selection—Making Decisions
FIGURE 4-10 Function Return Statements
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
FIGURE 9-5 Integer Constants and Variables
Topics discussed in this section:
Topics discussed in this section:
Chapter 9 Pointers Objectives
User-Defined Functions
Topics discussed in this section:
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
FIGURE 4-10 Function Return Statements
Topics discussed in this section:
User Defined Functions
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
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:
Topics discussed in this section:
Functions, Part 1 of 3 Topics Using Predefined Functions
Zhen Jiang West Chester University
Topics discussed in this section:
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Topics discussed in this section:
Statistics 300: Elementary Statistics
Topics discussed in this section:
Topics discussed in this section:
FIGURE 4-10 Function Return Statements
Topics discussed in this section:
Topics discussed in this section:
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Topics discussed in this section:
Functions, Part 1 of 3 Topics Using Predefined Functions
Topics discussed in this section:
Introduction to C++ Programming Language
Selection—Making Decisions
Topics discussed in this section:
FIGURE 4-10 Function Return Statements
Topics discussed in this section:
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Methods/Functions.
The return Statement © 2018 Kris Jordan.
Presentation transcript:

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 must be named main. In general, the purpose of a function is to receive zero or more pieces of data, operate on them, and return at most one piece of data. At the same time, a function can have a side effect. A function side effect is an action that results in a change in the state of the program. Computer Science: A Structured Programming Approach Using C

Note In C, a program is made of one or more functions, one and only one of which must be called main. The execution of the program always starts with main, but it can call other functions to do some part of the job. Computer Science: A Structured Programming Approach Using C

FIGURE 4-3 Structure Chart for a C Program Computer Science: A Structured Programming Approach Using C

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

Note A function in C can have a return value, a side effect, or both. The side effect occurs before the value is returned. The function’s value is the value in the expression of the return statement. A function can be called for its value, its side effect, or both. Computer Science: A Structured Programming Approach Using C

Sample Program with Subfunction Computer Science: A Structured Programming Approach Using C

Sample Program with Subfunction Computer Science: A Structured Programming Approach Using C

Sample Program with Subfunction Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 4-3 User-Defined Functions Like every other object in C, functions must be both declared and defined. The function declaration gives the whole picture of the function that needs to be defined later. The function definition contains the code for a function. Topics discussed in this section: Basic Function Designs Function Definition Function Declaration The Function Call Computer Science: A Structured Programming Approach Using C

void Function with a Parameter PROGRAM 4-2 void Function with a Parameter Computer Science: A Structured Programming Approach Using C

void Function with a Parameter PROGRAM 4-2 void Function with a Parameter Computer Science: A Structured Programming Approach Using C

void Function with a Parameter PROGRAM 4-2 void Function with a Parameter Computer Science: A Structured Programming Approach Using C

FIGURE 4-7 Non-void Function without Parameters Computer Science: A Structured Programming Approach Using C

FIGURE 4-8 Calling a Function That Returns a Value Computer Science: A Structured Programming Approach Using C

Read a Number and Square It PROGRAM 4-3 Read a Number and Square It Computer Science: A Structured Programming Approach Using C

Read a Number and Square It PROGRAM 4-3 Read a Number and Square It Computer Science: A Structured Programming Approach Using C

Read a Number and Square It PROGRAM 4-3 Read a Number and Square It Computer Science: A Structured Programming Approach Using C

Read a Number and Square It PROGRAM 4-3 Read a Number and Square It Computer Science: A Structured Programming Approach Using C

FIGURE 4-9 Function Definition Computer Science: A Structured Programming Approach Using C