APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

Similar documents
Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

CSE 374 Programming Concepts & Tools

CS16 Exam #1 7/17/ Minutes 100 Points total

C Functions. 5.2 Program Modules in C

Functions. Lecture 6 COP 3014 Spring February 11, 2018

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

Unit 7. Functions. Need of User Defined Functions

Pointers, Arrays and Parameters

User Defined Functions

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

Lecture 04 FUNCTIONS AND ARRAYS

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

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

In addition to name. Each variable in C program is characterized by its Type Scope Storage Class. Type Have already discussed

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

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

CSE 230 Intermediate Programming in C and C++ Functions

Functions BCA-105. Few Facts About Functions:

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

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

Chapter 7 Functions. Now consider a more advanced example:

Data Structure and Programming Languages

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Lecture 04 FUNCTIONS AND ARRAYS

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Lecture 12 Modular Programming with Functions

Tutorial No. 2 - Solution (Overview of C)

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

A Foundation for Programming

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Functions in C C Programming and Software Tools

Computer Science & Engineering 150A Problem Solving Using Computers

Variables and Bindings

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

First of all, it is a variable, just like other variables you studied

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

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

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

Functions. Arash Rafiey. September 26, 2017

AMCAT Automata Coding Sample Questions And Answers

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

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Smaller, simpler, subcomponent of program Provides abstraction

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Decision Making -Branching. Class Incharge: S. Sasirekha

Programming for Engineers Introduction to C

Computing and Statistical Data Analysis Lecture 3

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

More Arrays. Last updated 2/6/19

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

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

The four laws of programs

Introduction to Computing Lecture 08: Functions (Part I)

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

EMBEDDED SYSTEMS PROGRAMMING Language Basics

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Chapter 2 - Introduction to C Programming

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

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

SPIM Procedure Calls

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

Subject: Fundamental of Computer Programming 2068

What is a Function? What are functions good for?

Formatting & Style Examples

15 FUNCTIONS IN C 15.1 INTRODUCTION

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

CS349/SE382 A1 C Programming Tutorial

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Chapter 3 - Functions

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

BSM540 Basics of C Language

Functions. Systems Programming Concepts

Execution order. main()

WARM UP LESSONS BARE BASICS

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Lecture 5: Methods CS2301

This exam is to be taken by yourself with closed books, closed notes, no calculators.

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Chapter 8: Intraprogram Communication. Lecture 8 1

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Chapter 2, Part I Introduction to C Programming

Transcription:

APS105 Functions (and Pointers) Functions Tetbook Chapter5 1 2 Modularity Modularity Break a program into manageable parts (modules) Modules interoperate with each other Benefits of modularity: Divide-and-conquer: work on small problems Small parts are easier to read/understand Different people can work on different parts Avoid duplication of the same code Re-use modules from other programs Can test each module in isolation C pre-defined functions Math functions Sqrt(), fabs(), etc I/O functions printf(), scanf() modularity/modules in C: functions 3 4 1

Two Types of Functions Commands perform a process, but don t return anything Queries evaluate something, return a result Some languages: commands: called procedures, subroutines queries: called functions C language: commands: called functions queries: called functions Defining a Function Define a function that prints a blank line void skipline (void) printf("\n"); Note: this is called a "function definition" 5 6 Incorporating a Function in a Program #include <stdio.h> void skipline (void) printf("\n"); skipline(); skipline(); Function Prototype Sometimes must summarize a function tell the compiler what it needs to know eg., if function has parameters or return value Function Prototype: similar to a copy of the first line of the function definition Eample of a prototype: void skipline (void); 7 8 2

