CMSC 1041 Functions II Functions that return a value.

Slides:



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

Chapter Five Functions
Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
An Introduction to Programming with C++ Fifth Edition
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Review for midterm exam Dilshad M. Shahid Spring NYU.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 6: User-Defined Functions I
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
1 Lecture 3 Part 1 Functions with math and randomness.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
© 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.
Functions Why we use functions C library functions Creating our own functions.
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?
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
© 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.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
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.
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.
Final Review1 Final Exam l Final Exam: Thursday December 15, 2005 at 8:30 pm in room MP-008, the regular classroom. If you can not make it to the exam.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Chapter 6: User-Defined Functions I
TMF1414 Introduction to Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 5 - Functions Outline 5.1 Introduction
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Chapter 6 - Functions Outline 5.1 Introduction
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
A First Book of ANSI C Fourth Edition
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

CMSC 1041 Functions II Functions that return a value

CMSC 1042 A function that returns a value /*************************************************** ** AverageTwo takes two integers arguments ** and returns their average as a float ****************************************************/ float AverageTwo (int num1, int num2) { float average; average = (num1 + num2) / 2.0; return average; }

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

CMSC 1044 Local Variables l Functions only “see” their own local variable. This includes main ( ) l The variables that are passed to the function are matched with the formal parameters in the order they are passed l The parameters are declarations of local variables. The values passed are assigned to those variables l Other local variables can be declared within the function

CMSC 1045 #include float AverageTwo (int num1, num2) float AverageTwo (int num1, int num2); { 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); } num1 num2 average 5 8 int int float int int float Local Variables

CMSC 1046 More about Local Variables #include void AddOne (int num1); void AddOne (int num1) { main ()num1++; {printf (“In AddOne: “); int num1 = 5;printf (“num1 = %d,num1); AddOne (num1); } printf (“In main: “); printf (“num1 = %d\n”, num1);num1 } num1 int 5 OUTPUT int In AddOne: num1 = 6 In main: num1 = 5

CMSC 1047 Data Types and Conversion Specifiers Data Type printf scanf conversion conversion float%f%f double%f%lf long double%Lf%Lf int%d%d long int%ld%ld unsigned int%u%u unsigned long int%lu%lu shortint%hd%hd char%c%c

CMSC 1048 Header Files l Header files contain function prototypes for all of the functions found in the corresponding library l It also contains definitions of constants and data types used in that library

CMSC 1049 Commonly Used Header Files header fileContains function prototypes for the standard input/output library functions & information used by them the math library functions the conversion of number to text, text to number, memory allocation, random numbers and other utility functions manipulating time and date functions that test characters for certain properties and that can convert case otherssee page 159 of text

CMSC Math Library l double sqrt (double x); oreturns the square root of x l double pow (double x, double y) ox raised to the y power l pow (3.0, 2.0) is 9.0 l pow (8.0, 0.33) is 2.0 l double sin (double x) otrigonometric sine of x (x in radians) l All math library functions take doubles as arguments and return doubles l others on page 151 of text

CMSC Common stdlib functions l void exit (int x); oprematurely ends program execution l void srand (unsigned int x); o“seeds” the random number generator with an unsigned integer that is used to start the calculations that generate the pseudo-random number l srand (200); l int rand (void); oreturns an unsigned pseudo-random integer in the range of 0 to or 0 to depending on the size of an integer on the system your on onum = rand();

CMSC Manipulating what rand() returns l Since rand() returns unsigned integers in a large range, we often have to manipulate the return value to suit our purposes l Suppose we want only random numbers in the range from 0 to 5 onum = rand () % 6 l How about 1 to 6? onum = 1 + rand( ) % 6; l How about 5 to 20? onum = 5 + rand ( ) % 16;

CMSC srand ( ) and rand ( ) l The pseudo-random number generator needs an unsigned int as it’s seed l Although it produces what appear to be random numbers, if we use the same seed, we get the same sequence of random numbers l To get different random numbers each time we run our program, we have to give a different seed each time

CMSC srand ( ) and rand ( ) #include Since we are always include using the value 67 #define SEED 67generator, the same numbers will be main ( )produced whenever we run our {program int i, num; srand (SEED); for (i = 0; i < 5; i++) { num = rand ( ); num = 1 + num % 6; printf (“%d\n”, num); }

CMSC l One of the most useful functions in the time library is the time() function l It returns the time of day as seconds l Since this number is different every time we call it, it’s greatest use is as a seed for the random number generator l Each time we run our program, a different sequence of random numbers will be produced l srand (time ( NULL) );