CSCI 2132 Software Development. Lecture 18: Functions

Similar documents
CSCI 2132 Software Development. Lecture 17: Functions and Recursion

CSCI 2132 Software Development. Lecture 20: Program Organization

CSCI 2132 Software Development. Lecture 19: Generating Permutations

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

CSCI 2132 Software Development Lecture 18: Implementation of Recursive Algorithms

BSM540 Basics of C Language

CSCI 2132 Software Development. Lecture 14: Software Development Life Cycle

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

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

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?

Functions. Arash Rafiey. September 26, 2017

CSC 270 Survey of Programming Languages. What is a Pointer?

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

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

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

Computer Programming: C++

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

Lecture 9 - C Functions

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

Principles of Programming. Chapter 6: Arrays

We first learn one useful option of gcc. Copy the following C source file to your

Functions BCA-105. Few Facts About Functions:

CSCI 2132 Software Development Lecture 35: Dynamically Allocated Arrays, Course Review

Tutorial 5. Call Stack and Stack Frames. Memory Addresses and Pointers. Content vs Address of Pointers. scanf() function. Perils of Pointers

Chapter 7 Functions. Now consider a more advanced example:

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Subject: Fundamental of Computer Programming 2068

C Programming Language

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

Programming Studio #9 ECE 190

Solutions to Assessment

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

Functions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay

Computer Science & Engineering 150A Problem Solving Using Computers

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

25.2 Opening and Closing a File

Fundamental of Programming (C)

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

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

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

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

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

Lesson 7. Reading and Writing a.k.a. Input and Output

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

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

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

CS 108 Computing Fundamentals. October/November Array Bootcamp

A brief intro to pointers for the purposes of passing reference parameters

ESc101: Pointers and Arrays

Chapter 9: Functions. Chapter 9. Functions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Arrays and Applications

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

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

Tutorial 6. Memory Addresses and Pointers. scanf() function. Perils of Pointers. Function Pointers. Declarations vs. Definitions

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Unit 7. Functions. Need of User Defined Functions

Recursion. Data and File Structures Laboratory. DFS Lab (ISI) Recursion 1 / 20

Dynamic memory allocation

Code No: R Set No. 1

Pointers, Arrays and Parameters

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

Array Initialization

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

Functions. Functions. Identify Repeated Code. Identify Repeated Code. Identify Similar Code. Use Parameters to Customize 2/25/14

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

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

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

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

Recursion. Data and File Structures Laboratory. DFS Lab (ISI) Recursion 1 / 27

CSE 230 Intermediate Programming in C and C++ Functions

Introduction: The Unix shell and C programming

Binding and Variables

Computer Programming

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

The Compilation Process

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

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

Welcome! COMP s1. Programming Fundamentals

Lecture 3: C Programm

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

(Refer Slide Time 01:41 min)

Course Information and Introduction

DECLARAING AND INITIALIZING POINTERS

AMCAT Automata Coding Sample Questions And Answers

Memory Allocation in C

ESC101N: Fundamentals of Computing End-sem st semester

Solution for Data Structure

C Programming Lecture V

Procedural Programming

C programming basics T3-1 -

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Chapter 4 Functions By C.K. Liang

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Transcription:

CSCI 2132 Software Development Lecture 18: Functions Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 18-Oct-2017 (18) CSCI 2132 1

Previous Lecture Example: binary search Multidimensional arrays Variable-length arrays 18-Oct-2017 (18) CSCI 2132 2

