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.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

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.
Recursion. Binary search example postponed to end of lecture.
 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.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
 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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
© 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 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CPS120: Introduction to Computer Science Functions.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
1 MT258 Computer Programming and Problem Solving Unit 4.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
CSCI 171 Presentation 6 Functions and Variable Scope.
Solving Complex Problems. Review A subroutine is a set of instructions to perform a particular computation –used to solve subproblems of more complex.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
CGS 3460 Function – I n What is a function lOne named program module that does one primary task lConsists of a set of statements n Why we need functions?
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CGS 3460 Functions – Definition n One named program module that does one primary task n It consists of a set of statements that have been grouped together.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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.
Program Development and Design Using C++, Third Edition
Recursion Function calling itself
Function – I What is a function Why we need functions?
Chapter Topics Chapter 16 discusses the following main topics:
Functions Course conducted by: Md.Raihan ul Masood
User-Written Functions
Chapter 15 Recursion.
C Functions -Continue…-.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Chapter 15 Recursion.
Functions and an Introduction to Recursion
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
Unit 3 Test: Friday.
Function.
CS149D Elements of Computer Science
Function.
Presentation transcript:

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 little machine which is created and operated whenever it is requested Why we need functions? –Compartmentalize a long program into manageable parts: Better organization of code, encapsulation Use same code repeatedly, rather than repeating the same code –Each function acts as a building block

Functions How to declare a function return_type function_name(parameter_list) { Declarations; Statements; } Parameter-list format type1 variable_name1, type2 variable_name2,… typeN variable_nameN

Example Function Declaration Example: int main() { …;} double calcMax(double a,double b) {…;} Special cases –Use void for return_type if no value is to be returned. –Use void for parameter_list if no parameters are to be passed to the function

Using Functions How to return a value in a function –return; // for void return type –return ; // to return the value of // to the caller –In both cases, the next instruction executed is the instruction following the invocation of the function in the calling program/function How to call (invoke) a function –function_name(…); // as its own statement Or –function_name(…) may appear in an expression, where its value will be the value returned.

Example: Area Calculation double calcArea(double height, double width) { return height * width; } int main() { double h, w, area; printf(“Please input height and width\n”); scanf(“%f, %f”, &h, &w); area = calcArea(h, w); printf(“The area is %f”, area); return 0; }

Example – I Compute the square root of x Pseudo-code (Babylonian Method) 1.Set the value of guess to 1 2.If |guess 2 - x| <, proceed to step 4 3.Set a new value of guess to (guess + x/guess)/2 and repeat step 2 4.Return guess as the result

Example I – code float fabs(float x) {// this is really a if (x < 0.0) return -x;// standard library return x;// function in } float squareRoot(float x) { const float epsilon = ; // desired precision float guess = 1.0; while ( fabs(guess*guess - x) >= epsilon) { guess = (guess + x/guess) / 2.0; } return guess; }

Example – II Compute factorials double factorial(int n) { double result = 1; int i; for( i = 2; i <= n; i++) { result = result * i; } return result ; }

Example: factorials 5! = 5 * 4 * 3 * 2 * 1 –Notice that 5! = 5 * 4! 4! = 4 * 3!... –Can define factorials recursively –Solve base case (1! = 0! = 1) then plug in 2! = 2 * 1! = 2 * 1 = 2; 3! = 3 * 2! = 3 * 2 = 6; factorial(5) = 5 * factorial(4);

Example: factorials factorial(n) = n * factorial(n-1); double factorial(int n) { if ( n <= 1 ) return 1; // base case return n * factorial(n – 1); } Does this code work?

Local Variables – I What are local variables (also called automatic variables) –Variables declared within a function –Function parameters are also “local” Example: double calcMax(double a[10]) { int i; double maxValue; …; } int main() { int i; double a[10] double maxValue; maxValue = CalcMax(a) ; } Local variables

Local Variables – II Why use local variables? –To store temporary information –Values are private to current function –Can't be accessed by other functions Parameters are local variables! Every invocation of a function creates its own private set of local variables!

Recursive Function Call double factorial(int n) { if (n <= 1) return 1; return n*factorial(n–1); } double factorial(int n) { if (n <= 1) return 1; return n*factorial(n–1); } double factorial(int n) { if (n <= 1) return 1; return n*factorial(n–1); } double factorial(int n) { if (n <= 1) return 1; return n*factorial(n–1); } n=4n=3 n=2 n=1 return (4 * factorial(4 – 1));factorial( 3)); return (3 * factorial(3 – 1)); factorial( 2)); return (2 * factorial(2 – 1));factorial( 1)); return 1; 1); 2); 6);

