Functions. Chapter 5

Similar documents
Functions and Recursion

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

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

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

CSE 230 Intermediate Programming in C and C++ Functions

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

C: How to Program. Week /Apr/16

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

Data Structures And Algorithms

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

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

Functions. Systems Programming Concepts

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

1.7 Recursion. Department of CSE

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

Lecture 10: Recursive Functions. Computer System and programming in C 1

CSE123. Program Design and Modular Programming Functions 1-1

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

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

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

Storage class in C. Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables.

Memory Allocation in C

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

Dynamic memory allocation

EC 413 Computer Organization

LESSON 6 FLOW OF CONTROL

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

The C language. Introductory course #1

Unit 7. Functions. Need of User Defined Functions

Structured Programming. Functions and Structured Programming. Functions. Variables

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

15 FUNCTIONS IN C 15.1 INTRODUCTION

CSC 1600 Memory Layout for Unix Processes"

C Introduction. Comparison w/ Java, Memory Model, and Pointers

OBJECTIVE QUESTIONS: Choose the correct alternative:

Scope: Global and Local. Concept of Scope of Variable

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

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

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Lecture 04 FUNCTIONS AND ARRAYS

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

Lecture 3. More About C

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Variation of Pointers

Class Information ANNOUCEMENTS

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

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Function Call Stack and Activation Records

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

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

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Topic 6: A Quick Intro To C

Kurt Schmidt. October 30, 2018

Procedural programming with C

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Flow Control. CSC215 Lecture

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

EE 368. Weeks 4 (Notes)

Fundamental of Programming (C)

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

High Performance Programming Programming in C part 1

Lecture 9 Assertions and Error Handling CS240

CMPE-013/L. Introduction to C Programming. Unit testing

Subject: Fundamental of Computer Programming 2068

CSCI 171 Chapter Outlines

JTSK Programming in C II C-Lab II. Lecture 3 & 4

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Introduction to C. Systems Programming Concepts

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

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Pace University. Fundamental Concepts of CS121 1


Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Arrays and Pointers in C. Alan L. Cox

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C: How to Program. Week /Apr/23

Functions in C C Programming and Software Tools

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

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

Chapter 17 Recursion

ECEN 449 Microprocessor System Design. Review of C Programming

Chapter 3 - Functions

Data Types. Data Types. Integer Types. Signed Integers

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Fundamental of Programming (C)

Programming in C - Part 2

Functions BCA-105. Few Facts About Functions:

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

Programming Languages

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

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

Transcription:

Functions Chapter 5

Function Definition type function_name ( parameter list ) declarations statements For example int factorial(int n) int i, product = 1; for (i = 2; I <= n; ++i) product *= i; return product;

