Chapter 4 Functions By C.K. Liang

Size: px
Start display at page:

Download "Chapter 4 Functions By C.K. Liang"

Transcription

1 1 Chapter 4 Functions By C.K. Liang

2 What you should learn? 2 To construct programs modularly from small pieces called functions Math functions in C standard library Create new functions Pass information between functions Simulation by random number genetation Write and use functions that call themselves

3 Introduction 3 Program design technique: Building blocks (modules)

4 Function format 4 Syntax: Data_type function_name(argument_list) Statements Return value; Notices: A function may have no arguments. The return statement will be missed if the function returns no values. The void datatype must be added when there is no returned value.

5 Function examples 5 Example 1: int max(int x, int y) if(x>y) return x; else return y; Example 2: void print_result(int x) printf( The result = %d\n,x);

6 Function prototype and definitions 6 Example: #include <stdio.h> Int main(void) int square(int); prototype int x; x = square(5); call int square(int x) definition return x*x;

7 Math library functions 7 Math library functions allow you to perform certain common mathematical calculations. Example: sqrt : square root of a number #include <math.h> printf( %7.2f, sqrt(900.0));

8 Math library functions 8

9 Math library functions 9

10 Function variables 10 All variables defined in functions are called local variables Local variables: Cannot be used outside the function int main(void) int square(int); int x; x = square(5); int square(int x) local variable return x*x;

11 Functions 11 #include <stdio.h> int max(int,int); int main(void) int x,y,z; printf( Maximum = %d\n, max(x,max(y,z))); int max(int a,int b) if(a>b) return a; else return b;

12 Argument passing 12 How to pass the arguments Called by value A copy of argument s value is made and passed to function Changes to the copy do not change the original value Called by address (reference) The memory address of argument s value is passed to function Changes to the argument s value means to change the original value How to return the value back to the calling function Called by address

13 Arguments passing 13 #include <stdio.h> void swap(int,int); int main(void) int x=3,y=5; swap(x, y); printf( x = %d, y = %d\n, x,y); void swap(int a,int b) int temp; temp = a; a = b; b = temp;

14 Random number generation 14 C standard library function: rand #include <stdlib.h> i = rand(); rand function generates an integer between 0 and RAND_MAX (32767)

15 Random number generation 15 To get different random sequences Provide a seed value Like a random starting point in the sequence The same seed will give the same sequence srand(seed) <cstdlib> Used before rand() to set the seed

16 Random number generation 16 #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) int i; srand(time(null)); time return seconds since for(i=1;i<=10;i++) printf( %d, (rand()%6) + 1);

17 Recursion 17 A function calls itself int factorial(int); int main(void) printf( 5! = %d\n, factorial(5)); int factorial(int n) definition if(n <=1 ) return 1 else return n * factorial(n 1); call itself

18 Recursion 18 Recursively calling of itself

19 Recursion example 19 Example: Greatest common divisor (GCD) GCD(M,N) = M if N = 0; = GCD(N,M) if M<N; = GCD(N, M % N) GCD(36,24) = GCD(24,12) = GCD(12,0) = 12

20 Recursion example 20 Example: Fibonacci series : 0,1,1,2,3,5,8,13,21, fib(n) = 0, if n = 0; = 1, if n = 1; = fib(n-1) + fib(n-2), if n>1 fib(3) = fib(2) + fib(1) = fib(1)+fib(0) + 1 = = 2

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved. Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create

More information

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa Functions Autumn Semester 2009 Programming and Data Structure 1 Courtsey: University of Pittsburgh-CSD-Khalifa Introduction Function A self-contained program segment that carries out some specific, well-defined

More information

Chapter 5 C Functions

