Func%ons. CS449 Fall 2017

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

Outline. Pointers arithme.c and others Func.ons & pointers

Functions. Arash Rafiey. September 26, 2017

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

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

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

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

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?

Lecture 9 - C Functions

Local defini1ons. Func1on mul1ples- of

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Fundamentals of Programming & Procedural Programming

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

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

Operators and Control Flow. CS449 Fall 2017

Lecture 04 FUNCTIONS AND ARRAYS

Unit 7. Functions. Need of User Defined Functions

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Subject: Fundamental of Computer Programming 2068

C: How to Program. Week /Mar/05

Computer Programming Lecture 12 Pointers

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).

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

AMCAT Automata Coding Sample Questions And Answers

Slides adopted from T. Ferguson Spring 2016

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.


6.096 Introduction to C++ January (IAP) 2009

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

CS201- Introduction to Programming Current Quizzes

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

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

Structured Programming. Functions and Structured Programming. Functions. Variables

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

Memory Management. CS449 Fall 2017

Chapter 2 - Introduction to C Programming

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

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Chapter 1 & 2 Introduction to C Language

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

Memory Allocation in C

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

Fundamentals of Programming Session 14

Computer Programming for Engineering Applica4ons. Intro to Programming 10/2/13 ECE 175. Limita4ons of Reference by Value. The Concept of Pointers

MODULE 3: Arrays, Functions and Strings

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Functions. Using Bloodshed Dev-C++ Heejin Park. Hanyang University

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

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

Fundamentals of Programming. Lecture 3: Introduction to C Programming

EK131 E5 Introduction to Engineering

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

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

BBM 201 DATA STRUCTURES

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

CS 0449 Sample Midterm

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Programming Studio #9 ECE 190

Array Initialization

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

SYSC 2006 C Winter 2012

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

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

D0010E Object- oriented programming and design. Today. Today An introduc<on to the basic syntax and seman<cs of Java

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Dept. of CSE, IIT KGP

LECTURE 3. Compiler Phases

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Functions in C C Programming and Software Tools

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

C Programming Primer 8/31/15 1

BRANCHING if-else statements

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

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

CS 345. Functions. Vitaly Shmatikov. slide 1

Programming and Data Structures

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

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

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

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

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

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

Dynamic memory allocation

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Computer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers

Chapter 17 Recursion

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Functions BCA-105. Few Facts About Functions:

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

The C language. Introductory course #1

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

Transcription:

Func%ons CS449 Fall 2017 1

Func%ons in C A group of statements performing a task Statements inside func%on is executed by calling it using the () operator Uses: Readability (through code modulariza%on) Reusability (e.g. the C Standard Library) Implemen%ng recursive algorithms through recursion (we will learn what this means soon) What makes C a procedural language Cf. Java is also a procedural language, and it is also an object-oriented language (something C is not) 2

Running Example int add(int a, int b); >>./a.out Sum: 7 int x = 3, y = 4, sum = 0; sum = add(x, y); prinz( Sum: %d\n, sum); int add(int a, int b) return a+b; 3

Func%on Declara%on int add(int a, int b); int x = 3, y = 4, sum = 0; sum = add(x, y); prinz( Sum: %d\n, sum); int add(int a, int b) return a+b; Syntax: <return type> <name> ( <parameter list> ); E.g. int add(int a, int b); Declares the func%on prototype A type declara%on for the func%on Func%on name + return type + parameter types Tells compiler the sizes / types of values passed back and forth on a func%on call Helps compiler translate calls correctly Compiler some%mes performs type conversion Must come before call (just like variable declara%ons) Can occur outside or inside func%ons Header file (e.g. stdio.h) contains func%on declara%ons #include copies and pastes contents of header file 4

Func%on Defini%on int add(int a, int b); int x = 3, y = 4, sum = 0; sum = add(x, y); prin8( Sum: %d\n, sum); int add(int a, int b) return a+b; Syntax: <return type> <name> ( <parameter list> ) declara%ons and statements E.g. int add(int a, int b) return a+b; Consists of: Func%on prototype (must match declara%on) Local variable declara%ons Statements main() is a special func%on that is called at the start of the program return statement exits from func%on Must return a value of the return type If return type is void, no return value needed Just do return; when exi%ng return; at the end can be omiged 5

Func%on Call int add(int a, int b); int x = 3, y = 4, sum = 0; sum = add(x, y); prin8( Sum: %d\n, sum); int add(int a, int b) return a+b; Syntax: <name> ( <argument list> ); E.g. add(x, y); Consists of: Func%on name Arguments (expressions that evaluate to each respec%ve type in parameter list) If number of arguments differ from number of parameters, it results in a compile error If argument types differ from parameters, arguments are converted into parameter types when possible All arguments are passed by value 6

Arguments are Passed by Value All arguments are passed by value in C Meaning: arguments are copied to parameters Argument variables and parameter variables reside in different memory loca%ons Modifying parameters will not modify arguments Cf. Java All arguments are also passed by value in Java Slight difference in what those values are Values in Java: primi%ve values, object references Values in C: primi%ve values, pointers, structs 7

When Passing by Value is Insufficient Works just fine for providing input values to a func%on E.g. int add(int x, int y) add() does not need (nor want) to change x or y But what if you want to modify the values of the input variables to a func%on? E.g. void swap(int x, int y) swap() needs to swap the values of x and y How can this be done when just the values are passed? (Any modifica%ons inside swap will not affect x and y) Answer: pass pointers to those variables Can t modify pointer but can modify what it points to 8

(Wrong) Example of Swap Func%on void swap(int a, int b); int x = 3, y = 4; prinz( x: %d, y: %d\n, x, y); swap(x, y); prinz( x: %d, y: %d\n, x, y); void swap(int a, int b) int temp = a; a = b; b = temp; >>./a.out x: 3, y: 4 x: 3, y: 4 9

