Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Similar documents
BSM540 Basics of C Language

BSM540 Basics of C Language

CC112 Structured Programming

Lecture 9 - C Functions

Programming and Data Structure

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

UNIT III (PART-II) & UNIT IV(PART-I)

CSCI 2132 Software Development. Lecture 18: Functions

Memory Allocation in C

Pointers. Pointers. Pointers (cont) CS 217

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

DECLARAING AND INITIALIZING POINTERS

Language comparison. C has pointers. Java has references. C++ has pointers and references

Fundamental of Programming (C)

Computer Science & Engineering 150A Problem Solving Using Computers

Unit 7. Functions. Need of User Defined Functions

Arithmetic Expressions in C

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

BİL200 TUTORIAL-EXERCISES Objective:

Computer Programming Lecture 12 Pointers

Basic and Practice in Programming Lab7

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Arrays and Pointers. CSE 2031 Fall November 11, 2013

At the end of this module, the student should be able to:

Course organization. Course introduction ( Week 1)

Overview (1A) Young Won Lim 9/14/17

Lecture 2: C Programming Basic

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Overview (1A) Young Won Lim 9/9/17

Functions. Arash Rafiey. September 26, 2017

Overview (1A) Young Won Lim 9/25/17

C programming basics T3-1 -

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Function I/O. Last Updated 10/30/18

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Question 2. [5 points] Given the following symbolic constant definition

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Pointers, command line arguments, Process control, and functions

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Arrays and Pointers. Lecture Plan.

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Technical Questions. Q 1) What are the key features in C programming language?

Fundamentals of Programming & Procedural Programming

Programming and Data Structures

Array Initialization

Pointers. Pointer References

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

C Program Structures

AROUND THE WORLD OF C

Subject: Fundamental of Computer Programming 2068

AMCAT Automata Coding Sample Questions And Answers

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Pointers and scanf() Steven R. Bagley

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j);

CS113: Lecture 4. Topics: Functions. Function Activation Records

Multiple Choice Questions ( 1 mark)

EL2310 Scientific Programming

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

Decision Making -Branching. Class Incharge: S. Sasirekha

Chapter 11 Introduction to Programming in C

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Programming Language A

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

C Programming Language

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Functions & Memory Maps Review C Programming Language

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Functions. (transfer of parameters, returned values, recursion, function pointers).

Computer Science 217 Final Exam May 15, :30pm-4:30pm

Slides adopted from T. Ferguson Spring 2016

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Lab 3. Pointers Programming Lab (Using C) XU Silei

CS240: Programming in C

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Func%ons. CS449 Fall 2017

CHAPTER 4 FUNCTIONS. 4.1 Introduction

2. Numbers In, Numbers Out

Introduction to Scientific Computing and Problem Solving

Chapter 11 Introduction to Programming in C

Arrays and Pointers in C. Alan L. Cox

Chapter 5 C Functions

n Group of statements that are executed repeatedly while some condition remains true

Transcription:

Lecture 16 Functions II they re back and they re not happy Daily Puzzle If it is raining at midnight - will we have sunny weather in 72 hours?

function prototypes For the sake of logical clarity, the main() function is preferably placed at the beginning of a program. After all, main() is always the first part of a program to be executed. function prototypes #include functiona #include main functionb call functiona call functionb main call functiona call functionb functiona functionb

function prototypes Placing functions after main() can be achieved using function prototypes. The purpose is to to establish the type of arguments a function is to receive and the order in which it is to receive them. function prototypes #include #include functiona functiona_proto functionb_proto main functionb call functiona call functionb main call functiona call functionb functiona functionb

function prototypes For example the prototype for the function add is: int add(int a, int b); This indicates that add has an int return value, and two arguments. function prototypes It is not necessary to specify the names of the arguments in a function prototype. So, an equally acceptable function prototype for add is: int add(int, int);

function prototypes!! #include <stdio.h> int add(int a, int b);!!! int main (void)!! int a=4, b=20, z;! z = add(a,b);! printf( %d\n, z);! function prototype This is needed because add is called before it is defined.!! int add(int a, int b)!!! int sum;! sum = a + b;! return sum;! Function add is defined after main. function prototypes #include <stdio.h> #define PI 3.14159265 /* function prototypes */ double find_circumference(double r); double find_area(double r); void main(void) double radius=3.7; printf( Area = %.2f\n, find_area(radius)); // Computes the circumference of a // circle with radius r double find_circumference(double r) return (2.0 * PI * r); // Computes the area of a circle with radius r double find_area(double r) return (PI * pow(r,2));

function prototypes Follow this rule: Either the function prototype or the entire code for a function must appear before the main function. pass-by-value When a single value is passed to a function as an argument then the value of that argument is simply copied to the function.

pass-by-value The argument radius used when calling find_area is not affected radius = 10.0; area = find_area(radius); Call find_area with r=10.0 Return result of 314.59 double find_area(double r) return (PI * pow(r,2)); The variable r in find_area can be modified pass-by-value y=add(x,y); copy of x, copy of y int add(int a, int b) int sum; sum = a + b; return sum;

