Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Modular Programming With Functions
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Variable Scope Storage Class Recursion
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
Chinese Proverb says……... Advantages void main( ) { int n, k, i ; printf(“\n Enter number:-”); scanf(“%d”, &n); for(i=2 ; i
Week 11 Multi-file Programs and Scope of Variables.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
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.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Chapter 7: User-Defined Functions II
“Studying C programming excluding pointers is meaningless.” d0m3z
C Functions -Continue…-.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Object-Oriented Programming Using C++
CSC 253 Lecture 8.
Scope, Parameter Passing, Storage Specifiers
CSC 253 Lecture 8.
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
Local Variables, Global Variables and Variable Scope
Chapter 7: User-Defined Functions II
Function.
Chapter 9: Pointers and String
Function.
CPS125.
Scope Rules.
Presentation transcript:

Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic

Department of Electronic & Electrical Engineering Functions are cool --- use them. Functions are very useful for encapsulating a procedure. Allows it to be reused. printf and scanf are examples of library functions that help us with IO Some languages have subroutines. These are just functions that do not return anything.

Department of Electronic & Electrical Engineering Function. Declarations and definitions float func(float z); / * Declaration of a function prototype */ /* Should be done before using it */ int main() { printf(" func()=%f",func(9.0)); /* calling a function */ } float func(float z) { /* definition of the function */ return z*z; } type of thing returned Parameter 9.0 is an argument

Department of Electronic & Electrical Engineering Function. Declarations and definitions /* If you define a function before using you don't need to declare it */ float func(float z) { /* definition of the function */ return z*z; } int main() { printf(" func()=%f",func(9.0)); /* calling a function */ }

Department of Electronic & Electrical Engineering void type. void func(float x) { printf(" The value passed was %f \n",x); } Use void type if function does not return a value

Department of Electronic & Electrical Engineering Arguments are copied into functions. void func(float x) { x=5.0; } int main() { float y=2.0; func(y); /* Y is still 2.0 */ } argument values are copied into parameters memory x y 2 ? Use void type if function does not return a value

Department of Electronic & Electrical Engineering Pointers and dereferencing (*) int x; int y; int *ptr; x=6; ptr = &x; y=*ptr; *ptr=7; int *ptr is how we declare pointers. ptr is a pointer variable. it is used as an address in memory x ptr 7 34A45678 memory (after running) Address of x Using * to get or set the memory is called dereferencing y 6

Department of Electronic & Electrical Engineering Using pointers as arguments to a function void func(int *ptr) { *ptr=7; } int main() { int z=6; func(&z); printf("%d\n",z); } argument points at z e.g. addressof Parameter is a pointer to an int Now z holds the value 7

Department of Electronic & Electrical Engineering Local variables only exist within the declaring block Scope of variables. int x; float func(int y) { float z=6.0; return x*z*y; } int main() { int i=6; x=2.0; x=func(i)*x; } anything declared outside a function is global you can use it anywhere. Parameters are local and only exist within the function.

Department of Electronic & Electrical Engineering static versus automatic storage int x; void func(){ float y; static float z;... } global variables are always persistent (static). Storage is allocated when the program loads. Any initialisation happens once. by default local variables are allocated storage when we enter the block (automatic) using static means storage is allocated when program loads.

Department of Electronic & Electrical Engineering Example. int z=2; void func(){ static int y=4; int x=3; x++; y++; z++; printf("x=%d y=%d z=%d\n",x,y,z); } int main(){ func(); } Each time Once at the start

Department of Electronic & Electrical Engineering Summary Use functions to encapsulate code - reuse + easier to read program Pointers are used if you want to change the value of an argument. Scope - Global (declared outside any function) - Local (within a function/block) static key word allocate local storage once at the start (global variables are always static by default) local variables are automatic by default.