Functions and Recursion

Slides:



Advertisements
Similar presentations
MS-Excel XP Lesson 5. Exponentiation 1.A1  2 A2  3 A3  =A1^A2 B1  =2^4 2.^ for exponentiation.
Advertisements

Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Number Theory.  A prime number is a natural number greater than 1 that has exactly two factors (or divisors), itself and 1.  Prime numbers less than.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Prime numbers Jordi Cortadella Department of Computer Science.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 13 Thanks for lecture slides: Prentice Hall, Inc., 2. C++
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
C allows programmer to define their own function according to their requirement. These types of functions are known as user-defined functions. Suppose,
Parameter passing Jordi Cortadella Department of Computer Science.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Recursion.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
- Standard C Statements
Variables A piece of memory set aside to store data
CMPT 201 Functions.
Specifications What? Not how!.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Using variables, for..loop and functions to help organize your code
Unit 2 Programming.
By Hector M Lugo-Cordero September 17, 2008
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
By Hector M Lugo-Cordero September 17, 2008
Search,Sort,Recursion.
Conditionals.
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
By Hector M Lugo-Cordero September 3, 2008
CS149D Elements of Computer Science
The structure of programming
CS148 Introduction to Programming II
Dale Roberts, Lecturer IUPUI
Presentation transcript:

Functions and Recursion By Hector M Lugo-Cordero September 10, 2008 Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Outline Functions Parameters Return Value Recursion Base Case Process Stack The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Functions Functions A function is a piece of code that performs specific tasks Allows code reuse Syntax <return_type> <name_of_function>(<params>){ //code goes here [return statement] } Functions can be declared before or after main The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Recursion Recursion Recursion is the act of a function calling itself The computer can easily run out of memory Thus the base case is then introduced Indicates when the function will stop calling itself Each function run has a different process stack The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Examples Write a function that simulates Windows system(“pause”) command for a Linux computer. void pause(){ … } Write a function that compares two integers int compare(int x, int y){ … } This funcion returns 1 if x is greater than y 0 if x is equal to y -1 if x is less than y The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Examples (cont.) Write a function that checks if a number is prime or not bool isPrime(int num){ … } The function checks if num is 2 if so returns true If not then it verifies if the number is even in which case returns false if it is Finally if none of these conditions are met then it starts dividing num by 3, 5, 7, … until it finds that any of these divides the number, in which case returns false The function returns true if the divisor is greater than the square root of the number The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Questions? ? The Department of Electrical and Computer Engineering