Functions: Part 2 of 3 2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Chapter 6: User-Defined Functions I
Review for midterm exam Dilshad M. Shahid Spring NYU.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
Chapter 6: User-Defined Functions I
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 6: User-Defined Functions
CMSC 1041 Functions II Functions that return a value.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
CSCI 171 Presentation 6 Functions and Variable Scope.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 6: User-Defined Functions I
Computer Science 210 Computer Organization
INC 161 , CPE 100 Computer Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
Programming Fundamentals Lecture #7 Functions
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Computer Science 210 Computer Organization
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
A First Book of ANSI C Fourth Edition
Functions, Part 1 of 3 Topics Using Predefined Functions
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions that return a value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1

Functions, Part 2 of 3 Topics Functions That Return a Value Parameter Passing Local Variables Header Files Reading Sections

Functions Can Return Values /**************************************************************************** ** averageTwo - calculates and returns the average of two numbers ** Inputs: num1 - an integer value ** num2 - an integer value ** Outputs: the floating point average of num1 and num2 *****************************************************************************/ float AverageTwo (int num1, int num2) { float average ; /* average of the two numbers */ average = (num1 + num2) / 2.0 ; return average ; }

Using averageTwo #include float AverageTwo (int num1, int num2) ; int main ( ) { float ave ; int value1 = 5, value2 = 8 ; ave = AverageTwo (value1, value2) ; printf (“The average of %d and %d is %f\n”, value1, value2, ave) ; return 0 ; } float AverageTwo (int num1, int num2) { float average ; average = (num1 + num2) / 2.0 ; return average ; }

Parameter Passing Actual parameters are the parameters that appear in the function call. average = AverageTwo (value1, value2) ; Formal parameters are the parameters that appear in the function header. float AverageTwo (int num1, int num2) Actual and formal parameters are matched by position. Each formal parameter receives the value of its corresponding actual parameter.

Parameter Passing (con’t) Corresponding actual and formal parameters do not have to have the same name, but they may. Corresponding actual and formal parameters must be of the same data type, with some exceptions.

Local Variables Functions only “see” (have access to) their own local variables. This includes main( ). Formal parameters are declarations of local variables. The values passed are assigned to those variables. Other local variables can be declared within the function body.

#include float AverageTwo (int num1, int num2) float AverageTwo (int num1, int num2) ; { int main ( ) float average ; { float ave ; average = (num1 + num2) / 2.0 ; int value1 = 5, value2 = 8 ; return average ; } ave = AverageTwo (value1, value2) ; printf (“The average of “) ; printf (“%d and %d is %f\n”, value1, value2, ave) ; return 0 ; } value1 value2 ave num1 num2 average 5 8 int int float int int float Parameter Passing and Local Variables

#include float AverageTwo (int num1, int num2) float AverageTwo (int num1, int num2) ; { int main ( ) float average ; { float average ; average = (num1 + num2) / 2.0 ; int num1 = 5, num2 = 8 ; return average ; } average = AverageTwo (num1, num2) ; printf (“The average of “) ; printf (“%d and %d is %f\n”, num1, num2, average) ; return 0 ; } num1 num2 average 5 8 int int float int int float Same Name, Still Different Memory Locations

Changes to Local Variables Do NOT Change Other Variables with the Same Name #include void AddOne (int number) ; void AddOne (int num1) { int main ( )num1++ ; {printf (“In AddOne: “) ; int num1 = 5 ;printf (“num1 = %d\n”, num1) ; AddOne (num1) ; } printf (“In main: “) ; printf (“num1 = %d\n”, num1) ;num1 return 0 ; } int num1 5 OUTPUT int In AddOne: num1 = 6 In main: num1 = 5

Header Files Header files contain function prototypes for all of the functions found in the specified library. They also contain definitions of constants and data types used in that library.

Commonly Used Header Files Header File Contains Function Prototypes for: standard input/output library functions and information used by them math library functions conversion of numbers to text, text to numbers, memory allocation, random numbers, and other utility functions manipulating the time and date functions that test characters for certain properties and that can convert case functions that manipulate character strings others see Chapter 5 of text

Using Header Files #include int main ( ) { float side1, side2, hypotenuse ; printf(“Enter the lengths of the right triangle sides: “) ; scanf(“%f%f”, &side1, &side2) ; if ( (side1 <= 0) || (side2 <= 0) { exit (1) ; } hypotenuse = sqrt ( (side1 * side1) + (side2 * side2) ) ; printf(“The hypotenuse = %f\n”, hypotenuse) ; return 0 ; }