CSE 251 Dr. Charles B. Owen Programming in C1 Functions.

Slides:



Advertisements
Similar presentations
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
Advertisements

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.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C-1 University of Washington Computer Programming I Lecture 3: Variables, Values, and Types © 2000 UW CSE.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
ICS103 Programming in C Lecture 9: Functions I
CS 201 Functions Debzani Deb.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Recursion CMPE231, Spring 2012 Aleaxander G. Chefranov 1.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
CPS120: Introduction to Computer Science Functions.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
JavaScript III Functions and Abstraction. 2 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
1 ICS103 Programming in C Lecture 8: Functions I.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Functions What is a function –“sub” program, with an isolated set of statements –Accepts parameters, returns a result (perhaps) –Think of it as its own.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
UNIT 17 Recursion: Towers of Hanoi.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Programming Module 3 Functions Python Programming, 2/e1.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Functions Course conducted by: Md.Raihan ul Masood
C Functions -Continue…-.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
The Hanoi Tower Problem
Lesson #6 Modular Programming and Functions.
In C Programming Language
CSC 143 Recursion.
Introduction to Problem Solving and Programming
CPS125.
Presentation transcript:

CSE 251 Dr. Charles B. Owen Programming in C1 Functions

CSE 251 Dr. Charles B. Owen Programming in C2 Moon Landings

CSE 251 Dr. Charles B. Owen Programming in C3 Triangle Area Computation p1=(x1,y1) p2=(x2,y2) p3=(x3,y3) a c b How would you write this program?

CSE 251 Dr. Charles B. Owen Programming in C4 Variables int main() { double x1=0, y1=0; double x2=17, y2=10.3; double x3=-5.2, y3=5.1; double a, b, c; /* Triangle side lengths */ double p; /* For Heron's formula */ double area;

CSE 251 Dr. Charles B. Owen Programming in C5 Lengths of Edges a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); c = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)); p1=(x1,y1) p2=(x2,y2) p3=(x3,y3) a c b

CSE 251 Dr. Charles B. Owen Programming in C6 Area p = (a + b + c) / 2; area = sqrt(p * (p - a) * (p - b) * (p - c)); printf("%f\n", area);

CSE 251 Dr. Charles B. Owen Programming in C7 Whole Program int main() { double x1=0, y1=0; double x2=17, y2=10.3; double x3=-5.2, y3=5.1; double a, b, c; /* Triangle side lengths */ double p; /* For Heron's formula */ double area; a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); c = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)); p = (a + b + c) / 2; area = sqrt(p * (p - a) * (p - b) * (p - c)); printf("%f\n", area); } What if I made a mistake on the edge length equation?

CSE 251 Dr. Charles B. Owen Programming in C8 Functions Functions are subprograms that perform some operation and return one value They “encapsulate” some particular operation, so it can be re-used by others (for example, the abs() or sqrt() function)

CSE 251 Dr. Charles B. Owen Programming in C9 Characteristics Reusable code – code in sqrt() is reused often Encapsulated code – implementation of sqrt() is hidden Can be stored in libraries – sqrt() is a built-in function found in the math library

CSE 251 Dr. Charles B. Owen Programming in C10 Writing Your Own Functions Consider a function that converts temperatures in Celsius to temperatures in Fahrenheit. – Mathematical Formula: F = C * – We want to write a C function called CtoF

CSE 251 Dr. Charles B. Owen Programming in C11 Convert Function in C double CtoF ( double paramCel ) { return paramCel* ; } This function takes an input parameter called paramCel (temp in degree Celsius) and returns a value that corresponds to the temp in degree Fahrenheit

CSE 251 Dr. Charles B. Owen Programming in C12 How to use a function? #include double CtoF( double ); /************************************************************************ * Purpose: to convert temperature from Celsius to Fahrenheit ************************************************************************/ int main() { double c, f; printf(“Enter the degree (in Celsius): “); scanf(“%lf”, &c); f = CtoF(c); printf(“Temperature (in Fahrenheit) is %lf\n”, f); } double CtoF ( double paramCel) { return paramCel * ; }

CSE 251 Dr. Charles B. Owen Programming in C13 Terminology Declaration: double CtoF( double ); Invocation (Call): Fahr = CtoF(Cel); Definition: double CtoF( double paramCel ) { return paramCel* ; }

CSE 251 Dr. Charles B. Owen Programming in C14 Function Declaration Also called function prototype: Declarations describe the function: – the return type and function name – the type and number of parameters return_type function_name (parameter_list) double CtoF(double)

