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

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6: User-Defined Functions I
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.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
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.
CPS120: Introduction to Computer Science Functions.
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?
Functions Are supposed to be fun…. Parameter=Door The word parameter is to function as door is to house. A parameter is used to get data into a function.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
User defined functions
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
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 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.
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.
Subprograms Functions Procedures.
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Functions Review.
Chapter 7 Text Input/Output Objectives
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 10: Void Functions
FIGURE 4-10 Function Return Statements
Functions, locals, parameters, and separate compilation
FIGURE 9-5 Integer Constants and Variables
Chapter 9 Pointers Objectives
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
FIGURE 4-10 Function Return Statements
Local Variables variables which are declared within a
Topics discussed in this section:
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
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.
Chapter 4 Functions Objectives
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:
FIGURE 4-10 Function Return Statements
Topics discussed in this section:
Chapter 6: User-Defined Functions I
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.
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to C++ Programming Language
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.
Rudra Dutta CSC Spring 2007, Section 001
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

FIGURE 4-10 Function Return Statements Computer Science: A Structured Programming Approach Using C

FIGURE 4-11 Function Local Variables Computer Science: A Structured Programming Approach Using C

Formal and Actual Parameters Note Formal and Actual Parameters Formal parameters are variables that are declared in the header of the function definition. Actual parameters are the expressions in the calling statement. Formal and actual parameters must match exactly in type, order, and number. Their names, however, do not need to match. Computer Science: A Structured Programming Approach Using C

FIGURE 4-12 Parts of a Function Call Computer Science: A Structured Programming Approach Using C