Writing Functions in C

Similar documents
Heap Arrays and Linked Lists. Steven R. Bagley

Heap Arrays. Steven R. Bagley

Singly linked lists in C.

Recitation 2/18/2012

CS201- Introduction to Programming Current Quizzes

CS201 Some Important Definitions

Basic Pointers. CSCI 112: Programming in C

C Programming Basics II

SYSC 2006 C Winter 2012

Pointers, Dynamic Data, and Reference Types

CS349/SE382 A1 C Programming Tutorial

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

Kurt Schmidt. October 30, 2018

COMP 524 Spring 2018 Midterm Thursday, March 1

CSC C69: OPERATING SYSTEMS

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Chapter 1 Getting Started

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Binghamton University. CS-211 Fall Functions. The Basis of C

QUIZ. What are 3 differences between C and C++ const variables?

Binghamton University. CS-211 Fall Dynamic Memory

CS61C : Machine Structures

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Ch. 11: References & the Copy-Constructor. - continued -

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Exercise: Inventing Language

Pointers. Reference operator (&) ted = &andy;

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Memory Management I. two kinds of memory: stack and heap

Week 3 Lecture 2. Types Constants and Variables

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

EL2310 Scientific Programming

CA31-1K DIS. Pointers. TA: You Lu

What goes inside when you declare a variable?

Welcome! COMP s1. Programming Fundamentals

Short Notes of CS201

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

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

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

However, in C we can group related variables together into something called a struct.

CS201 - Introduction to Programming Glossary By

Dynamic memory. EECS 211 Winter 2019

Dynamic Data Structures. CSCI 112: Programming in C

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

CSCI 350: Getting Started with C Written by: Stephen Tsung-Han Sher June 12, 2016

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

VARIABLES AND TYPES CITS1001

Introduction to C++ with content from

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

CSE409, Rob Johnson, Alin Tomescu, November 11 th, 2011 Buffer overflow defenses

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

CS240: Programming in C

Goals of this Lecture

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CA341 - Comparative Programming Languages

CSE 303: Concepts and Tools for Software Development

Lecture 16 CSE July 1992

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Structures, Unions, and Typedefs

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Cache and Virtual Memory Simulations

LSN 3 C Concepts for OS Programming

Introduction to C: Pointers

Fall 2018 Discussion 2: September 3, 2018

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

Structures and Pointers

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

G Programming Languages - Fall 2012

Programs in memory. The layout of memory is roughly:

QUIZ. What is wrong with this code that uses default arguments?

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Lecture 20 C s Memory Model

Object Oriented Programming. Solved MCQs - Part 2

Object-Oriented Principles and Practice / C++

Memory and Pointers written by Cathy Saxton

In Java we have the keyword null, which is the value of an uninitialized reference type

1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended)

CS61C Midterm Review on C & Memory Management

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Lecture Notes on Types in C

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Lecture 20 Notes C s Memory Model

Chapter 19 Data Structures

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSE351 Winter 2016, Final Examination March 16, 2016

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Run-time Environments

Run-time Environments

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

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

Algorithms & Data Structures

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Transcription:

Writing Functions in C 1

Test 2, Problem 5 b. Write a function to allocate space for a new instance of your structure, as defined in part a. Write the C code for a function to get space from the heap using malloc for an instance of your structure, and return a pointer to that memory. You do not need to initialize any of the values in the structure. struct vehicle { int time; enum lne { northbound=0,southbound=1} lane; enum wgt { compact=1, midsize=2, } weight; }; 2

Step 0: Get the Big Picture What is your function supposed to do? Who is it doing this for? How will your function get called? How will it s return value be used? 3

Step 0: Get the Big Picture What is your function supposed to do? Allocate space for a new instance of the vehicle structure Who is it doing this for? Program to monitor bridge traffic How will your function get called? Whenever the monitor returns info about a vehicle How will it s return value be used? Program will fill in the structure and manage it 4

Step 1: Choose a Name What are you going to call the function? Name must follow the rules Start with a letter Contains letters and numbers and underscores Not a keyword Not another function or variable name Choose a name that is easy to use Easy to remember Short to type 5

Step 1: Choose a Name What are you going to call the function? makevehicle 6

