1 CSC103: Introduction to Computer and Programming Lecture No 16.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Lab 8 User Defined Function.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Functions Pass by Value Pass by Reference IC 210.
1 ICS103 Programming in C Lecture 10: Functions II.
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.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
1 CSC103: Introduction to Computer and Programming Lecture No 19.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
C# Programming Methods.
C++ Programming Lecture 12 Functions – Part IV
Programming Fundamentals Enumerations and Functions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
User-Written Functions
Introduction to Programming
Chapter 2 - Introduction to C Programming
Pointers and Pointer-Based Strings
Chapter 2 - Introduction to C Programming
Functions in C Mrs. Chitra M. Gaikwad.
Chapter 5 - Functions Outline 5.1 Introduction
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Pointers Call-by-Reference CSCI 230
A function with one argument
Faculty of Computer Science & Information System
Function “Inputs and Outputs”
CISC181 Introduction to Computer Science Dr
Introduction to Problem Solving and Programming
CS149D Elements of Computer Science
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

1 CSC103: Introduction to Computer and Programming Lecture No 16

2 Previous lecture Pointer fundamental & operator * operator Pointer declaration Pointer type

3 Today’s lecture outline Function call by address or pointer Function return value Recursive functions

4 Function Calls Arguments can generally be passed to functions in one of the two ways: – sending the values of the arguments – sending the addresses of the arguments

5 Example program 1 – function call by value Memory main() change_value() Program Output a = 10 b = 20 x = 20 y = 40 a = 10 b = 20 Press any key to continue … Go to program 10 a 20 b x 20 y

6 Example program 1 – function call by address Program Output a = 10 b = 20 x = 20 y = 40 a = 20 b = 40 Press any key to continue … Go to program Memory main() change_value() 10 a 20 b *x *y *x change_value(6505, 7505); =*(6504) =

7 Example program 2-function call by value Go to program

8 Example program 2-function call by address Go to program

9 Return value of a function

10 Points to remember Passing arguments by value is not the most efficient means for programming in C When arguments are passed by value, the called function is unable to modify the original contents of the incoming parameters When arguments are passed by address, the called function is able to modify the original contents of the incoming parameters A function only return a single value

11 Example program Write a program in which a user input radius of a circle. The program calculates the area and the perimeter of the circle using a single function Write a program

12 Conclusion If we want that the value of an actual argument should not get changed in the function being called, pass the actual argument by value. If we want that the value of an actual argument should get changed in the function being called, pass the actual argument by reference. If a function is to be made to return more than one value at a time then return these values indirectly by using a call by reference.

13 Recursive function In C, it is possible for the functions to call themselves A function is called ‘recursive’ if a statement within the body of a function calls the same function

14 Example program - factorial Go to program

15 Example program – factorial using recursion Go to program

16 Cont.

17 Cont. main() { …. fact = rec(3); printf ( "%d ", fact); } rec ( 3 ) { int f ; if ( 3 == 1 ) return ( 1 ) ; else f = 3 * rec ( ) ; return ( f ) ; } false rec ( 2 ) { int f ; if ( 2 == 1 ) return ( 1 ) ; else f = 2 * rec ( ) ; return ( f ) ; } rec ( 1 ) { int f ; if ( 1 == 1 ) return ( 1 ) ; else f = 2 * rec ( ) ; return ( f ) ; } false true f = 2 * 1; f = 2 f = 3 * 2; f = 6 fact = 6

18 Example program 2 Write a definition of a function that adds n integers using recursion and then return the sum. Prototype of the function is below int sum_number(int); – int sum_number(int); Write a program

19