CSE 251 Dr. Charles B. Owen Programming in C15 Function Definition return_type function_name (parameter_list) { …. function body …. } double CtoF(double paramCel) { return paramCel* ; }

CSE 251 Dr. Charles B. Owen Programming in C16 Function Invocation double CtoF ( double paramCel ) { return paramCel* ; } int main() { … f = CtoF(c); } 1. Call copies argument c to parameter paramCel 2. Control transfers to function “CtoF”

CSE 251 Dr. Charles B. Owen Programming in C17 double CtoF ( double paramCel ) { return paramCel* ; } int main() { … f = CtoF(c); } 3. Expression in “CtoF” is evaluated 4. Value of expression is returned to “main” Invocation (cont)

CSE 251 Dr. Charles B. Owen Programming in C18 Local Objects The parameter “paramCel” is a local object which is defined only while the function is executing. Any attempt to use “paramCel” outside the function is an error. The name of the parameter need not be the same as the name of the argument. Types must agree.

CSE 251 Dr. Charles B. Owen Programming in C19 Can we do better than this? int main() { double x1=0, y1=0; double x2=17, y2=10.3; double x3=-5.2, y3=5.1; double a, b, c; /* Triangle side lengths */ double p; /* For Heron's formula */ double area; a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); c = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)); p = (a + b + c) / 2; area = sqrt(p * (p - a) * (p - b) * (p - c)); printf("%f\n", area); }

CSE 251 Dr. Charles B. Owen Programming in C20 What should we name our function? a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); “Length” sounds like a good idea. ??? Length( ??? ) { }

CSE 251 Dr. Charles B. Owen Programming in C21 What does our function need to know? a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); (x, y) for two different points: ??? Length(double x1, double y1, double x2, double y2) { }

CSE 251 Dr. Charles B. Owen Programming in C22 What does our function return? double Length(double x1, double y1, double x2, double y2) { } a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); A computed value which is of type double

CSE 251 Dr. Charles B. Owen Programming in C23 How does it compute it? double Length(double x1, double y1, double x2, double y2) { double len; len = sqrt(( x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); return(len); } a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); A computed value which is of type double

CSE 251 Dr. Charles B. Owen Programming in C24 Using This #include /* Declaration */ double Length(double x1, double y1, double x2, double y2); /* * Program to determine the area of a triangle */ int main() { double x1=0, y1=0; double x2=17, y2=10.3; double x3=-5.2, y3=5.1; double a, b, c; /* Triangle side lengths */ double p; /* For Heron's formula */ double area; a = Length(x1, y1, x2, y2); b = Length(x1, y1, x3, y3); c = Length(x2, y2, x3, y3); p = (a + b + c) / 2; area = sqrt(p * (p - a) * (p - b) * (p - c)); printf("%f\n", area); } /* Definition */ double Length(double x1, double y1, double x2, double y2) { double len; len = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); return(len); } Invocations Declaration Definition 1

CSE 251 Dr. Charles B. Owen Programming in C25 Potential Errors #include double convert( double ); int main() { double c, f; printf(“Enter the degree (in Celsius): “); scanf(“%lf”, &c); f= convert(c); printf(“Temp (in Fahrenheit) for %lf Celsius is %lf”, paramCel, f); } double CtoF( double paramCel) { return c * ; } Error! paramCel is not defined Error! C is not defined Scope – Where a variable is known to exist. No variable is known outside of the curly braces that contain it, even if the same name is used!

CSE 251 Dr. Charles B. Owen Programming in C26 Another Example #include double GetTemperature(); double CelsiusToFahrenheit( double ); void DisplayResult( double, double ); int main() { double TempC, // Temperature in degrees Celsius TempF; // Temperature in degrees Fahrenheit TempC = GetTemperature(); TempF = CelsiusToFahrenheit(TempC); DisplayResult(TempC, TempF); return 0; } Declarations Invocations

CSE 251 Dr. Charles B. Owen Programming in C27 Function: GetTemperature double GetTemperature() { double Temp; printf("\nPlease enter a temperature in degrees Celsius: "); scanf("%lf", &Temp); return Temp; }

CSE 251 Dr. Charles B. Owen Programming in C28 Function: CelsiusToFahrenheit double CelsiusToFahrenheit(double Temp) { return (Temp * ); }

CSE 251 Dr. Charles B. Owen Programming in C29 Function: DisplayResult void DisplayResult(double CTemp, double FTemp) { printf("Original: %5.2f C\n", CTemp); printf("Equivalent: %5.2f F\n", FTemp); return; }

