2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Spring Semester 2013 Lecture 5
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Chapter 6: User-Defined Functions I
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Chapter 6: User-Defined Functions
CMSC 1041 Functions II Functions that return a value.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Learners Support Publications Functions in C++
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
User-Defined Functions (cont’d) - Reference Parameters.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
Programming Fundamentals Enumerations and Functions.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
TK1924 Program Design & Problem Solving Session 2011/2012
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Chapter 10: Void Functions
FIGURE 4-10 Function Return Statements
Chapter 5 Functions DDC 2133 Programming II.
CSCI 161: Introduction to Programming Function
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
6.12 Default Arguments.
6 Chapter Functions.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Chapter 6: User-Defined Functions I
CS149D Elements of Computer Science
In C Programming Language
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Functions Extra Examples.
Functions Imran Rashid CTO at ManiWeber Technologies.
A simple function.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 42 Topics Functions That Return a Value
CPS125.
Corresponds with Chapter 5
Presentation transcript:

2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang Functions Review 2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang [Based on dblock spr’06lecture, slightly modified]

Expressions An expression gives a value and can be a constant (e.g., 3) the value stored in a variable (e.g., x ) the result of an operation (e.g., 3 + 4) the return value of a function (e.g., sqrt(5) ) any combination of the above: x + sqrt( 7 * 4) - 1

Functions The name of a function can appear in 3 places: a function prototype the implementation of a function a function call Each has a different syntax!!!

Function Prototypes Function prototypes tell the compiler: the name of a function the type of the return value (if any) the types of the formal parameters (if any) double average (int n, int m) ; void print_this () ; void PrintLabel) (int year) ;

Function Prototypes Function prototypes appear in: the beginning of a .c file inside a header file (a .h file) If you use a function in a file, the function prototype for that function should be present in one of these two ways (not both). If the prototype is in a .h file, it must be included using #include.

Function Implementation The function implementation: has the actual C program for the function can appear in a separate file from where the function is called. type of the return value and parameters must match types in the function prototype. for separate compilation, include the .h file so the compiler can check type compatibility.

Function Implementation type functionName ( type param, type param, ...) { variable declarations statements }

Function Call A function call: must include actual parameters (arguments) that match the function prototype in number and type: x = average(a, b) ; actual parameters can be expressions: x = average(a + 7, 2 * b) ; does not include any type declarations!

Function Call (cont’d) A function call: has a return value that can be used in an expression: x = 2 * average(a, b) - 5 ; x = 2 * average( add5(a), b + 1 ) - 5 ;

Function Call (cont’d) A function call: has a return value that can be ignored: scanf (“%d”, &n) ; without a return value cannot be used in an expression: PrintLabel (year) ;