Example: latin.c n n matrix is a Latin square if each row is a permutation of {1, 2,..., n each column is a permutation of {1, 2,..., n Examples: 1 2 3 is Latin 1 2 3 is not Latin 2 3 1 square 3 1 2 square 3 1 2 1 3 2 Completed Sudoku puzzles are 9 9 Latin squares with some additional constraints 18-Oct-2017 (18) CSCI 2132 3

/* Program latin.c */ #include <stdio.h> int main() { int size = 0, i, j; Program latin.c printf("enter the size of the latin square: "); scanf("%d", &size); if (size < 1) return 1; int square[size][size], occurs[size+1]; printf("enter a matrix (%d by %d) of integers in " "the range [1-%d]: \n", size, size, size); 18-Oct-2017 (18) CSCI 2132 4

latin.c: Reading the matrix for (i = 0; i < size; i++) for (j = 0; j < size; j++) { scanf("%d", ); if (square[i][j] > size square[i][j] <= 0) { printf("error: element out of range.\n"); return 1; 18-Oct-2017 (18) CSCI 2132 5

latin.c: Checking Rows for (i = 0; i < size; i++) { for (j = 1; j <= size; j++) occurs[j] = 0; for (j = 0; j < size; j++) { if ( > 0) { printf("this is not a Latin square.\n"); return 1; else occurs[ ] = 1; 18-Oct-2017 (18) CSCI 2132 6

latin.c: Checking Columns for (j = 0; j < size; j++) { for (i = 1; i <= size; i++) occurs[i] = 0; for (i = 0; i < size; i++) { if ( > 0) { printf("this is not a Latin square.\n"); return 1; else occurs[ ] = 1; printf("this is a Latin square.\n"); return 0; 18-Oct-2017 (18) CSCI 2132 7

Functions The main code abstraction method: sequence of statements that can be called by a name; i.e., executed Functions have arguments and usually return a value, and that is where name comes from Function definition example int max(int a, int b) { int c; c = (a > b)? a : b; return c; 18-Oct-2017 (18) CSCI 2132 8

int by default Function Return Value Not necessary; can use: void An array cannot be return value Example of calling a function: printf("%d\n", max(a,b) ); Some known examples of functions: Return value of printf: number of printed characters Return value of scanf: number of converted values or, EOF if error or mismatch occurs before the first value is converted 18-Oct-2017 (18) CSCI 2132 9

Example with scanf if ( 2!= scanf("%d %d", &i, &j) ) { printf("invalid input\n"); return 1; 18-Oct-2017 (18) CSCI 2132 10

Function Declarations or Function Prototypes Used to declare function argument types and return type before defining the body of the function Needed if we call function in source code before being defined, so that compiler does not need to guess Syntax: return-type function-name(parameters); Example: int max(int a, int b); or: int max(int, int); 18-Oct-2017 (18) CSCI 2132 11

#include <stdio.h>; An Example int max(int a, int b); int main(void) { int a = 5, b = 4; printf("%d\n", max(a,b)); return 0; int max (int a, int b) { return (a < b)? a : b; 18-Oct-2017 (18) CSCI 2132 12

Arguments Arguments vs. parameters Similar terms and frequently used interchangeably Strictly speaking, they are different Arguments, or actual parameters: Expressions used in function call; e.g.: max( a, 3+(b-1)/2 ); Parameters, or formal parameters: Variables used in function definition; e.g.: int max( a, b ) { return (a < b)? a : b; 18-Oct-2017 (18) CSCI 2132 13

Arguments Passed by Value Arguments in programming languages can be passed: by value by reference and few other variations Arguments in C are passed by value Arrays behave in a special way they appear to be passed by reference however, they are still passed by value, but as pointers to be better explained later 18-Oct-2017 (18) CSCI 2132 14

Code Example void swap (int a, int b) { int temp = a; a = b; b = temp;... int a = 4; int b = 5; swap(a, b); printf("a=%d, b=%d\n", a, b); 18-Oct-2017 (18) CSCI 2132 15

Passing Array Parameters Functions cannot tell the length of an array parameter Usually need to pass array length as another parameter For example: int max_array(int, int []); int max_array(int len, int a[]) {... 18-Oct-2017 (18) CSCI 2132 16

Call Stack: Process Memory Partition What happens when a function is called? Remember memory partition: code, data, stack, heap Code: stores program code Data: stores static data; e.g., #include <stdio.h> int A[10][10], Asize; /* Static variables */ int main() { scanf("%d", &Asize); /* etc... */ return 0; 18-Oct-2017 (18) CSCI 2132 17

Use of Stack: Call Stack Have not seen yet how stack and heap are used Call stack is a stack in data structures sense Important in the context of function calling and local variables Call stack keeps information about active functions; i.e., functions being executed Whenever function starts execution: new stack frame is pushed on stack; also called activation record Contains information such as: arguments, local variables, return value, return instruction pointer, previous stack base address 18-Oct-2017 (18) CSCI 2132 18

Example Assume that function main calls function max The call stack would look as follows: top of the stack max main stack frame of function max stack frame of function main 18-Oct-2017 (18) CSCI 2132 19

Recursion Let us see what role the call stack plays in recursion Consider the following example: int power(int x, int n) { if (n == 0) /* Base case */ return 1; else /* Recursive case */ return x * power(x, n-1); Let us assume that power(3,4) is called from main 18-Oct-2017 (18) CSCI 2132 20

Call Stack: power(3,4) power(3,0) power(3,1) power(3,2) power(3,3) power(3,4) main Each call adds a new stack frame After each function return, a stack frame is popped 18-Oct-2017 (18) CSCI 2132 21