CSE 251 Dr. Charles B. Owen Programming in C30 Declarations (Prototypes) double GetTemp( ); double CelsiusToFahrenheit( double ); void Display( double, double ); void means “nothing”. If a function doesn’t return a value, its return type is void

CSE 251 Dr. Charles B. Owen Programming in C31 Abstraction int main() { double TempC, // Temperature in degrees Celsius TempF; // Temperature in degrees Fahrenheit TempC = GetTemperature(); TempF = CelsiusToFahrenheit(TempC); DisplayResult(TempC, TempF); return 0; } 1.Get Temperature 2.Convert Temperature 3.Display Temperature We are hiding details on how something is done in the function implementation.

CSE 251 Dr. Charles B. Owen Programming in C32 Recursion: Computing Factorial Pseudocode for factorial(n) if n == 0 then result = 1 else result = n * factorial(n – 1) After all, 5! = 5 * 4 * 3 * 2 * 1 = 5 * 4!

CSE 251 Dr. Charles B. Owen Programming in C33 Factorial function contains an invocation of itself. We call this a: recursive call. Recursive functions must have a base case (if n == 0): why? int Factorial(int n) { if(n == 0) return 1; else return n * Factorial(n-1); } Recursive Functions This works much like proof by induction. if n == 0 then result = 1 else result = n * factorial(n – 1)

CSE 251 Dr. Charles B. Owen Programming in C34 Infinite Recursion int Factorial(int n) { return n * Factorial(n-1); } What if I omit the “base case”? This leads to infinite recursion! Factorial(3)= 3 * Factorial(2) = 3 * 2 * Factorial(1) = 3 * 2 * 1 * Factorial(0) = 3 * 2 * 1 * 0 * Factorial(-1) = … Input n: 5 Input k: 3 Segmentation fault

CSE 251 Dr. Charles B. Owen Programming in C35 Psuedocode and Function int Factorial(int n) { if(n == 0) return 1; else return n * Factorial(n-1); } if n == 0 then result = 1 else result = n * factorial(n – 1) Base Case 2 Declaration: int Factorial(int n); Invocation: f = Factorial(7); Definition: int Factorial(int n) { if(n == 0) return 1; else return n * Factorial(n-1); }

CSE 251 Dr. Charles B. Owen Programming in C36 Towers of Hanoi Problem Let’s consider general case of n disks. Suppose that we had a solution for n-1 disks and could state solution for n disks in terms of the solution for n-1 disks. Then the problem would be solved. This is true because in the trivial case of one disk (continually subtracting 1 from n will eventually produce 1), the solution is simple: merely move the single disk from peg A to peg C. 36

CSE 251 Dr. Charles B. Owen Programming in C37 Towers of Hanoi Problem 37

CSE 251 Dr. Charles B. Owen Programming in C38 Towers of Hanoi Problem Move n disks from A to C using B as auxiliary 1.If n==1, move the single disk from A to C and stop 2.Move the top n-1 disks from A to B, using C as auxiliary 3.Move the remaining disk from A to C 4.Move the n-1 disks from B to C, using A as auxiliary 38

CSE 251 Dr. Charles B. Owen Programming in C39 Solution #include int main() { towers(20, 'A', 'B', 'C'); return 0; }

CSE 251 Dr. Charles B. Owen Programming in C40 Solution void towers(int n, char fromPeg, char toPeg, char auxPeg) { /* If only 1 disk, make the move and return */ if (n==1) { printf("Move disk 1 from peg %c to peg %c \n", fromPeg, toPeg); return; } /* Move top n-1 disks from fromPeg to auxPeg */ towers(n-1, fromPeg, auxPeg, toPeg); /* Move remaining disk from fromPeg to toPeg */ printf("Move disk %d from peg %c to peg %c \n", n, fromPeg, toPeg); /* Move n-1 disks from auxPeg to toPeg */ towers(n-1, auxPeg, toPeg, fromPeg); }

CSE 251 Dr. Charles B. Owen Programming in C41 Sample output Move disk 1 from peg a to peg b Move disk 2 from peg a to peg c Move disk 1 from peg b to peg c Move disk 3 from peg a to peg b Move disk 1 from peg c to peg a Move disk 2 from peg c to peg b Move disk 1 from peg a to peg b Move disk 4 from peg a to peg c Move disk 1 from peg b to peg c Move disk 2 from peg b to peg a Move disk 1 from peg c to peg a Move disk 3 from peg b to peg c Move disk 1 from peg a to peg b Move disk 2 from peg a to peg c Move disk 1 from peg b to peg c 41