TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions.

Slides:



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

Introduction to C Programming
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Functions a group of declarations and statements that is assigned a name effectively, a named statement block usually has a value a sub-program when we.
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.
 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.
Computer Science 210 Computer Organization Introduction to C.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Return function Random function Recursion function Function in C 1.
CMSC 1041 Functions II Functions that return a value.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
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.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
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.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
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.
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 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality in the process.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
Computer Science 210 Computer Organization
Introduction to Programming
C Functions -Continue…-.
Functions, Part 2 of 2 Topics Functions That Return a Value
Module 4 Functions – function definition and function prototype.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Computer Science 210 Computer Organization
Chapter 5 - Functions Outline 5.1 Introduction
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Chapter 6 - Functions Outline 5.1 Introduction
بنام خدا زبان برنامه نویسی C (21814( Lecture 4 Chapter 5
שיעור רביעי: פונקציות, מבוא לרקורסיה
Functions, Part 2 of 3 Topics Functions That Return a Value
In C Programming Language
Introduction to Problem Solving and Programming
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Main() { int fact; fact = Factorial(4); } main fact.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions are used in a program, be sure to include their prototypes. Library function prototypes are placed in specific header files. Example: Function prototypes of printf() and scanf() can be found in header file stdio.h.

TK1913-C Programming2 TK1913-C Programming 2 To include a function prototype defined in a header file into our program, use the preprocessor instruction #include. Example, #include

TK1913-C Programming3 TK1913-C Programming 3 math Library function One type of library functions which provides several functions for execution of mathematic operations is math Prototypes of these functions are declared in a header file named math.h

TK1913-C Programming4 TK1913-C Programming 4 Example: sqrt() function This function can be used to attain the square root of a given number The function declaration: Example: double num; printf(“Input a number: "); scanf("%lf", &num); printf(“Square root of %.2lf: %.2lf\n”, num, sqrt(num)); double sqrt(double n); The number its square root you want to get The data type returned by function sqrt() math Library function

TK1913-C Programming5 TK1913-C Programming 5 Example: abs() function This function is used to get the absolute value of a number The function declaration: Example: int num, abs_value; printf(“Input number: "); scanf("%d", &num); abs_value = abs(num); printf(“Absolute value for %d: %d\n”, num, abs_value); int abs(int n); The number its absolute value you want to get The data type returned by abs() function math Library function

TK1913-C Programming6 TK1913-C Programming 6 Variable scope There are two types of scope for variables: local and global Local variable : May only be accessed and manipulated in the block where it is declared Global variable : May be accessed and manipulated anywhere in a program, including in user-defined functions Must be declared outside a function definition Is not encouraged because program readability may be compromised and maintenance more tedious

TK1913-C Programming7 TK1913-C Programming 7 #include void negative(int); int main( ) { int num; scanf("%d", &num); negative(num); printf(“Value of num is now: %d\n", num); } void negative(int number) { number = -number; } Example: ??? num ??? number Variable scope : local variables

TK1913-C Programming8 TK1913-C Programming 8 In the following example, the variable number in main() and variable number in negative() are not referring to the same memory location. Both are local variable Variable scope : local variables number #include void negative(int); int main( ) { int number; scanf("%d", &number); negative(number); printf(“Value of number is now: %d\n", number); } void negative(int number) { number = - number; } void negative(int number) number int main( )

TK1913-C Programming9 TK1913-C Programming 9 In the following example, number in main() and negative() are referring to the same memory location #include void negative(); int number; int main( ) { scanf("%d", &number); negative(); printf(“Value of number is now : %d\n", number); } void negative() { number = -number; } Variable scope : global variables number int main( ) void negative()

TK1913-C Programming10 TK1913-C Programming 10 Can a function call itself ? A function that calls itself is known as recursive function Recursive function is used to handle problem that can be broken into sub-problem which resembles the original problem, repeatedly until the solution is achieved Recursive Function

TK1913-C Programming11 TK1913-C Programming 11 Example : Factorial formula can be written as : 1, when n = 0 n! = n * (n-1) * … * 2 * 1, when n >= 1 In other words, 1, when n = 0 n! = n * (n-1)!, when n >= 1 Recursive Function

TK1913-C Programming12 TK1913-C Programming 12 factorial(5) = 5 * factorial(4) Getting the value of factorial(5) using recursion Recursive Function Example : factorial factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * factorial(0) factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * factorial(0) factorial(0) = 1 factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * 1 factorial(0) = 1 factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * 1 factorial(1) = 1 factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * 2 factorial(2) = 2 factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 6 factorial(5) = 5 * factorial(4) factorial(4) = 4 * 6 factorial(3) = 6 factorial(5) = 5 * factorial(4) factorial(4) = 24 factorial(5) = 5 * 24 factorial(4) = 24 factorial(5) = 120

TK1913-C Programming13 TK1913-C Programming 13 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial( int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } This is a recursive function because a function call in its body calls itself. factorial function (using recursion)

TK1913-C Programming14 TK1913-C Programming 14 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial( int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 0! = 1 n! = n * (n – 1)! factorial function (using recursion)

TK1913-C Programming15 TK1913-C Programming 15 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } ??? n _ int main() Input n :_ 5 Input n : 5 5 n1n1 Caller CalledP/m passed Returned value main fac-15 fac-1

TK1913-C Programming16 TK1913-C Programming 16 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { Caller CalledP/m passed Returned value main fac-1 5 Caller CalledP/m passed Returned value main fac * factorial(4) fac-2 (4) n2n2 4 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1 fac-24

TK1913-C Programming17 TK1913-C Programming 17 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { fac-3 (3) n2n2 4 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1 fac-2 4 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1 fac * factorial(3) 3 n3n3 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2 fac-33

TK1913-C Programming18 TK1913-C Programming 18 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { fac-4 (2) n2n2 4 3 n3n3 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2 fac-3 3 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2 fac * factorial(2) n4n4 2 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3 fac-42

TK1913-C Programming19 TK1913-C Programming 19 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { fac-5 (1) n2n2 4 3 n3n3 n4n4 2 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3 fac-4 2 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3 fac * factorial(1) n5n5 1 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3fac-42 2 * factorial(1) fac-4 fac-51

TK1913-C Programming20 TK1913-C Programming 20 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { fac-6 (0) n2n2 4 3 n3n3 n4n4 2 n5n5 1 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3fac-42 2 * factorial(1) fac-4 fac-5 1 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3fac-42 2 * factorial(1) fac-4 fac * factorial(0) n6n6 0 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3fac-42 2 * factorial(1) fac-4fac-51 1 * factorial(0) fac-5 fac Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3fac-42 2 * factorial(1) fac-4fac-51 1 * factorial(0) fac-5 fac-6 0 1

TK1913-C Programming21 TK1913-C Programming 21 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { n2n2 4 3 n3n3 n4n4 2 n5n5 1 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3fac-42 2 * factorial(1) fac-4 fac * 1 1 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3fac-42 2 * factorial(1) fac-4 fac-5 1 1

TK1913-C Programming22 TK1913-C Programming 22 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { n2n2 4 3 n3n3 n4n4 2 2 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3 fac * 1 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2fac-33 3 * factorial(2) fac-3 fac Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2 fac * 2

TK1913-C Programming23 TK1913-C Programming 23 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { n2n2 4 3 n3n3 6 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2 fac * 2 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1fac-24 4 * factorial(3) fac-2 fac-3 3 6

TK1913-C Programming24 TK1913-C Programming 24 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { n2n Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1 fac * 6 Caller CalledP/m passed Returned value mainfac-15 5 * factorial(4) fac-1 fac

TK1913-C Programming25 TK1913-C Programming 25 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n - 1)); } 5 n Input n : 5 5 n1n1 int factorial(int n) { 120 Caller CalledP/m passed Returned value main fac * 24 Caller CalledP/m passed Returned value main fac Input n : 5 Value of 5! = 120 int main()

TK1913-C Programming26 TK1913-C Programming 26 #include int factorial(int n); int main() { int n; printf(“Input n : "); scanf("%d", &n); printf(“Value of %d! = %d\n", n, factorial(n)); } int factorial(int n ) { if (n == 0) return 1; else return (n * factorial(n -1)); } Input n : 5 Value of 5! = 120