Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure.

Slides:



Advertisements
Similar presentations
6 Recap Functions.
Advertisements

Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
 2006 Pearson Education, Inc. All rights reserved Functions.
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.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
 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.
Stacks.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
 2007 Pearson Education, Inc. All rights reserved C Functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
CS 117 Spring 2002 April 5, Exam 3 April 10 –files, arrays, strings, classes –practice exams are up –review on Monday.
 2006 Pearson Education, Inc. All rights reserved Functions.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
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.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
 Monday 10/18/2010  Content: Week 1 – Week 6  Format:  Multiple choice questions  Matching questions  Determine what’s wrong  Determine the results.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Stack and Heap Memory Stack resident variables include:
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
CMSC 1041 Functions II Functions that return a value.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Functions Review.
C++ Programming Lecture 10 Functions – Part II
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CS 261 – Recitation 2 Fall 2013 Oregon State University School of Electrical Engineering and Computer Science.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
+ Arrays & Random number generator. + Introduction In addition to arrays and structures, C supports creation and manipulation of the following data structures:
1D Arrays and Random Numbers Artem A. Lenskiy, PhD May 26, 2014.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.
STACK Data Structure
CSE202: Lecture 13The Ohio State University1 Function Scope.
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.
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Functions.
C Functions Pepper.
Functions, Part 2 of 2 Topics Functions That Return a Value
Number guessing game Pick a random number between 1 and 10
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
6.11 Function Call Stack and Activation Records
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
توابع در C++ قسمت اول اصول كامپيوتر 1.
Chapter 6 - Functions Outline 5.1 Introduction
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Chapter 9: Value-Returning Functions
Assignment Operators Topics Increment and Decrement Operators
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure The last item pushed (inserted) on the stack is the first item popped (removed) from the stack

Function Call Stack Supports the function call/return mechanism Each time a function calls another function, a stack frame (also known as an activation record) is pushed onto the stack Maintains the return address that the called function needs to return to the calling function Contains automatic variables—parameters and any local variables the function declares When the called function returns Stack frame for the function call is popped Control transfers to the return address in the popped stack frame Stack overflow Error that occurs when more function calls occur than can have their activation records stored on the function call stack (due to memory limitations)

Function call stack int fcn1 (int local_int) { int x; x = fcn2(local_int * 2); return (x - 3); } int fcn2 (int local_int2) { int y = local_int2 – 85; return y; } int main() { int z; z = fcn1 (10); printf(“%d\n”,z); return 0; }

Random function rand() function Declared in generate a random (integer) number between 0 and ' RAND_MAX ' (at least 32767). RAND_MAX is defined in a header file ( stdlib.h ) srand is necessary for selecting a “SEED” for random number generation Syntax int r; r = rand();

srand() function srand seeds the random number generation function rand() so it does not produce the same sequence of numbers Library: stdlib.h Prototype: void srand(unsigned int seed); Syntax: unsigned int seed=10; /* seed value */ srand(seed); Quite often, we use srand(time(NULL)); // seed value will be current time

#include int main() { int count1=0, count2=0, count3=0, count4=0, count5=0, count6=0; int face, roll; srand(time(NULL)); for (roll=0 ; roll <= 6000 ; roll++) { face = 1 + rand() % 6 ; // get a random number between 1 and 6 switch (face) { case 1 : count1++; break; case 2 : count2++; break; case 3 : count3++; break; case 4 : count4++; break; case 5 : count5++; break; case 6 : count6++; break; default : printf("This case is impossible!\n"); break; }

printf("1 : %5d\n",count1); printf("2 : %5d\n",count2); printf("3 : %5d\n",count3); printf("4 : %5d\n",count4); printf("5 : %5d\n",count5); printf("6 : %5d\n",count6); return 0; } 1 : : : : : : 990 output

Using Array?