Programming for Engineers: Functions

Similar documents
COMP26120: Pointers in C (2018/19) Lucas Cordeiro

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

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

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

1.7 Recursion. Department of CSE

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

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

Intermediate Programming, Spring 2017*

Introduction to Programming (Java) 4/12

C Pointers. 7.2 Pointer Variable Definitions and Initialization

EC 413 Computer Organization

Computer Programming Lecture 12 Pointers

CSSE 332 Standard Library, Storage classes, and Make

A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions.

Subject: Fundamental of Computer Programming 2068

What is an algorithm?

Chapter 4 Functions By C.K. Liang

Week 7: Pointers and Arrays. BJ Furman 02OCT2009

CS 211: Recursion. Chris Kauffman. Week 13-1

Programming for Engineers Pointers

CS 211: Recursion. Chris Kauffman. Week 13-1

Tree traversals. Review: recursion Tree traversals. October 05, 2017 Cinda Heeren / Geoffrey Tien 1

Functions. Chapter 5

Lecture 14. Dynamic Memory Allocation

Functions in C C Programming and Software Tools

Functions and Recursion

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

Linked Data Representations. Data Structures and Programming Techniques

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Recursion. February 02, 2018 Cinda Heeren / Geoffrey Tien 1

CSE 142 Wi01 Midterm 2 page 1 of 6

C Pointers. 6th April 2017 Giulio Picierro

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

AN OVERVIEW OF C++ 1

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

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

Week 3: Pointers (Part 2)

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

Lecture 4. Default Arguments. Set defaults in function prototype. int myfunction( int x = 1, int y = 2, int z = 3 );

Chapter 17 Recursion

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Computer Programming Lecture 13 Functions with Multiple Output Parameters

Procedural programming with C

EK131 E5 Introduction to Engineering

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

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

Lecture 9 - C Functions

MODULE 3: Arrays, Functions and Strings

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Cpt S 122 Data Structures. Data Structures

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

CHAPTER 4 FUNCTIONS. 4.1 Introduction

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

Data Structures And Algorithms

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search

File IO and command line input CSE 2451

More Pointers Week 11

Chapter 7 Functions. Now consider a more advanced example:

Chapter 5 C Functions

Basic and Practice in Programming Lab7

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

Copyright The McGraw-Hill Companies, Inc. Persion required for reproduction or display. What is Recursion?

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

Functions BCA-105. Few Facts About Functions:

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Dynamic memory allocation

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

Recursion Enums. Basics of Programming 1. Department of Networked Systems and Services G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A.

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

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

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

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?

Dynamic Memory Allocation and Command-line Arguments

Lecture 12 Modular Programming with Functions

C Functions. Object created and destroyed within its block auto: default for local variables

CSCI 1103: Basics of Recursion

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

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

DECLARAING AND INITIALIZING POINTERS

Homework 2 Answers. Due Date: Monday, April 29, 2002, at 11:59PM Points: 100

Arrays and Pointers in C. Alan L. Cox

EE 368. Weeks 4 (Notes)

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

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Programming in C++: Assignment Week 1

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1

1 Recursion. 2 Recursive Algorithms. 2.1 Example: The Dictionary Search Problem. CSci 235 Software Design and Analysis II Introduction to Recursion

(T) x. Casts. A cast converts the value held in variable x to type T

Dynamic Memory Allocation

Lecture 23: Pointer Arithmetic

BSM540 Basics of C Language

Assembly Programming (III)

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

OBJECT ORIENTED PROGRAMMING USING C++

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Transcription:

Programming for Engineers: Functions 18 February 2011 23 February 2011 25 February 2011

Function Architecture Prototype Specifies the return type The function name ought to have mnemonic content Specifies the argument (N.O.T.) Number Order Type Names ought to have mnemonic content The Prototype catches errors and oversights Invocation Assigns return values Obeys N.O.T. but without type declaration Definition May contain local variables Uses the variables in an algorithm to calculate a return value

Example Function Prototype: void starbar(void); Invocation: starbar(); Definition: void starbar(void) { int count; for(count=1;count =WIDTH;count++) putchar(*); putchar( n); }

letterhead1.c #include <stdio.> #define NAME GIGATHINK, INC #define ADDRESS 101 Megabuck Plaza #define PLACE Megapolis, CA 94904 #define WIDTH 40 void starbar(void); /* prototype */

letterhead.c (cont d) int main(void) { starbar(); printf( %s n,name); printf( %s n,address); printf( %s n,place); starbar(); return 0; }

