Chapter 4 Functions By C.K. Liang

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

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

Chapter 5 C Functions

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

Lecture 04 FUNCTIONS AND ARRAYS

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

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

CS 310 Advanced Data Structures and Algorithms

1.7 Recursion. Department of CSE

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

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

CSE123. Program Design and Modular Programming Functions 1-1

C Functions. 5.2 Program Modules in C

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

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

Programming for Engineers Functions

Lecture 9 - C Functions

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

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

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

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

Lab Instructor : Jean Lai

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

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

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

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

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

Chapter 15: Recursion

Functions. Systems Programming Concepts

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

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

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

LAB 7 FUNCTION PART 2

Chapter 3 - Functions

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

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

C: How to Program. Week /Apr/16

C Functions Pearson Education, Inc. All rights reserved.

EK131 E5 Introduction to Engineering

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

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

C Programming Language

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

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

ME 172. C Programming Language Sessional Lecture 8

ECET 264 C Programming Language with Applications

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

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

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

Question Bank (SPA SEM II)

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

FUNCTIONS OMPAL SINGH

Chapter 7 Functions. Now consider a more advanced example:

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Calling Prewritten Functions in C

Functions. Arash Rafiey. September 26, 2017

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

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

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

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

Functions in C C Programming and Software Tools

Fundamental of Programming (C)

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

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

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

Functions. Introduction :

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

CSE202-Lec#4. CSE202 C++ Programming

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

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

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

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

Programming & Data Structure

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

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

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

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

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

C Programming for Engineers Functions

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

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

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

CS 101: Computer Programming and Utilization

Koch snowflake. Fractal Fern

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

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

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

Assignment #3 Answers

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

Lecture 04 FUNCTIONS AND ARRAYS

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

Module 05: Types of recursion

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

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

C: How to Program. Week /Apr/23

C Programming Lecture V

Pointers and scanf() Steven R. Bagley

Structured programming

SUHAIL T A

Transcription:

1 Chapter 4 Functions By C.K. Liang

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

Introduction 3 Program design technique: Building blocks (modules)

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.

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);

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;

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));

Math library functions 8

Math library functions 9

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;

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;

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

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;

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)

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

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 1970.1.1 for(i=1;i<=10;i++) printf( %d, (rand()%6) + 1);

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

Recursion 18 Recursively calling of itself

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

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 = 1 + 0 + 1 = 2