C Parameter Passing.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Spring Semester 2013 Lecture 5
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Functions Pass by Value Pass by Reference IC 210.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
1 CS161 Introduction to Computer Science Topic #10.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
C++ Pointers Review. Overview  What is a pointer  Why do I care?  What can be 'pointed to'?  Example.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Chapter 6 Methods Chapter 6 - Methods.
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.
Week 12 Methods for passing actual parameters to formal parameters.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
User-Defined Functions (cont’d) - Reference Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Lists in Python Lists as Arguments/Parameters. Lists as arguments to functions Just like other data types, lists can be sent to functions as arguments.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
Chapter 10: Void Functions
A Lecture for the c++ Course
Function There are two types of Function User Defined Function
Module 4 Functions – function definition and function prototype.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Programming Fundamentals Lecture #7 Functions
Object-Oriented Programming Using C++
Scope, Parameter Passing, Storage Specifiers
Object Oriented Programming COP3330 / CGS5409
METHODS AND BEHAVIORS AKEEL AHMED.
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Chapter 5 Function Basics
Systems Programming Concepts
Variables and Their scope
Passing Parameters by value
Pointers Call-by-Reference CSCI 230
Array and Method.
Functions Pass By Value Pass by Reference
Classes and Objects.
Parameter Passing in Java
FUNCTION CSC128.
ECE 103 Engineering Programming Chapter 32 Array Parameters
Simulating Reference Parameters in C
Function “Inputs and Outputs”
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
Submitted By : Veenu Saini Lecturer (IT)
A+ Computer Science PARAMETERS
ETE 132 Lecture 6 By Munirul Haque.
C Pointers Systems Programming.
Corresponds with Chapter 5
Scope Rules.
Presentation transcript:

C Parameter Passing

Passing Argument to Function In C Programming we have different ways of parameter passing schemes. Function is good programming style in which we can write reusable code that can be called whenever require. Whenever we call a function then sequence of executable statements gets executed. We can pass some of the information to the function for processing called argument.

There are Two Ways of Passing Argument to Function in C Language : Call by Reference or pass by reference Call by Value or pass by value

Call by Value While Passing Parameters using call by value xerox copy of original parameter is created and passed to the called function. Any update made inside method will not affect the original value of variable in calling function. . As their scope is limited to only function so they cannot alter the values inside main function.

Call by reference or address While passing parameter using call by address scheme , we are passing the actual address of the variable to the called function. Any updates made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.

Summary

Call by value Program void swapByValue(int, int); /* Prototype */ int main() /* Main function */ { int n1 = 10, n2 = 20; /* actual arguments will be as it is */ swapByValue(n1, n2); printf("n1: %d, n2: %d\n", n1, n2); } void swapByValue(int a, int b) int t; t = a; a = b; b = t;

Call by reference program void swapByReference(int*, int*); /* Prototype */ int main() /* Main function */ { int n1 = 10, n2 = 20; /* actual arguments will be altered */ swapByReference(&n1, &n2); printf("n1: %d, n2: %d\n", n1, n2); } void swapByReference(int *a, int *b) int t; t = *a; *a = *b; *b = t;