Chapter 5 C Functions Chapter 5 C Functions Objectives of this chapter: To construct programs from small pieces called functions. Common math functions in math.h the C Standard Library. sin( ), cos( ), tan( ), atan( ), sqrt(

More information

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from INTRODUCTION TO C++ FUNCTIONS Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Functions: Program modules in C Function Definitions Function

More information

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

More information

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Lecture 9 Functions Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To understand how to construct programs modularly

More information

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

6-1 (Function). (Function) !*+!#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x (Function) -1.1 Math Library Function!"#! $%&!'(#) preprocessor directive #include !*+!"#!, Function Description Example sqrt(x) square root of x sqrt(900.0) is 30.0 sqrt(9.0) is 3.0 exp(x) log(x)

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 310 Advanced Data Structures and Algorithms Recursion June 27, 2017 Tong Wang UMass Boston CS 310 June 27, 2017 1 / 20 Recursion Recursion means defining something, such as a function, in terms of itself

More information

1.7 Recursion. Department of CSE

1.7 Recursion. Department of CSE 1.7 Recursion 1 Department of CSE Objectives To learn the concept and usage of Recursion in C Examples of Recursion in C 2 Department of CSE What is recursion? Sometimes, the best way to solve a problem

More information

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 1 Functions Functions are everywhere in C Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Introduction Function A self-contained program segment that carries

More information

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch Lecture 3 CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions Review Conditions: if( ) / else switch Loops: for( ) do...while( ) while( )... 1 Examples Display the first 10

More information

CSE123. Program Design and Modular Programming Functions 1-1

CSE123. Program Design and Modular Programming Functions 1-1 CSE123 Program Design and Modular Programming Functions 1-1 5.1 Introduction A function in C is a small sub-program performs a particular task, supports the concept of modular programming design techniques.

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan. Functions Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 5.1 Introduction 5.3 Math Library Functions 5.4 Functions 5.5

More information

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction Functions Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur Programming and Data Structure 1 Function Introduction A self-contained program segment that

More information

Programming for Engineers Functions

Programming for Engineers Functions Programming for Engineers Functions ICEN 200 Spring 2018 Prof. Dola Saha 1 Introduction Real world problems are larger, more complex Top down approach Modularize divide and control Easier to track smaller

More information

Lecture 9 - C Functions

Lecture 9 - C Functions ECET 264 C Programming Language with Applications Lecture 9 C Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 9- Prof. Paul I. Lin

More information

Functions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay

Functions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Functions 60-141 Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Motivation A complex program Approximate Ellipse Demo Ellipse2DDouble.java

More information

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter What you will learn from Lab 7 In this laboratory, you will understand how to use typical function prototype with

More information

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion 2. Recursion Algorithm Two Approaches to Algorithms (1) Iteration It exploits while-loop, for-loop, repeat-until etc. Classical, conventional, and general approach (2) Recursion Self-function call It exploits

More information

FUNCTIONS. Without return With return Without return With return. Example: function with arguments and with return value

FUNCTIONS. Without return With return Without return With return. Example: function with arguments and with return value FUNCTIONS Definition: A is a set of instructions under a name that carries out a specific task, assigned to it. CLASSIFICATION of s: 1. User defined s (UDF) 2. Library s USER DEFINED FUNCTIONS Without

More information

Lab Instructor : Jean Lai

Lab Instructor : Jean Lai Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.

More information

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference

More information

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4 BIL 104E Introduction to Scientific and Engineering Computing Lecture 4 Introduction Divide and Conquer Construct a program from smaller pieces or components These smaller pieces are called modules Functions

More information

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module

More information

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Functions CS10001: Programming & Data Structures Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Recursion A process by which a function calls itself

More information

Computer Programming 6th Week Functions (Function definition, function calls),

Computer Programming 6th Week Functions (Function definition, function calls), Computer Programming 6th Week Functions (Function definition, function calls), Hazırlayan Asst. Prof. Dr. Tansu Filik Computer Programming Previously on Bil-200 loops (do-while, for), Arrays, array operations,

More information

Chapter 15: Recursion

Chapter 15: Recursion Chapter 15: Recursion Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 15 discusses the following main topics: Introduction to Recursion

More information

Functions. Systems Programming Concepts

Functions. Systems Programming Concepts Functions Systems Programming Concepts Functions Simple Function Example Function Prototype and Declaration Math Library Functions Function Definition Header Files Random Number Generator Call by Value

More information

Unit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions.

Unit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions. Unit III Functions Functions: Function Definition, Function prototype, types of User Defined Functions, Function calling mechanisms, Built-in string handling and character handling functions, recursion,

More information

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Programming & Data Structure Laboratory. Day 2, July 24, 2014 Programming & Data Structure Laboratory Day 2, July 24, 2014 Loops Pre and post test loops for while do-while switch-case Pre-test loop and post-test loop Condition checking True Loop Body False Loop Body

More information

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK Assoc. Prof. Dr. Tansu FİLİK Computer Programming Previously on Bil 200 Midterm Exam - 1 Midterm Exam - 1 126 students Curve: 49,78 Computer Programming Arrays Arrays List of variables: [ ] Computer Programming

More information

LAB 7 FUNCTION PART 2

LAB 7 FUNCTION PART 2 LAB 7 FUNCTION PART 2 School of Computer and Communication Engineering Universiti Malaysia Perlis 1 OBJECTIVES 1. To differentiate the file scope and block scope. 2. To write recursive function. 3. To

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number

More information

A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions.

A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions. FUNCTIONS IN C A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions. Big problems require big programs too big to be written

More information

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming PDS Lab Section 16 Autumn-2018 Tutorial 5 Functions The C language is termed as function-oriented programming Every C program consists of one or more functions. The concept is based on the divide-and conquer

More information

C: How to Program. Week /Apr/16

C: How to Program. Week /Apr/16 C: How to Program Week 8 2006/Apr/16 1 Storage class specifiers 5.11 Storage Classes Storage duration how long an object exists in memory Scope where object can be referenced in program Linkage specifies

More information

C Functions Pearson Education, Inc. All rights reserved.

C Functions Pearson Education, Inc. All rights reserved. 1 5 C Functions 2 Form ever follows function. Louis Henri Sullivan E pluribus unum. (One composed of many.) Virgil O! call back yesterday, bid time return. William Shakespeare Call me Ishmael. Herman Melville

More information

EK131 E5 Introduction to Engineering

EK131 E5 Introduction to Engineering EK131 E5 Introduction to Engineering Lecture 5: Conditional, Functions, Recursions Prof. Michel A. Kinsy Conditional execution Conditional constructs provide the ability to control whether a statement

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Class Teacher: Pralay Mitra Department of Computer Science and Engineering Indian Institute of Technology Kharagpur An Example: Random

More information

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt ECE 2574: Data Structures and Algorithms - Recursion Part I C. L. Wyatt Today we will introduce the notion of recursion, look at some examples, and see how to implement them in code. Introduction to recursion

More information

C Programming Language

C Programming Language C Programming Language Arrays & Pointers I Dr. Manar Mohaisen Office: F208 Email: manar.subhi@kut.ac.kr Department of EECE Review of Precedent Class Explain How to Create Simple Functions Department of

More information

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion? CH. 5 RECURSION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OVERVIEW Recursion is an algorithmic

More information

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C. 1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific task. The functions which are created by programmer are

More information

ME 172. C Programming Language Sessional Lecture 8

ME 172. C Programming Language Sessional Lecture 8 ME 172 C Programming Language Sessional Lecture 8 Functions Functions are passages of code that have been given a name. C functions can be classified into two categories Library functions User-defined

More information

ECET 264 C Programming Language with Applications

ECET 264 C Programming Language with Applications ECET 264 C Programming Language with Applications Lecture 10 C Standard Library Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 10

More information

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Recursion What is recursion? Week 10 Generally, when something contains a reference to itself Gaddis:19.1-19.5 CS 5301 Spring 2014 Jill Seaman 1 Math: defining a function in terms of itself Computer science:

More information

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 34. Recursion Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Introduction Example: Factorials Example: Fibonacci Numbers Recursion vs. Iteration References Introduction Introduction Recursion

More information

Computer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers

Computer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers Computer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers Hazırlayan Asst. Prof. Dr. Tansu Filik Introduction to Programming Languages Previously on Bil-200 Functions: Function

More information

Question Bank (SPA SEM II)

Question Bank (SPA SEM II) Question Bank (SPA SEM II) 1. Storage classes in C (Refer notes Page No 52) 2. Difference between function declaration and function definition (This question is solved in the note book). But solution is

More information

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion Copyright 2003 Scott/Jones Publishing Topics 19.1 Introduction to Recursion 19.2 The Recursive Factorial Function 19.3 The Recursive

More information

FUNCTIONS OMPAL SINGH

FUNCTIONS OMPAL SINGH FUNCTIONS 1 INTRODUCTION C enables its programmers to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others. Every function

More information

Chapter 7 Functions. Now consider a more advanced example:

Chapter 7 Functions. Now consider a more advanced example: Chapter 7 Functions 7.1 Chapter Overview Functions are logical groupings of code, a series of steps, that are given a name. Functions are especially useful when these series of steps will need to be done

More information

Language comparison. C has pointers. Java has references. C++ has pointers and references

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.

More information

Calling Prewritten Functions in C

Calling Prewritten Functions in C Calling Prewritten Functions in C We've already called two prewritten functions that are found in the C library stdio.h: printf, scanf. The function specifications for these two are complicated (they allow

More information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design What is a Function? C Programming Lecture 8-1 : Function (Basic) A small program(subroutine) that performs a particular task Input : parameter / argument Perform what? : function body Output t : return

More information

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lesson 5: Functions and Libraries 1 Functions 2 Overview Function is a block of statements which performs a specific task, and can be called by others Each function has a name (not identical to any other),

More information

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries Hazırlayan Asst. Prof. Dr. Tansu Filik Computer Programming Previously on Bil 200 Low-Level I/O getchar, putchar,

More information

Lecture 10: Recursive Functions. Computer System and programming in C 1

Lecture 10: Recursive Functions. Computer System and programming in C 1 Lecture 10: Recursive Functions Computer System and programming in C 1 Outline Introducing Recursive Functions Format of recursive Functions Tracing Recursive Functions Examples Tracing using Recursive

More information

Functions in C C Programming and Software Tools

Functions in C C Programming and Software Tools Functions in C C Programming and Software Tools N.C. State Department of Computer Science Functions in C Functions are also called subroutines or procedures One part of a program calls (or invokes the

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 6 Modular programming Department of Computer Engineering Outline Introduction to pointer

More information

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis: Recursion What is recursion? Week 10! Generally, when something contains a reference to itself Gaddis:19.1-19.5! Math: defining a function in terms of itself CS 5301 Spring 2015 Jill Seaman 1! Computer

More information

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function. Recursion! A Recursive function is a functions that calls itself.! Recursive functions can be useful in solving problems that can be broken down into smaller or simpler subproblems of the same type.! A

More information

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014 Programming & Data Structure Laboratory rrays, pointers and recursion Day 5, ugust 5, 2014 Pointers and Multidimensional rray Function and Recursion Counting function calls in Fibonacci #include

More information

Functions. Introduction :

Functions. Introduction : Functions Introduction : To develop a large program effectively, it is divided into smaller pieces or modules called as functions. A function is defined by one or more statements to perform a task. In

More information

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science Functions in C C Programming and Software Tools N.C. State Department of Computer Science Functions in C Functions are also called subroutines or procedures One part of a program calls (or invokes the

More information

CSE202-Lec#4. CSE202 C++ Programming

CSE202-Lec#4. CSE202 C++ Programming CSE202-Lec#4 Functions and input/output streams @LPU CSE202 C++ Programming Outline Creating User Defined Functions Functions With Default Arguments Inline Functions @LPU CSE202 C++ Programming What is

More information

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop: Matlab? Lecture Slides ME 4060 Machine Vision and Vision-based Control Chapter 3-4 Matlab and IPT Basics By Dr. Debao Zhou 1 MATric LABoratory data analysis, prototype and visualization Matrix operation

More information

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

More information

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06 Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Practice Sheet #06 Topic: Recursion in C 1. What string does the following program print? #include #include

More information

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion School of Electrical and Computer Engineering Cornell University revision: 2018-09-13-21-07 1 Dictionary Analogy 2 2 Computing Factorial

More information

Programming & Data Structure

Programming & Data Structure Functions Programming & Data Structure CS 11002 Partha Bhowmick http://cse.iitkgp.ac.in/ pb CSE Department IIT Kharagpur Spring 2012-2013 Functions Callee : Caller = Bowler : Captain Functions int f(){...

More information

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2013 C++ Programming Language Lab # 6 Functions C++ Programming Language Lab # 6 Functions Objective: To be familiar with

More information

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$617$

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$617$ BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$617$ Instructors:AykutErdem,ErkutErdem,FuatAkal TAs:YasinSahin,AhmetSelmanBozkir,GultekinIsik, OguzhanGuclu Today# Func/ons# Defini@ons Invoca@on

More information

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

CSCI 2132 Software Development. Lecture 17: Functions and Recursion CSCI 2132 Software Development Lecture 17: Functions and Recursion Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 15-Oct-2018 (17) CSCI 2132 1 Previous Lecture Example: binary

More information

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point? CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point? Overview For this lab, you will use: one or more of the conditional statements explained below scanf()

More information

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

C Programming for Engineers Functions

C Programming for Engineers Functions C Programming for Engineers Functions ICEN 360 Spring 2017 Prof. Dola Saha 1 Introduction Real world problems are larger, more complex Top down approach Modularize divide and control Easier to track smaller

More information

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to. Computer Programming and Utilization (CPU) 110003 G) Functions 1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific

More information

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 6-7

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 6-7 BBM 101 Introduc/on to Programming I Fall 2013, Lecture 6-7 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Yasin Sahin, Ahmet Selman Bozkir, Gultekin Isik, Oguzhan Guclu 1 Today Func/ons Defini@ons

More information

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1 RECURSION Chapter 5 Recursive Thinking Section 5.1 1 Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that are difficult

More information

CS 101: Computer Programming and Utilization

CS 101: Computer Programming and Utilization CS 101: Computer Programming and Utilization Jul - Nov 2016 Bernard Menezes (cs101@cse.iitb.ac.in) Lecture 13: Recursive Functions About These Slides Based on Chapter 10 of the book An Introduction to

More information

Koch snowflake. Fractal Fern

Koch snowflake. Fractal Fern CSC 111: Recursive Methods Fractals: Self Similar Shapes http://en.wikipedia.org/wiki/fractal Koch snowflake Fractal Fern Functions: Example Problem Factorial of a number: 0! = 1 Factorial(N)= 1! = 1 Product

More information

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh Remember! Practice will make you perfect!!! :D Learning C Language For BEGINNERS The 6 th week / May 24 th, 26 Su-Jin Oh sujinohkor@gmail.com 1 Index Basics Operator Precedence Table and ASCII Code Table

More information

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? Recursion Chapter 8 CS 3358 Summer I 2012 Jill Seaman What is recursion? Generally, when something contains a reference to itself Math: defining a function in terms of itself Computer science: when a function

More information

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis: Recursion What is recursion? Week 10 Gaddis:19.1-19.5 CS 5301 Spring 2017 Jill Seaman 1 l Generally, when something contains a reference to itself l Math: defining a function in terms of itself l Computer

More information

Assignment #3 Answers

Assignment #3 Answers Assignment #3 Answers Introductory C Programming UW Experimental College Assignment #3 ANSWERS Question 1. How many elements does the array int a[5] contain? Which is the first element? The last? The array

More information

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis:

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis: Recursion What is recursion? Week 7! Generally, when something contains a reference to itself Gaddis:19.1-19.4! Math: defining a function in terms of itself CS 5301 Fall 2013 Jill Seaman 1! Computer science:

More information

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Teacher: Sourangshu Bha@acharya sourangshu@gmail.com h@p://cse.iitkgp.ac.in/~sourangshu/ Department of Computer Science and Engineering

More information

Module 05: Types of recursion

Module 05: Types of recursion Module 05: Types of recursion Topics: Review of purely structural recursion Accumulative recursion Generative recursion Readings:ThinkP 5.8-5.10, 6.5-6.7 1 Review: Structural Recursion Template for code

More information

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions CSE 2421: Systems I Low-Level Programming and Computer Organization Functions Read/Study: Reek Chapters 7 Gojko Babić 01-22-2018 Predefined Functions C comes with libraries of predefined functions E.g.:

More information

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers Functions 1 Function n A program segment that carries out some specific, well-defined task n Example A function to add two numbers A function to find the largest of n numbers n A function will carry out

More information

C: How to Program. Week /Apr/23

C: How to Program. Week /Apr/23 C: How to Program Week 9 2007/Apr/23 1 Review of Chapters 1~5 Chapter 1: Basic Concepts on Computer and Programming Chapter 2: printf and scanf (Relational Operators) keywords Chapter 3: if (if else )

More information

C Programming Lecture V

C Programming Lecture V C Programming Lecture V Instructor Özgür ZEYDAN http://cevre.beun.edu.tr/ Modular Programming A function in C is a small sub-program that performs a particular task, and supports the concept of modular

More information

Pointers and scanf() Steven R. Bagley

Pointers and scanf() Steven R. Bagley Pointers and scanf() Steven R. Bagley Recap Programs are a series of statements Defined in functions Can call functions to alter program flow if statement can determine whether code gets run Loops can

More information

Structured programming

Structured programming Exercises 8 Version 1.0, 1 December, 2016 Table of Contents 1. Recursion................................................................... 1 1.1. Problem 1...............................................................

More information

SUHAIL T A

SUHAIL T A SUHAIL T A Functions A number of statements grouped into a single logical unit C pgm is a collection of functions Self contained pgm segment that carries out a specific task main is a special recognized

More information