PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.

Slides:



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

Introduction to C Programming
Introduction to C Programming
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
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
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Kernighan/Ritchie: Kelley/Pohl:
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Storage Classes.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
FUNCTIONS. C function can be classified into two categories, namely, library functions and user – defined functions. main is an example of user – defined.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
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.
CSCI 171 Presentation 6 Functions and Variable Scope.
Principles of Programming - NI Chapter 6: Function In this chapter, you will learn about Introduction to function User define function Function prototype.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Definition of function Types of Function Built-in User Defined Catagories of Function No argument No return value Argument but No return value No argument.
Chinese Proverb says……... Advantages void main( ) { int n, k, i ; printf(“\n Enter number:-”); scanf(“%d”, &n); for(i=2 ; i
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointers A pointer is a variable which stores the address of another variable A pointer is a derived data type in C which is constructed from fundamental.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
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.
A First Book of ANSI C Fourth Edition
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 4 Function and Structure.
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
Storage class in C Topics Automatic variables External variables
trigraph ??= is for # ??( is for [ ??< is for { ??! is for |
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Scope, Parameter Passing, Storage Specifiers
Local Variables, Global Variables and Variable Scope
C Storage classes.
Storage class.
Chapter 7: User-Defined Functions II
Scope Rules Of Variables
In C Programming Language
Submitted By : Veenu Saini Lecturer (IT)
STORAGE CLASS.
STORAGE CLASS.
C Language B. DHIVYA 17PCA140 II MCA.
C Programming Lecture-17 Storage Classes
Functions Chapter No. 5.
Storage Classes.
Presentation transcript:

PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC # REGISTERREGISTER

Functions communicate with each other by passing arguments. It can be passed in Two Ways 1.Call By Value 2. Call by Reference BACK

Passing Parameters Call by value Call by reference

The values are passed through temporary variables. Any manipulation to be done only on these temporary variables. The called function does not access the actual memory location of the original variable and therefore cannot change its value.

/* CALL BY VALUE EXAMPLE*/ #include void add(int,int); void main() { int x,y; printf("Enter two number"); scanf("\t\t%d %d",&x,&y); add(x,y); } void add(int a,int b) { int c=a+b; printf("\t\tThe C Value is %d",c); } Enter two number = The C Value is 80 BACK

The function is allowed access the actual memory location(Address) of the argument (original variable) and therefore can change the value of the arguments of the calling routine have to be changed.

/*CALL BY REFERENCE as well as to swap 2 numbers using pointers */ #include void swap(int *,int *); void main() { int a,b; printf("\nEnter the numbers to swap"); scanf("%d %d",&a,&b); printf("The values before swapping :"); printf("\n%d %d",a,b); swap(&a,&b); } void swap(int *e,int *f) { int *temp; *temp=*e; *e=*f; *f=*temp; printf("\n The swapped values are %d %d",*e,*f); }

NOTE : &  Address of *  Content of Enter the numbers to swap = 5 6 The values before swapping : 5 6 The swapped values are : 6 5 BACK

 Functions are easier to write and understand  The arguments are separated by commas  The body of the function may consist of one or many statements  It cannot be defined within another function  Function prototype is a function declaration that specifies the data types of the arguments  Calling one function from within another is said to be nesting of Function calls  main() returns an integer which is generally the operating system

Every ‘C’ variable has a characteristic called its Storage Class. All variables have datatype and storage classes Keyword Where it is Declared Storage Area Default Initial value Lifetime of a variable BACK

1.Local or Auto or Internal variable 2. External or Global variable 3.Static variable 4.Register Variable

The scope of an variable refers to the region within which the variable is declared and can hence be utilized.

Any variable declared inside a function is said to be local variable i.e. specific to that function. It can only be accessed by the function in which it is created. After the end of the function, the local variable is automatically destroyed and it can not be retrieved. Hence the Scope of local variable is limited to the function in which it is defined. Example : float sum(float a, float b) { float c; c=a+b; return c; }

Auto Variable are always declared within a function and they are local to the function in which they are declared. Hence they are also named as local variables Keyword : auto Declaration : Inside the function Storage Area : Stack Initial Value : Garbage value (At the time of compilation compiler assigns any value) Lifetime : Upto that function only Example : auto int x; (or) int x; #include void function1(); void function2(); void main() { int m=1000; function2(); printf("%d\n",m); } void function1() { int m=10; printf("%d\n",m); } void function2() { int m=100; function1(); printf("%d\n",m); } BACK

Variable declared outside the main program are available to all the functions declared within it as well as the main function. These variable are said to be global to these functions, Hence the scope of global variable is the entire function. Example: #include Int count,x; main() { int sum; …………. } Int some() { Int I; …………. }

A variable which can be access with in a function and outside the main function. These variables are also named as Global variables or External variables Keyword : extern Declaration : Outside of the main() function Storage Area : CPU–memory Initial Value : zero Lifetime : Upto the entire program Example : int x; (or) main() { { extern int x; } #include int k; void function1(); void function2(); void function3(); void main() { k=20; function1(); function2(); function3(); } void function1() { k=k+10; printf("%d\n",k); } void function2() { k=k+1000; printf("%d\n",k); } void function3() { k=k+10; printf("%d\n",k); } BACK

This variable static is constant and the value is continued in all the steps. Keyword : static Declaration : Inside the function Storage Area : CPU – memory Initial Value : Zero Lifetime : The value of the variable persists between different function calls. Example : static int x; /* To print the value of x */ #include void stat(); void main() { int i; for(i=1;i<=5;i++) stat(); //calling } void stat() //definition { static int x=0; printf("x=%d\n",x); x=x+1; }

/* Example 2 */ #include void incre(); /* Function prototype declaration */ void main() { clrscr(); incre(); getch(); } void incre() { static char x=65; printf("\n The character stored in x is %c",x++); } The character Stored in x is A The character Stored in x is B The character Stored in x is C BACK

These variables are stored in CPU registers and hence they can be accessed faster than the one which is stored in memory. Keyword : register Declaration : Inside the function Storage Area : CPU - Register Initial Value : Garbage value(At the time of compilation compiler assigns any value) Lifetime : Upto that function only Example : register int x; Note : register double x; register float y; Registers are usually a 16bit therefore it cannot hold a float or double data type value which require 52 & 64 bytes respectively for storing a value. But the compiler would treat as automatic variables #include void main() { register int x; clrscr(); printf("\n The value is %d",x); getch(); } -899 (Garbage Value) BACK