void print_course(void) printf("%s\n%s\n%s\n%s\n%s\n\n", " *************************", " ***Tel Aviv University***", " *** Computer Sciences ***", " *** Programming Proj. ***", " ************************* ) Usage: print_course()

The return statement The return statement may or may not include an expression: return expression; return; For example return; return ++a; return (a * b); The return statement terminates the execution of the function There can be more than one return statement in a function. double abs_value( double x ) if ( x >= 0.0 ) return x; else return -x;

The return statement Type is converted, if necessary to the type of the function. For example float f(char a) int i;... return i; /* converted to float */

Function Prototypes Function should be declared before used. A function prototype tells the compiler the type of the value returned by the function as well as the number and types of its parameters. type function_name ( parameter type list ) For example: double sqrt( double ); Identifiers are optional, and have no affect on the prototype. So void f(char c, int i); is equivalent to: void f(char, int);

Functions Example /* create a table of powers */ #include <stdio.h> #define N 7 /* prototypes */ long power( int, int ); void prn_heading( void ); void prn_tbl_of_powers( int ); int main(void) prn_heading(); prn_tbl_of_powers( N ); return 0;

Functions Example void prn_heading( void ) printf( \n::::: A TABLE OF POWERS :::::\n\n ); long power( int m, int n) int i = 0; long product = 1; for ( i = 1; i <= n; ++i ) product *= m; return product;

Functions Example void prn_tbl_of_powers( int n ) int i = 0, j = 0; for ( i = 1; i <= n; ++i ) for ( j = 1; j <= n; ++j ) if ( j == 1 ) printf( %ld, power( i, j ) ); else printf( %9ld, power( i, j ) ); putchar( \n ); ::::: A TABLE OF POWERS ::::: 1 2 3... 1 4 9 1 8 27 1 16 81 1 32 243 1 64 729 1 128 2187

An alternate style for function definition #include <stdio.h> #define N 7 long power(int m, int n)... void prn_heading( void )... void prn_tbl_of_powers(int n)... printf( %ld, power( i, j ) );... int main(void) prn_heading(); prn_tbl_of_powers( N ); return 0;

Function Invocation and Call by Value #include <stdio.h> int compute_sum( int ) int main(void) int n = 3, sum = 0; printf( "%d\n", n ); sum = compute_sum( n ); printf("%d\n", n ); printf("%d\n", sum ); return 0; /*sum integers from 1:n */ int compute_sum( int n ) int sum = 0; for ( ; n > 0; --n ) sum += n; return sum;

Developing large programs pgm.h #includes #defines list of prototypes main.c fct.c prn.c #include pgm.h #include pgm.h #include pgm.h

main.c #include "pgm.h" int main( void ) char ans = 0; int i = 0, n = N; printf( %s, This program does not do very much.\n Do you want more information? ); scanf( %c, &ans ); if ( ans == y ans == Y ) prn_info( pgm ); for ( i = 0; i < n; ++i ) fct1( i ); printf( Bye!\n ); return 0;

pgm.h #include <stdio.h> #include <stdlib.h> #define N 3 void fct1(int k); void fct2(void); void prn_info(char *);

#include "pgm.h" fct.c void fct1( int n ) int i = 0; printf("hello from fct1()\n" ); for ( i = 0; i < n; ++i ) fct2(); void fct2( void ) printf( "Hello from fct2()\n" );

prn.c #include "pgm.h" void prn_info( char *pgm_name ) printf( Usage: %s\n\n, pgm_name ); printf( %s\n, "This program illustrates how one can write a program\n "in more than one file. In this example, we have a\n "single.h file that gets included at the top of our\n "three.c files. Thus the.h file acts as the \ glue\ \n "that binds the program together.\n "\n" "Note that the functions fct1() and fct2() when called\n "only say \ hello.\ Writing a serious program, the\n "programmer sometimes does this in a first working\n "version of the code.\n );

Compiling and linking gcc c main.c fct.c wrt.c gcc -o pgm.exe main.o fct.o wrt.o We will learn in the sequel to handle such projects using the make utility

Assertions A macro that helps to ensure that the value of an expression is what it is supposed to be. #include <assert.h> #include <stdio.h> int f( int a, int b ); int main( void ) int a = 0, b = 0, c = 0;... scanf( %d%d, &a, &b );... c = f( a,b ); assert( c > 0 ); /* an assertion */...

Assertions int f( int a, int b )... assert( a == 1 a == -1 ); assert( b >= 7 && b <== 11 );...

Scope Rules Each block introduces its own identifiers. An outer block definition is valid unless an inner block redefines it Inner blocks may be nested to arbitrary depths, limited by system limitations. int a = 2, b = 2, c = 3; printf( "%d\n", a ); /* 2 */ int a = 5; printf("%d\n", a); /* 5 */ printf( %d\n", ++a); /* 3 */

Scope Rules - example int a = 1, b = 2, c = 3; printf ( "%3d%3d%3d\n", a, b, c ); int b = 4; float c = 5.0; printf( "%3d%3d%5.1f\n", a, b, c); a = b; int c; c = b; printf ( "%3d%3d%3d\n", a, b, c ); printf( "%3d%3d%5.1f\n", a, b, c ); printf( "%3d%3d%3d\n", a, b, c );

Storage Classes auto extern register static

auto This is the default storage class. Can be made explicit (but seldom done so): auto int a,b,c; auto float f;

extern External variables are used for transmitting information across blocks and functions. When a variable is declared outside a function, storage is permanently assigned to it, and its storage class is extern. Variables defined outside a function have external storage type even if not explicitly stated as extern. Some old C compilers complain when extern is used.

extern - example #include <stdio.h> int a = 1, b = 2, c = 3; /* global variables */ int f(void); int main(void) printf( "%3d\n", f() ); printf( "%3d%3d%3d\n", a, b, c ); return 0; int f( void ) int b = 0, c = 0; /* b and c are local!! */ /* global b and c are masked */ a = b = c = 4; return (a + b + c);

extern example- cont. file1.c #include <stdio.h> int a = 1, b = 2, c = 3; /* global variables */ int f(void); int main(void) printf( "%3d\n", f() ); printf( "%3d%3d%3d\n", a, b, c ); return 0; file2.c int f( void ) extern int a; /* look for it elsewhere */ int b = 0, c = 0; /* b and c are local!! */ /* global b and c are masked */ a = b = c = 4; return (a + b + c);

register Storage class register tells the compiler that the associated variables are to be stored in high speed memory registers. register int i; for ( i = 0; i < LIMIT; ++i )... /* block exit will free the register */

static The main use of static is to allow a local variable to retain its previous value when the block is reentered. void f(void) static int cnt = 0; ++cnt; if ( cnt % 2 == 0 )... /* do something */ else... /* do something else */

Static external variables This is a more subtle use of static. It allows privacy mechanism for external variables. Static external variables are scope-restricted external variables: Their scope is restricted ONLY to the remainder of the file in which they are defined. void f(void)... /* v is not available here */ static int v; void g(void)... /* v is available here */

#define INITIAL_SEED 17 #define MULTIPLIER 25173 #define INCREMENT 13849 #define MODULUS 65536 #define FLOATING_MODULUS 65536.0 static unsigned seed = INITIAL_SEED; /*external, but private to this file */ unsigned random( void ) seed = (MULTIPLIER * seed + INCREMENT) % MODULUS; return seed; double probability(void) seed = ( MULTIPLIER * seed + INCREMENT ) % MODULUS; return ( seed / FLOATING_MODULUS );

Recursion A function is said to be recursive if it calls itself, directly or indirectly. In C all functions can be used recursively. int factorial ( int n ) /* reucrsive version */ if ( n <= 1 ) return 1; else return ( n * factorial( n 1 ) ); int factorial( int n ) /* iterative version */ int product = 1; for( ; n > 1; --n ) product *= n; return product;

write a line backwards #include <stdio.h> void wrt_it(void); int main(void) printf("input a line: " ); wrt_it(); printf("\n\n" ); return 0; void wrt_it(void) int c = 0; if ( ( c = getchar() )!= \n ) wrt_it(); putchar( c );

Recursive Power double power( double val, unsigned int pow ) if ( pow == 0 ) return( 1.0 ); /* pow(x,0) returns 1 */ else return( power( val, pow 1 ) * val );

Fibonacci Value of a Number int fib(int n) if( n <= 1 ) return n; else return(fib(n 1) + fib(n 2)); Value of n 1 2 4 8 16 32 Number of invocations of fib 1 3 9 67 3193 7049155

Iterative (Non Recursive) Solution int fib(int n) int i, fn, fn1 = 0, fn2 = 1; if( n == 0 n == 1) return n; else for ( i = 2; i <= n; ++i ) fn = fn1 + fn2; fn1 = fn2; fn2 = fn; return fn

Top Down Recursion /* Outputs an integer comma separated */ void int_comma( int num ) if ( num < 1000 ) else printf( %3d, num ); int_comma( num / 1000 ); printf(,%03d, num % 1000 );

Recursion Underpinning Every instance of a function execution (call) creates an Activation Record, (frame) for the function. Activation records hold required execution information for functions: Parameters Return memory address, (calling instruction address) Dynamic link - pointer to activation record of calling function Return value for the function Local variables

Runtime Stack fnc() activation Activation records are are created record and and stored in in an an area area of of memory termed the the runtime stack. fnc() activation record return value actual parameters control link local data temporaries return value actual parameters control link local data temporaries main() activation record return value actual parameters control link local data temporaries

Storage Organization The program stack grows each time a function call is made. Infinite recursion results in a collision between the runtime stack and the heap termed a runtime stack overflow error. Illegal pointer de-references (garbage, dangling-references) often result in memory references outside the operating system allocated partition, (segment) for the C program resulting in a segmentation error (GPF - access violation) and core dump. Typical Typical C program execution memory model model System Privileges (not accessible to the C program) Binary Code Static Data Runtime Stack Heap Function activation record management Dynamic memory structure management

Example int x; /* static storage */ void main() int y; /* dynamic stack storage */ char *str; /* dynamic stack storage */ str = malloc(10); /* dynamic heap storage */ y = foo(23); free(str); /* deallocates heap storage */ /* y and str deallocated when stack frame is popped */ int foo(int z) /* z is dynamic stack storage */ char ch[100]; /* ch is dynamic stack storage */ if (z == 23) foo(7); return 3;

Last example: Hanoi Towers Move the disks from peg A to peg C, so that no disk lyes on a smaller disk A B C

Hanoi Towers cont. #include "hanoi.h" /* has all includes, externs and function prototypes*/ int cnt = 0; /* count the number of moves */ int main(void) int n; /* number of disks */ scanf("%d", &n); assert(n > 0); move(n, 'A', 'B', 'C'); /* recursive fct */ return 0;

Hanoi Towers cont. #include "hanoi.h" void move(int n, char a, char b, char c) if (n == 1) ++cnt; printf("%5d: %s%d%s%c%s%c.\n", cnt, "Move disk ",1, " from tower ",a, " to tower ",c); else move(n - 1, a, c, b); ++cnt; printf("%5d: %s%d%s%c%s%c.\n", cnt, "Move disk ",n, " from tower ", a, " to tower ",c); move(n - 1, b, a, c);