Prototype Eample: Declare First #include <stdio.h> void skipline (void); skipline(); skipline(); a prototype here solves the problem compiler needs to know if this is the correct way to call skipline(), but it hasn't seen its definition yet Functions with Parameters Define a function that prints n blank lines void skiplines (int n) for (int i = 1; i <= n; i++) printf("\n"); void skipline (void) printf("\n"); Note: eecution always starts with regardless of the order functions appear in the file 9 Note: n is called a "parameter" 10 Calling a Function with Parameters #include <stdio.h> void skiplines (int); int i = 10; skiplines(i+5); void skiplines (int n) need only provide the type of each parameter i+5 is called an "argument" When A Function is Called 1) Arguments are evaluated 2) control passes to the function 3) parameters are assigned values the values of the arguments are copied i.e., param gets a copy of the value of the arg this method is called "call-by-value" the only method used by C other languages provide other methods 4) the function is eecuted 5) control passes back to the calling function 11 12 3

Eample of A Query Function a function that returns the factorial of an int int factorial (int n) // 5 copied to n int product = 1; for (int i=1; i <= n; i++) product *= i; return product; // returns 120 int int = 2, y; y = factorial( + 3); // called with 5 // 120 copied to y 13 Animation of calls foo calls bar int = foo(4); int = foo(4); 4

int = foo(4); int = foo(4); int = foo(4); foo y 4 int = foo(4); foo y 4 5

int = foo(4); foo bar z 4 y 4 = 11 int = foo(4); foo bar z 4 y 4 = 11 int = foo(4); foo bar z 4 y 4 = 11 int = foo(4); foo y 4 6

= 15 int = foo(4); foo y 4 = 15 int = foo(4); foo y 15 int = foo(4); = 15 int = foo(4); 15 7

int = foo(4); 15 Function Scope of Identifiers/Variables An identifier/variable eists within its scope if declared within a function the scope is within the function ( local variable ) Implications: for a variable declared within a function can't be accessed outside that function ( local ) can re-use the identifier in multiple functions Be careful, as this can create confusion with variables with the same name Remember What happens in Vegas, stays in Vegas. 30 Eample of Scope // 7 copied to fun:n // fun:n = 3; // returns 3 Animation of scope // :n set to 7 // fun called // 3 copied to result 31 8

result result 9

fun fun fun result result n 3 result = 3 n 3 result 10

fun = 3 n 3 result = 3 result result 3 result 3 11

Eample of Multiple Parameters int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); Animation of Multipe Parameters parameters copied in left to right order 45 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); 12

sum3 sum3 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); 4 y 5 z 6 int sum3 (int, int y, int z) return + y + z; = 15 int = sum3(4,5,6); 4 y 5 z 6 13

sum3 int sum3 (int, int y, int z) return + y + z; = 15 int = sum3(4,5,6); 4 y 5 z 6 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); = 15 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); = 15 15 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); 15 14

Parameter Type Correspondence Param and argument types needn't match as long as they are "assignment compatible" Eample: double power (double, int n); result = power(5, 2); // 5 cast to 5.0 Returning result = power(5.3, 2.9); // 2.9 cast to 2 57 58 Multiple Returns A function can have multiple return statements Eample: a function that returns ma value int ma(int, int y) if ( > y) return ; return y; 59 No Return Eample: print n blank lines void skiplines(int n) if (n < 1) return; // ok to have this for (int i = 1; i < n; i++) printf("\n"); return; // optional to have this multiple returns OK for a command function 60 15

Style: Returning Bool Functions that return bool should be named: is<something> Produces more readable code Eample: if (isperfectsquare())... isperfectsquare bool isperfectsquare(int ) if ((int)sqrt()*(int)sqrt() == ) return true; else return false; Having multiple returns is bad style for a queryfunction 61 62 isperfectsquare (cont'd) bool isperfectsquare(int ) bool result = false; if ((int)sqrt()*(int)sqrt() == ) result = true; return result; better style---can we be more concise? isperfectsquare (cont'd) bool isperfectsquare(int ) return (int)sqrt()*(int)sqrt() == ; bool isperfectsquare(int ) int sqrt_of_ = (int) sqrt(); return sqrt_of_ * sqrt_of_ == ; 63 64 16