(Wrong) Example of Swap Func%on void swap(int a, int b); int x = 3, y = 4; prinz( x: %d, y: %d\n, x, y); swap(x, y); prinz( x: %d, y: %d\n, x, y); void swap(int a, int b) int temp = a; a = b; b = temp; Problem: Parameters a and b occupy memory loca%ons that are different from x and y What is the solu%on? 10

(Correct) Example of Swap Func%on void swap(int *a, int *b); int x = 3, y = 4; prinz( x: %d, y: %d\n, x, y); swap(&x, &y); prinz( x: %d, y: %d\n, x, y); void swap(int *a, int *b) int temp = *a; *a = *b; *b = temp; Problem: Parameters a and b occupy memory loca%ons that are different from x and y What is the solu%on? Use pointers as arguments Pointers are s%ll passed by value But since now a stores a pointer to x, the x can be updated by dereferencing: *a Cf. Impossible to do in Java No pointers and no way to point to memory loca%ons 11

Example of Division Func%on int divide(int a, int b, int *rem); int x = 7, y = 3, quo%ent, remainder; quo%ent = divide(x, y, &remainder); prinz( quo%ent: %d, remainder: %d\n, quo%ent, remainder); int divide(int a, int b, int *rem) *rem = a % b; return a / b; >>./a.out quo%ent: 2, remainder: 1 12

Example of Division Func%on int divide(int a, int b, int *rem); int x = 7, y = 3, quo%ent, remainder; quo%ent = divide(x, y, &remainder); prinz( quo%ent: %d, remainder: %d\n, quo%ent, remainder); int divide(int a, int b, int *rem) *rem = a % b; return a / b; remainder modified through pointer Note: Value in remainder variable is not even used inside divide(). remainder variable is a receptacle to store a return value of the func%on Func%on can return mul%ple values by Returning one value normally Returning the rest by passing pointers to variables to receive those values Exactly the reason for & in scanf( %d, &x) Scanf is not interested in value of x x is a receptacle to store the scanned value 13

Recursion A func%on calling itself, or a group of func%ons calling each other in a cyclic pagern Useful in expressing many algorithms. E.g.: Fibonacci series F(n) = F(n-1) + F(n-2) Tree traversal Visit(node) = Visit(lev node) + Visit(right node) Binary Search Search(array) = Search(lev half) + Search(right half) 14

Example of Fibonacci Numbers int fibonacci(int); int i; for(i = 0; i < 10; ++i) prinz( %d \n, fibonacci(i)); int fibonacci(int n) if(n == 0 n == 1) return 1; return fibonacci(n-1) + fibonacci(n-2); >>./a.out Num: 1 1 2 3 5 8 13 21 34 55 15

Func%on Pointers Pointers can point to code (not only data) Code is also stored in memory and have addresses Value of a func%on is a pointer to its code (similar to arrays) () operator takes func%on as operand and jumps to the code pointed to by that func%on Value of func%on cannot be overwrigen But value can be assigned to func%on pointers Can use func%on pointers to call func%ons Useful when you want a single func%on call to jump to different func%ons in different situa%ons E.g. Depending on day of week, you might either go jogging, sleep in, or have brunch One call to do the appropriate thing at the appropriate %me? 16

Example of Func%on Pointers void jog() prinz("jog\n"); void sleep() prinz("back to sleep\n"); void brunch() prinz("brunch\n"); void (*f[7])() = jog, jog, jog, jog, jog, sleep, brunch; int i; void (*todo)(); for(i = 0; i < 7; ++i) todo = f[i]; todo(); >>./a.out Jog Jog Jog Jog Jog Back to sleep Brunch 17

Func%on Pointer Declara%on void jog() prinz("jog\n"); void sleep() prinz("back to sleep\n"); void brunch() prinz("brunch\n"); void (*f[7])() = jog, jog, jog, jog, jog, sleep, brunch; int i; void (*todo)(); for(i = 0; i < 7; ++i) todo = f[i]; todo(); Syntax: <return type> (*<name>) (<parameter list>) e.g. void (*todo)() Meaning: todo is a pointer to a func%on with a return type void and a parameter list of () e.g. void (*f[7])() Meaning: f is an array of 7 pointers to func%ons with a return type void and a parameter list of () Any func%on assigned to the func%on pointer should match its type 18

Func%on Pointer Call void jog() prinz("jog\n"); void sleep() prinz("back to sleep\n"); void brunch() prinz("brunch\n"); void (*f[7])() = jog, jog, jog, jog, jog, sleep, brunch; Syntax: <name>(argument list) e.g. todo() Meaning: call func%on pointed to by todo with argument list () int i; void (*todo)(); for(i = 0; i < 7; ++i) todo = f[i]; todo(); 19

Why Func%on Pointers? void todo(int date) if(date < 5) prinz("jog\n"); else if(i == 5) prinz("back to sleep\n"); else prinz( Brunch\n"); int i; for(i = 0; i < 7; ++i) todo(i); Alterna%ve implementa%on without func%on pointers Without func%on pointers code becomes Less modular Now a single func%on todo is forced to perform disparate tasks Less flexibile Now dates are hard-coded into todo and can t be changed With array of func%on pointers, array can be updated at run%me to change your weekly schedule 20

PiZall 1: Pass by value What do you think the following will print? void foo(char *s) s = "World"; char *str = "Hello"; foo(str); prinz("%s\n", str); Problem: str and s refer to different loca%ons 21

PiZall 1: Pass by value Solu%on: void foo(char **s) *s = "World"; char *str = "Hello"; foo(&str); prinz("%s\n", str); 22