Step 2: Identify Argument List What data will your function need to work? What data is your caller willing and able to provide to you? What is the data type of each argument? What name will you use for each argument? (Note as your write your function, the answers to these questions may change you may have to add or remove items from your argument list but you should at least make a first pass at what is in the argument list up front.) 7

Step 2: Identify Argument List What data will your function need to work? Nothing as long as I know the definition of the vehicle structure, I can allocate space for that structure. My caller specifically said that I don t have to fill in any of the fields in the structure (If I did, they would have to be arguments.) What data is your caller willing and able to provide to you? Nothing.. makevehicle() or makevehicle(void) 8

Step 3: Identify Type of the Return Value First, what will this function return? Second, what is the data type of this value? 9

Step 3: Identify Type of the Return Value First, what will this function return? A pointer to an instance of structure vehicle Second, what is the data type of this value? struct vehicle * struct vehicle * makevehicle() 10

After step 3 You have a complete function prototype struct vehicle * makevehicle() Use the prototype to declare the function: struct vehicle * makevehicle(); Use the prototype to define the function: struct vehicle * makevehicle() { /* This is where the actual C implementation goes */ } 11

Step 4: Implement the Function What C instructions and local variables are needed to perform the function? Is there more than one way to do this? If so, which is the easiest/ most straightforward way? 12

Step 4: Implement the Function What C instructions and local variables are needed to perform the function? Call malloc to get heap memory, return the result, cast to the right type. Is there more than one way to do this? In this case, other than minor variations, probably not. If so, which is the easiest/ most straightforward way? struct vehicle * makevehicle() { return (struct vehicle *) malloc(sizeof(struct vehicle)); } 13

Step 5: Test Your Function Need the higher level code to call your function. Need to think about what could go wrong in your function. Need to come up with ways to try out your function and evaluate whether it is working correctly. 14

New Problem Write a function to count the number of times a specific letter (passed as the first argument) occurs in a null delimited text string (passed as the second argument). Return the number of occurances of the letter in the string. 15

Step 0: Get the Big Picture What is your function supposed to do? Who is it doing this for? How will your function get called? How will it s return value be used? 16

Step 0: Get the Big Picture What is your function supposed to do? Count the number of times a letter occurs in a string Who is it doing this for?? How will your function get called? x=func( e, This is a test ); How will it s return value be used?? 17

Step 1: Choose a Name What are you going to call the function? Name must follow the rules Start with a letter Contains letters and numbers and underscores Not a keyword Not another function or variable name Choose a name that is easy to use Easy to remember Short to type 18

Step 1: Choose a Name What are you going to call the function? howmany Name must follow the rules Start with a letter Contains letters and numbers and underscores Not a keyword Not another function or variable name Choose a name that is easy to use Easy to remember Short to type 19

Step 2: Identify Argument List What data will your function need to work? What data is your caller willing and able to provide to you? What is the data type of each argument? What name will you use for each argument? (Note as your write your function, the answers to these questions may change you may have to add or remove items from your argument list but you should at least make a first pass at what is in the argument list up front.) 20

Step 2: Identify Argument List What data will your function need to work? 1) a letter, and 2) a string What data is your caller willing and able to provide to you? Same as above What is the data type of each argument? 1) char and 2) char * What name will you use for each argument? 1) letter and 2) string. howmany(char letter, char *string) 21

Step 3: Identify Type of the Return Value First, what will this function return? Second, what is the data type of this value? 22

Step 3: Identify Type of the Return Value First, what will this function return? The number of occurances of the letter in the string Second, what is the data type of this value? int 23

Step 4: Implement the Function What C instructions and local variables are needed to perform the function? Is there more than one way to do this? If so, which is the easiest/ most straightforward way? 24

Step 4: Implement the Function What C instructions and local variables are needed to perform the function? Need to walk through the string, checking each character to see if it matches letter. Need a variable to index into the string. Need a variable to count the number of hits. Is there more than one way to do this? For sure e.g. pointers and vectors If so, which is the easiest/ most straightforward way? Let s use vectors 25

Example of Implementation int howmany(char letter, char *string) { int answer=0; int j; for(j=0;j<strlen(string);j++) { if (letter==string[j]) answer++; } return answer; } 26

Resources Programming in C, Chapter 7 C Functions Tutorial: http://www.tutorialspoint.com/cprogramming/c_functions.htm C functions You Tube Tutorial: https://www.youtube.com/watch?v=ios5spivuja 27