TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Lecture 10: Don't Reinvent the Wheel. Exam Results.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
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.
Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.
Introduction to Computers and Programming Introduction to Methods in Java.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 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.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
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 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
1 Lecture 3 Part 1 Functions with math and randomness.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Functions Review.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2007 Pearson Education, Inc. All rights reserved C Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
5 C Functions.
Functions Course conducted by: Md.Raihan ul Masood
Functions.
Dr. Shady Yehia Elmashad
5 C Functions.
5 C Functions.
Functions and an Introduction to Recursion
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Dr. Shady Yehia Elmashad
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Chapter 6 Methods: A Deeper Look
Chapter 6 - Functions Outline 5.1 Introduction
MSIS 655 Advanced Business Applications Programming
Functions and an Introduction to Recursion
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Presentation transcript:

TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function

OBJECTIVES In this chapter you will learn: To construct programs modularly from small pieces called functions. The common math functions available in the C Standard Library. To create new functions. The mechanisms used to pass information between functions. Simulation techniques using random num­ber generation. How to write and use recursive functions, i.e., functions that call themselves. 2

I NTRODUCTION Divide and conquer Construct a program from smaller pieces or components These smaller pieces are called modules Each piece more manageable than the original program 3

P ROGRAM M ODULES IN C Functions Modules in C Programs combine user-defined functions with library functions C standard library has a wide variety of functions Function calls Invoking functions Provide function name and arguments (data) Function performs operations or manipulations Function returns results Function call analogy: Boss asks worker to complete task Worker gets information, does task, returns result Information hiding: boss does not know details 4

5 Hierarchical boss function/worker function relationship.

M ATH L IBRARY F UNCTIONS Math library functions perform common mathematical calculations #include Format for calling functions FunctionName( argument ); If multiple arguments, use comma-separated list printf( "%.2f", sqrt( ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions 6

M ATH L IBRARY F UNCTIONS Include the math header by using the preprocessor directive #include when using functions in the math library. 7

8 Commonly used math library functions. (Part 1 of 2.)

9 Commonly used math library functions. (Part 2 of 2.)

F UNCTIONS Modularize a program All variables defined inside functions are local variables Known only in function defined Parameters Communicate information between functions Local variables Benefits of functions Divide and conquer Manageable program development Software reusability Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) Avoid code repetition 10

F UNCTIONS In programs containing many functions, main is often implemented as a group of calls to functions that perform the bulk of the program’s work. 11 Each function should be limited to performing a single, well- defined task, and the func­tion name should effectively express that task. This facilitates abstraction and promotes software reusability. Programs should be written as collections of small functions. This makes programs easier to write, debug, maintain and modify.

F UNCTIONS 12 void main(){ int a=10,b=20,m; int big(int x, int y); m=big(a,b); printf(“Max=%d”,m); } int big(int x, int y){ if(x>y) return(x); else return(y); }

F UNCTIONS 13 void main(){ int a=10,b=20,m int big(int x, int y); m=big(a,b); printf(“Max=%d”,m); } int big(int x, int y){ if(x>y) return(x); else return(y); } 1020 a b m

E XAMPLE : T O FIND THE LARGER BETWEEN TWO NUMBERS 14 void main(){ int a=10,b=20,m int big(int x, int y); m=big(a,b); printf(“Max=%d”,m); } int big(int x, int y){ if(x>y) return(x); else return(y); } 1020 a b 1020 x y 20 big 20 m

W RITE YOUR OWN FUNCTION Function definition Two principal components long int factorial(int n) { int i, long int prob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } return(prob); }

W RITE YOUR OWN FUNCTION Function definition Two principal components First line Contains data type to be returned Contains the function name A set of arguments (optional) long int factorial(int n) { int i, long int prob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } return(prob); }

W RITE YOUR OWN FUNCTION Function definition Two principal components First line Contains data type to be returned Contains the function name A set of arguments (optional) Body of function Contains expression statements for the operation required If data type of the function had been defined, should return more then one functions. long int factorial(int n) { int i, long int prob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } return(prob); }

F UNCTION P ROTOTYPES Function can be defined with first line and body However, compiler may confuse the function name and cause error of unknown variable or function Function prototypes is introduced to alert the compiler regarding existence of function #include main() { int a; printf("\na = "); scanf("%d", &a); d = cubebyvalue(a); printf("\n\n maximum = %d", a); return 0; } int cubebyvalue(int x) { return(x * x * x); } int cubebyvalue(int);

#include int cubebyvalue(int x) { return(x * x * x); }; main() { int a; printf("\na = "); scanf("%d", &a); d = cubebyvalue(a); printf("\n\n maximum = %d", a); return 0; }

Accessing function Usually known as calling a function A function can be called by its name (from first line ). Two type of function access Call by reference Call by value

Two type of function access Call by value Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument Avoids accidental changes 21 #include int cubebyvalue(int); main() { int a,d; printf("\na = "); scanf("%d", &a); d = cubebyvalue(a); printf("\n\n%d,%d", a,d); return 0; } int cubebyvalue(int x) { return(x * x * x); }

Call by reference Passes original argument Changes in function effect original Only used with trusted function #include void cubebyreference(int*); main() { int a,d; printf("\na = "); scanf("%d", &a); cubebyreference(&a); printf("\n\n %d,%d", a,d); return 0; } void cubebyreference(int *xPtr) { *xPtr =*xPtr * *xPtr * *xPtr; }

R ECURSION Recursive functions Functions that call themselves Can only solve a base case Divide a problem up into What it can do What it cannot do What it cannot do resembles original problem The function launches a new copy of itself (recursion step) to solve what it cannot do Eventually base case gets solved Gets plugged in, works its way up and solves whole problem 23

#include int factorial(int n); main() { int n; printf("n = "); scanf("%d", &n); printf("n! = %d\n", factorial(n)); } int factorial(int n) { if (n<=1) { return 1; }else { return(n * factorial(n-1)); }

5! = 5 * 4 * 3 * 2 *1 4! = 4 * 3 * 2 * 1 3! = 3 * 2 * 1 2! = 2 * 1 1! = 1 5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 factorial(5) in main Factorial(5) 1 5*4!4*3!3*2!2*1 Factorial(4) 5 Factorial(3) 4 Factorial(2) 3 Factorial(1) 2 return(n*factorial(n-1))

Check this out on youtube. NR=1&feature=fvwp

# E ND OF S ESSION # 27 Thank You