KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 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.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
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.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Lecture 2 Friday 11 July Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
 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.
5 C Functions.
IS Program Design and Software Tools Introduction to C++ Programming
Functions and an Introduction to Recursion
C Functions -Continue…-.
C++.
5 C Functions.
Chapter 3 - Functions Outline 3.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
Functions and an Introduction to Recursion
5 C Functions.
5 C Functions.
Function.
1-6 Midterm Review.
Function.
Chapter 3 - Functions Outline 3.1 Introduction
Presentation transcript:

KIC/Computer Programming & Problem Solving 1

 Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving 2

to introduce simulation techniques using random number generation. to understand how to write and use functions that call themselves. to be able to pass arrays to functions. to understand basic sorting techniques. Objectives KIC/Computer Programming & Problem Solving 3

Header Files Header files – Contain function prototypes for library functions –,, etc – Load with #include #include Custom header files – Create file with functions – Save as filename.h – Load in other files with # include "filename.h" – Reuse functions KIC/Computer Programming & Problem Solving 4 tan(x) cos(x) sin(x) exp(x) log(x) pow(x,y) #include

Header Files KIC/Computer Programming & Problem Solving 5

6

7

Random Number Generation rand function – Load – Returns "random" number between 0 and RAND_MAX (at least ) i = rand(); – Pseudorandom Preset sequence of "random" numbers Same sequence for every function call Scaling – To get a random number between 1 and n 1 + ( rand() % n ) rand() % n returns a number between 0 and n - 1 Add 1 to make random number between 1 and n 1 + ( rand() % 6) – number between 1 and 6 KIC/Computer Programming & Problem Solving 8

9

Custom header files KIC/Computer Programming & Problem Solving 10

11 KIC/Computer Programming & Problem Solving

12 KIC/Computer Programming & Problem Solving

Storage Classes Storage class specifiers – Storage duration – how long an object exists in memory – Scope – where object can be referenced in program – Linkage – specifies the files in which an identifier is known Automatic Storage – Object created and destroyed within its block – auto: default for local variables auto double x, y; – register : tries to put variable into high-speed registers Can only be used for automatic variables register int counter = 1; KIC/Computer Programming & Problem Solving 13

Storage Classes Static Storage – Variables exist for entire program execution – Default value of zero – static: local variables defined in functions. Keep value after function ends Only known in their own function – extern: default for global variables and functions Known in any function KIC/Computer Programming & Problem Solving 14

Scope Rules File scope – Identifier defined outside function, known in all functions – Used for global variables, function definitions, function prototypes Function scope – Can only be referenced inside a function body – Used only for labels ( start:, case:, etc.) KIC/Computer Programming & Problem Solving 15

Scope Rules Block scope – Identifier declared inside a block Block scope begins at definition, ends at right brace – Used for variables, function parameters (local variables of function) – Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block Function prototype scope – Used for identifiers in parameter list KIC/Computer Programming & Problem Solving 16

KIC/Computer Programming & Problem Solving 17

Program Output KIC/Computer Programming & Problem Solving 18

Recursion 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 KIC/Computer Programming & Problem Solving 19

Recursion Example: factorials 5! = 5 * 4 * 3 * 2 * 1 Notice that 5! = 5 * 4! 4! = 4 * 3!... – Can compute factorials recursively – Solve base case ( 1! = 0! = 1 ) then plug in 2! = 2 * 1! = 2 * 1 = 2; 3! = 3 * 2! = 3 * 2 = 6; KIC/Computer Programming & Problem Solving 20

Recursion KIC/Computer Programming & Problem Solving 21

KIC/Computer Programming & Problem Solving 22

KIC/Computer Programming & Problem Solving 23