pass-by-value radius = 10.0; area = find_area(radius); 10.0 input find_area output 314.59 pass-by-value Advantages? It allows a single-valued argument to be written as an expression, rather than being restricted to a single variable. In cases where the argument is a variable, the value of this variable is protected from alterations which take place within the function.

pass-by-value... and disadvantages Information cannot be transferred back to the calling portion of the program via arguments. Pass by value is strictly a one-way method of transferring information. returning multiple values Sometimes it is necessary to have functions which return more than a single value.

returning multiple values Consider a function which separates a floating point number into whole number and fractional parts. input parameter z split z_i z_f output parameters returning multiple values 7.3 z = input x =(int)floor(z) 7 y = z - x 0.3

returning multiple values #include <stdio.h> #include <math.h> /* Prototype for function split */ void split(double z, int *z_i, double *z_f); int main() double num, a, b; printf( Enter a floating-point number: ); scanf( %lf, &num); split(num, &a, &b); printf( Integer = %d, Fractional = %.2f\n", a, b); return 0; void split(double z, int *z_i, double *z_f) *z_i = (int)floor(z); *z_f = z - (*z_i); The values z_i and z_f are returned from the function split. The * signifies a value will be returned. returning multiple values A declaration of a simple output parameter such as int *z_i tells the compiler that z_i will contain the address of a type int variable.

returning multiple values z_i is a pointer to a type int variable z_f is a pointer to a type double variable void split(double z, int *z_i, double *z_f) *z_i = (int)floor(z); *z_f = z - (*z_i); returning multiple values The symbol & is used in front of a variable when calling the function to specify that a value is to be returned from the function. split(num, &a, &b);

returning multiple values address of a, address of b split(num,&a,&b); copy of num void split(double z, int *z_i, double *z_f) *z_i = (int)floor(z); *z_f = z - (*z_i); returning multiple values Parameter correspondence main num 7.43 a? b? split z 7.43 z_i 7421 z_f 7422 possible address in memory

returning multiple values double num=7.43, a, b; split(num, &a, &b); 7.43, &a, &b z z_i z_f split returning multiple values Passing parameters using pointers is commonly called pass-by-reference.

pass-by-reference There is a direct reference to a variable of type pointer to int and an indirect pointerfollowing reference to the same variable using the de-referencing operator. pass-by-reference For example:! int *z_i Here z_i is a pointer to int

pass-by-reference Consider the expression: *z_i = (int)floor(z); If z=7.43 this expression follows the pointer in z_i to the memory cell called a by main and stores the integer 7 there. pass-by-reference Similarly the statement: *z_f = z - (*z_i); uses two indirect references. One access the value in main s local variable a through the pointer in z_i, and another accesses b of main through the pointer z_f, to give the final output argument the value 0.43.

how to handle *? How are variables beginning with * handled within the body of a function? Just like any other variable. To avoid confusion with the multiplication operator, enclose these variables within parentheses. *z_f = z - (*z_i); 2-way parameters Call-by-reference parameters can also be used as input/output parameters. Values are passed to the function and returned from the function using the same variable. Consider a function swap() which interchanges two values.

2-way parameters #include <stdio.h> // A function to swap two integers void swap(int *, int *); int main() int a=2, b=4; printf( %d %d\n, a, b); swap(&a, &b); printf( %d %d\n, a, b); return 0; void swap(int *p, int *q) int temp; temp = *p; *p = *q; *q = temp; 2-way parameters no value is returned pointers to type int void swap(int *p, int *q) int temp; temp = *p; *p = *q; *q = temp; temporary storage

2-way parameters int a=2, b=4; swap(&a, &b); 2 4 a b void swap(int *p, int *q) int temp; temp = *p; *p = *q; *q = temp; 2-way parameters int a=2, b=4; swap(&a, &b); 2 4 a b void swap(int *p, int *q) int temp; temp = *p; *p = *q; *q = temp; temp = 2 a = 4 b = 2

2-way parameters How to read in pass-by-reference variables in a function? void input_values(int *a, int *b) Note - no & s &*a is equivalent to a scanf("%d %d", a, b); printf( Inside function: %d %d\n", *a, *b); void main(void) int a,b; input_values(&a,&b); printf("outside function: %d %d\n",a,b); What about main? It is possible to give the main function a parameter list.

What about main? Consider the main function header: int main(int argc, char *argv[]) integer variable array of pointers to characters What about main? #include <stdio.h> /* Program to read and echo data from command line */ int main(int argc, char *argv[]) int i; for (i = 1; i < argc; i++) printf("%s ", argv[i]); printf("\n"); return 0;

What about main? Assuming that the executable is called repeat, the typical output from this program is as follows: % repeat The quick brown fox jumped over the lazy hounds The quick brown fox jumped over the lazy hounds %