letterhead.c (cont d) void starbar(void) { int count; for(count=1;count =WIDTH;count++) putchar(*); putchar( n); } The output is **************************************** GIGATHINK, INC 101 Megabuck Plaza Megapolis, CA 94904 ****************************************

Generalizing and Centering the Letterhead We use a function to display any number of a character. We use a function to determine the number of spaces needed to center the name and address.

Generalizing and Centering the Letterhead (cont d) #include <stdio.> #define NAME GIGATHINK, INC #define ADDRESS 101 Megabuck Plaza #define PLACE Megapolis, CA 94904 #define WIDTH 40 #define SPACE void show n char(char ch,int num); /* prototype */ int spaces(int width,char *str);/* prototype */

Generalizing and Centering the Letterhead (cont d) int main(void) { show n char(space,spaces(width,name)); printf( %s n,name); show n char(space,spaces(width,address)); printf( %s n,address); show n char(space,spaces(width,place)); printf( %s n,place); } return 0;

Generalizing and Centering the Letterhead (cont d) int spaces(int width,char * str) { int temp; temp= (width-strlen(str))/2 return Temp; } void show n char(char ch,int num) { int count; for(count=1; count <= num; count++) putchar(ch); }

Other Argument Lists No Arguments int myfunc(void); Unspecified Arguments int printf(char *, ); Required Arguments first in list (char *) Optional Arguments as ellipsis ( )

Recursion Function that calls itself Only works with some algorithms not general Uses a lot of stack space Runs slow because of call and return overhead

Recursive Function Rules The function must return a value. The function must call itself, which starts the winding of the stack. The function must have tail recursion, which starts unwinding of stack. The argument and the return value must be the same type.

Classic Example of Recursion The growth of an idealized (biologically unrealistic) rabbit population, assuming that: in the first month there is just one newly-born pair, new-born pairs become fertile from after their second month, each month every fertile pair begets a new pair, and the rabbits never die Let the population at month n be F n. Then F 0 = 0 F 1 = 1 F n = F n 1 + F n 2

Sample Fibonacci Program unsigned int fib(intn) { switch(n) { case 0: return 0; //special tail recursion case 1: case 2: return 1; //normal tail recursion default: return fib(n-1)+fib(n-2); } }

Recursion Pros and Cons Pros For some algorithms recursion is simplest solution Cons May exhaust memory (stack space) Hard to understand and maintain Call and return overhead

Place multiple source files in the same file Control Module Support Modules Header files Use the #include Include prototypes for support functions in the header file

#include #include < > looks in standard system directories #include looks in current working directory and then standard system directories

letter.h #define NAME GIGATHINK, INC #define ADDRESS 101 Megabuck Plaza #define PLACE Megapolis, CA 94904 #define WIDTH 40 #define SPACE void show n char(char ch,int num); /* prototype */ int spaces(int width,char *str);/* prototype */

letterheadmultiple.c #include <stdio.h> #include letter.h #include letterfun.c int main(void) { show n char(space,spaces(width,name)); printf( %s n,name); show n char(space,spaces(width,address)); printf( %s n,address); show n char(space,spaces(width,place)); printf( %s n,place); } return 0;

letterfun.c #include <stdio.h> #include letter.h int spaces(int width,char * str) { int temp; temp= (width-strlen(str))/2 return Temp; } void show n char(char ch,int num) { int count; for(count=1; count <= num; count++) putchar(ch); }

: A First Loook & operator retrieves address of data item * operator gets data indirectly using address Example bla = -6789; // @ 0x1234 ptr = &bla; //ptr = 0x1234 not -6789 ph = * ptr; //ph = -6789

void swapviadata(int x, int y) { int temp; temp = x; x=y; y=temp; } Initially we have x = 5 y = 13. After swapviadata we have x = 5 y = 13.

void swapviaptr(int * xptr, int * yptr) { int temp; temp = *xptr; *xptr =*yptr; *yptr=temp; } Initially we have After swapviaptr we have x = 5 y = 13. x = 13 y = 5.

Declare variable as pointer use indirect operator void swapviaptr(int * x, int * y) To get address of variable use address operator swapviaptr(&x,&y); To get data given ptr use indirection operator temp = *xptr; *xptr=*yptr; *yptr=temp;

ACLU #9 1. Translate the spaces function to a pointer notation function. 2. Write factorial as a recursive function. Write the tail recursion. Write the recursive call.

Programming Assignment Complete Chapter 9 Exercise 4 Complete Chapter 9 Exercise 5 Complete Chapter 9 Exercise 10 Due Date: 02 March 2011