Recursion Recursive functions –Functions that invoke themselves –Use when the solution can be defined in terms of itself –Three requirements for successful use of recursion: There must be an identifiable base case –Such as: factorial(1) == 1 There must be a recursive case –Such as: factorial(n) == n * factorial(n-1) There must be progress towards the base case –Such as: factorial(n) depends on factorial(n-1), with n-1 closer to the base case of 1, than n.

Example Implement function power(base, exponent) which returns base exponent –Recursive definition: base exponent = base * base exponent-1 –Base case: base 1 = base;

Code double power(double base, int exponent) { if (exponent == 1) return base; else return base * power(base, exponent-1); }

Possible Improvement double power(double base, int exponent) { if (exponent == 0) return 1; else if (exponent == 1) return base; else if (exponent % 2 != 0) return(base * power(base, exponent-1)); else { double temp; temp = power(base,exponent/2) return temp * temp; }

Fibonacci series Fibonacci series: 0, 1, 1, 2, 3, 5, 8... –Each number is the sum of the previous two –Can be defined and solved recursively: fib( n ) = fib( n - 1 ) + fib( n - 2 ) –Code for the fibonacci function int fib( int n ) { if (n == 0 || n == 1) // base case return n; else return(fib(n - 1) + fib(n - 2)); }

Arrays as parameters Pass entire array to a function –Example: int minimum(int values[ ], int numberOfElements) Only the location of array is passed to the function –Not the value of all elements in the array –Without a separate parameter, the function has no way of knowing the size of the array

Pass by Value Passing a copy of the value as an argument –Parameters receive a distinct copy of the caller's arguments, as if the parameters were assigned the values of the actual arguments –Changes made to the parameters have no effect on the caller’s arguments (because they are copies) Examples: h = 3; w = 4; area = calcArea(h, w); double CalcArea(double height, double width) { …; } height = 3, width = 4

Except Arrays Arrays are NOT passed by value! Arrays are passed by “reference” The location of the array is passed to a function –Code within the function may “refer” to the elements of the array (with an index) –Changes made to array elements change the original array!

Calculate the minimum value In a recursive way: –Base case: If only one number is in the array, the number is the minumum –Recursive Calculate the minimum value for the first half of the array Calculate the minimum value for the second half of the array smaller value of these two values is the minimum min

Code int minimum(int values[], int first, int last) { int middle; int v1, v2; if (first == last) return values[first]; else { middle = (first + last) / 2; v1 = minimum(values, first, middle); v2 = minimum(values, middle+1, last); if (v1 < v2) return v1; else return v2; }

Iterative Solution int minimum(int values[], int first, int last) { int i; int min; min = values[first]; for (i = first + 1; i <= last; i++) { if (values[i] < min) min = values[i]; } return min; }

Global Variables What is a global variable –A variable defined outside of any function –A global variables can be accessed by any function in a program Why use a global variable –avoid passing frequently-used variables continuously among many functions, How to define a global variable –Same as other variable declaration, except it is declared outside a function

Example int x; /* Global variable */ int y = 10; /* Initialized global variable */ int foo(int z) { int w; /* local variable */ x = 42; /* assign to a global variable */ w = 10; /* assign to a local variable */ return (x % y + z / w); }

Global Variables are Discouraged! Global variables are generally regarded as poor programming practice Why? –Destroys encapsulation and structure of programs –Harder to debug –Harder to reliably enhance –Someone looking at code which uses a global variable cannot be sure of all the other places the variable may be used or modified, requiring them to understand all the code in order to understand any part of it -- recipe for problems!