Functions, Part 2 of 2 Topics 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

Modular Programming With Functions
Computer Programming w/ Eng. Applications
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Fungsi Risanuri Hidayat, Ir., M.Sc.. Functions C usually consist of two things: instance variables and functions. All C programs consist of one or more.
Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.
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.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
Functions Why we use functions C library functions Creating our own functions.
CMSC 1041 Functions II Functions that return a value.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
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;
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.
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.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
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
‘C’ Programming Structures and Commands
Functions Course conducted by: Md.Raihan ul Masood
Chapter 6: User-Defined Functions I
Mathematical Functions
Computer Science 210 Computer Organization
INC 161 , CPE 100 Computer Programming
presented BY : DURGESH KKHANDEKAR 1st semester
C Functions Pepper.
TMF1414 Introduction to Programming
ECE Application Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
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
Chapter 5 - Functions Outline 5.1 Introduction
Final Exam Final Exam: Thursday Dec 13th, 2001 at 8:30 pm in SS-111, the regular classroom.
Chapter 6 - Functions Outline 5.1 Introduction
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions, Part 1 of 3 Topics Using Predefined Functions
بنام خدا زبان برنامه نویسی C (21814( Lecture 4 Chapter 5
C Characters and Strings – Review Lab assignments
Functions, Part 2 of 3 Topics Functions That Return a Value
Introduction to C Topics Compilation Using the gcc Compiler
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
A First Book of ANSI C Fourth Edition
Introduction to C Topics Compilation Using the gcc Compiler
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to Problem Solving and Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
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.
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

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

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 <stdio.h> 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. (I.e.: The variables that are passed to the function are matched with the formal parameters in the order they are passed)

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.

Parameter Passing and Local Variables #include <stdio.h> 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

Same Name, Still Different Memory Locations #include <stdio.h> 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 num1 num2 average 5 8 int int float int int float

Changes to Local Variables Do NOT Change Other Variables with the Same Name #include <stdio.h> 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: <stdio.h> standard input/output library functions and information used by them <math.h> math library functions <stdlib.h> conversion of numbers to text, text to numbers, memory allocation, random numbers, and other utility functions <time.h> manipulating the time and date <ctype.h> functions that test characters for certain properties and that can convert case <string.h> functions that manipulate character strings others see Chapter 5 of text

Using Header Files #include <stdio.h> #include <stdlib.h> #include <math.h> 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 ;

Math Library double sqrt (double x); returns the square root of x double pow (double x, double y) x raised to the y power pow (3.0, 2.0) is 9.0 pow (8.0, 1.0 / 3) is 2.0 double sin (double x) trigonometric sine of x (x in radians) All math library functions take doubles as arguments and return doubles

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

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

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

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

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

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 short int %hd %hd char %c %c