Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Lab 8 User Defined Function.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Kernighan/Ritchie: Kelley/Pohl:
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 Array Knowledge Understand the execute technique of array Skill Can write application program using one and two dimensional array.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Computer Science 210 Computer Organization Pointers.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
A First Book of ANSI C Fourth Edition
Runtime Environments Compiler Construction Chapter 7.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
UNIT 14 Functions with Pointer Parameters.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Review 1 List Data Structure List operations List Implementation Array Linked List.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
CSC 8505 Compiler Construction Runtime Environments.
/* example program to demonstrate the passing of an array */ #include int maximum( int [] ); /* ANSI function prototype */ int maximum(
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
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.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Computer Programming for Engineers
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
User-Defined Functions (cont’d) - Reference Parameters.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Chapter 7 User-Defined Methods.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Functions Pass By Value Pass by Reference
Pointers (continued) Chapter 11
Topics discussed in this section:
Simulating Reference Parameters in C
ECE Application Programming
Java Programming Language
A simple function.
Presentation transcript:

Parameter Passing to Functions in C

C Parameter passing Review of by-value/by-reference

By-Value: The value of a variable is sent to function. The actual parameter cannot be changed by function. "In" parameter

void f (int x) // x is an integer { x=x+1; }

For example int z = 5; f(z); This wont change z. Only interested in the value of z. What happens is a temporary copy of z is sent to the function. This copy is changed, the value returned and the copy then deleted If we want change we have to pass arguments by reference.

By-Reference An address gets sent to function. The function can change the values at that address "Out" or "In-Out“ parameter manipulation

Sending by reference in C requires the use of the pointer symbol (*). void h(int *p_x) // p_x is an address where an integer is stored. { *p_x = *p_x + 1; }

Can call a by-reference function using the address operator & int x=9; f(x) ; // f cannot change value of x. h(&x); // h can change the value of x

Array parameters are similar to Java in effect int array[10]; void g(int arr[]) g (array);

Trace the following:

Program main() { int x=6; int arr[5]; for (int i=0;i<5;i++) arr[i]=i; f(x); g(arr); h(&x); h(&(arr[4])); } void f(int z) { z=z+1; } void g(int a[]) { a[2]=8; } void h(int *z) { *z = *z+1; }

Program main() { int x=6; int arr[5] for (int i=0;i<5;i++) arr[i]=i; f(x); g(arr); h(&x); h(&(arr[4])); } X= 6 arr01234 f(x) x=6 g(arr) h(&X) x=7 h(&(arr[4])); 01835

Exercise Write 2 functions to calculate the square of integer X One which passes parameters by value The other by Reference X^2

Pass by Value Square int square_by_value(int x) { return x^2; } Note the return which passes a result back out based on the input value No Change has taken place to the input varaiable. Only its value has been used.

Pass by Reference Square void square_by_reference(int *x) { *x= *x^2; } Note there is no return type. However the value pointed at the input address is changed.

Example program to demonstrate the passing of an array #include int maximum( int [] ); main() { int values[5], i, max; printf("Enter 5 numbers\n"); for( i = 0; i < 5; ++i ) scanf("%d", &values[i] ); max = maximum( values ); printf("\nMaximum value is %d\n", max ); }

Maximum Function int maximum( int values[5] ) { int max_value, i; max_value = values[0]; for( i = 0; i < 5; ++i ) if( values[i] > max_value ) max_value = values[i]; return max_value; }

Sample Program Output Enter 5 numbers Maximum value is 121 Note: The program defines an array of five elements (values) and initializes each element to the users inputted values. The array values is then passed to the function.

maximum function Declaration The declaration int maximum( int values[5] ) defines the function name as maximum, and declares that an integer is passed back as the result, and that it accepts a data type called values, which is declared as an array of five integers.

A local variable max_value is set to the first element of values, and a for loop is executed which cycles through each element in values and assigns the lowest item to max_value.local variablefor This number is then passed back by the return statement, and assigned to max in the main section.