Local Variables variables which are declared within a

Slides:



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

An introduction to pointers in c
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CS 201 Functions Debzani Deb.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
How to start Visual Studio 2008 or 2010 (command-line program)
Nested LOOPS.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Introduction to Programming
Chapter 7: User-Defined Functions II
CSE / ENGR 142 Programming I
CSCI206 - Computer Organization & Programming
AKA the birth, life, and death of variables.
C Functions -Continue…-.
Chapter 2 Elementary Programming
FIGURE 4-10 Function Return Statements
The Three Attributes of an Identifier
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
ICS103 Programming in C Lecture 3: Introduction to C (2)
Functions Dr. Sajib Datta
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions, variables, operators, loops Modularity
Functions in C Mrs. Chitra M. Gaikwad.
Lecture2.
User-Defined Functions
CSC 253 Lecture 8.
Scope, Parameter Passing, Storage Specifiers
FUNCTIONS WITH ARGUMENTS
FIGURE 4-10 Function Return Statements
CSC 253 Lecture 8.
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
User Defined Functions
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Functions Chapter 3 of the text Motivation:
Stack Memory 2 (also called Call Stack)
CSCE 206 Lab Structured Programming in C
Functions.
Lecture 18 Arrays and Pointer Arithmetic
Lec8.
Subroutines – parameter passing
A function with one argument
FIGURE 4-10 Function Return Statements
AKA the birth, life, and death of variables.
Function In this lesson, you will learn about Introduction to Function
Chapter 7: User-Defined Functions II
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
In C Programming Language
Functions Extra Examples.
FIGURE 4-10 Function Return Statements
CSCE 206 Lab Structured Programming in C
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
C# Language & .NET Platform 10th Lecture
Scope Rules.
Presentation transcript:

Local Variables variables which are declared within a function are LOCAL to the function They can only be used within the piece of code of the function. formal parameters are ALSO local to a function formal parameter (local as well) double circle_area(double radius) { double area; area = PI * radius * radius; return area; } local variable From a function, we only get the return value Everything else is forgotten once we are done with the execution of the function However, ... (see later the use of & and *) 142 G -1

An Example #include<stdio.h> #include<stdlib.h> int addone(int val); int main(void) { int a = 1; int b; b = addone(a); printf("a=%i, b=%i",a,b); return EXIT_SUCCESS; } int addone(int a) a = a + 1; return a; a (local to main) is the actual parameter in the call of addone a (formal parameter) is local to addone (nothing to do with a in main!) What is printed by printf? a=1, b=2 142 G -2

Memory Organization Before b=addone(a); After b=addone(a); main a b 1 main a b anything main a b 1 2 1 anything 2 X addone a addone a addone a 2 X 1 Not available Not available Call by VALUE 142 G -3

Using functions: an example Computing the area of a washer: outer_radius inner radius area = area of outer circle - area of inner circle Algorithm: 1) User inputs the inner and outer radius 2) compute the area of the washer (use a function to compute the area of a circle) 3) Display the washer area 142 G-4

Static Call Graph main washer_area scanf printf circle_area 142 G-5

Local Variables: Summary Formal parameters and variables declared in a function are LOCAL to it cannot be accessed by other functions allocated (created) on function entry De-allocated (destroyed) on function return Formal parameters are initialized by copying the values of the actual parameters. Call BY VALUE A GOOD IDEA! localize information reduce interactions 142 G-6