Functions Fall 2015 Jinkyu Jeong

Size: px
Start display at page:

Download "Functions Fall 2015 Jinkyu Jeong"

Transcription

1 Functions Fall 2015 Jinkyu Jeong 1

2 Flow of Control Review Operators Relational Equality Logical short circuits Conditional operators if while for do while goto break & continue switch 2

3 Functions To avoid repetitive similar code To structure the whole program as top-down approach Breaking up a large problem into smaller pieces and break each piece into smaller ones until each piece is readily expressed in code Each piece should be concise and logical entity 3

4 Functions: Example type name parameter long power(int m, int n) header int i; long product = 1; for ( i=1; i <= n; ++i ) body product *= m; return product; 4

5 Function Definition Function type Type of the return value, if any void if nothing to return If missing, it is assumed to be int Parameters are placeholders for values that are passed to the function when it is invoked Return statements return; return a+b; return (a+b); void print_hello(void) printf( Hello world\n ); return; /* no effect */ printf( Helloooooooooo\n ); 5

6 Function Prototypes Each function should be declared before it is used What if your program structure is top down? Some people prefer bottom-up approaches Usually, they are placed before the main( ) function Parameter names can be omitted (ANSI C) Parameters can be omitted (old C) void f(char c, int i); void f(char, int); /* parameter names omitted */ void f(); /* old C */ 6

7 #include <stdio.h> #define N 7 /* function prototypes */ long power(int, int); void prn_heading(void); void prn_tbl_of_powers(int); /* function definitions */ int main(void) prn_heading(); prn_tbl_of_powers(n); return 0; void prn_heading(void) printf("\n::::: A TABLE OF POWERS :::::\n\n"); void prn_tbl_of_powers(int n) int i, j; 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'); long power(int m, int n) int i; long product = 1; for (i = 1; i <= n; ++i) product *= m; return product; 7

8 #include <stdio.h> #define N 7 /* function definitions */ long power(int m, int n) int i; long product = 1; for (i = 1; i <= n; ++i) product *= m; return product; void prn_heading(void) printf("\n::::: A TABLE OF POWERS :::::\n\n"); void prn_tbl_of_powers(int n) int i, j; 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'); int main(void) prn_heading(); prn_tbl_of_powers(n); return 0; 8

9 Function Invocation The program starts by invoking the main function Parameters are passed as call-by-value You can implement call-by-reference with pointers 9

10 #include <stdio.h> int main(void) int n = 3, sum, compute_sum(int); printf("%d\n", n); /* 3 is printed */ sum = compute_sum(n); printf("%d\n", n); /* 3 is printed */ printf("%d\n", sum); /* 6 is printed */ return 0; int compute_sum(int n) /* sum the integers from 1 to n */ int sum = 0; for ( ; n > 0; --n) /* stored value of n is changed */ sum += n; return sum; 10

11 Developing a Large Program Usually developed by several teams Comprises many.h and.c files each.c file can be compiled separately pgm.h #include #define list of function prototypes main.c fct.c wrt.c #include pgm.h #include pgm.h gcc o pgm main.c fct.c wrt.c #include pgm.h 11

12 Assertion Make sure a certain condition holds true at any place of program assert(expression); A macro defined in the header file assert.h If the value of the expression is zero abort the program int f(int a, int b) assert(a == 1 a == -1); assert(b >= 7 && b <= 11); 12

13 Scope Rules Identifiers are accessible only within the block where they are defined They are invisible from outside int a = 2; printf( %d\n, a); int a = 5; printf( %d\n, a); printf( %d\n, ++a);

14 Storage Classes Every variable and functions in C has two attributes: type and storage class Storage Classes auto extern register static 14

15 auto The most common class Variables defined inside a function Default class you may omit it int a(void) auto int a, b, c; /* equals int a, b, c; */ auto float f; /* equals float f; */ The memory space is allocated/released when the function is invoked/exited When a function is reentered, the previous values are unknown 15

16 extern (external) Global Variables defined outside a function May be defined somewhere else (in another file) Never disappear Transmit values across functions May be hidden by re-declaration, but they are not destroyed 16

17 #include <stdio.h> int a = 1, b = 2, c = 3; /* global (external omitted) */ 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, c; /* local (auto omitted)*/ a = b = c = 4; return (a + b + c); 17

18 #include <stdio.h> main.c int a = 1, b = 2, c = 3; /* external variables */ int f(void); int main(void) printf("%3d\n", f( ) ); printf("%3d%3d%3d\n", a, b, c); return 0; int f(void) extern int a; /* look for it elsewhere */ int b, c; $ gcc main.c fct.c a = b = c = 4; return (a + b + c); fct.c 18

19 register Allocate this variable on a register To speed up the execution Not always possible to find a register auto when fail to find a register Tricky for memory-io operations 19

20 static To preserve the value even after the function exits extern does the same To control visibility of variable and functions static extern - visible only within the same source file void f(void) static int cnt = 0; static int seed = 100; /* static extern external, but invisible from other files */ ++cnt; if (cnt %2 == 0)... else... int random(void) seed = * seed ;... 20

21 /* function g( ) can be seen only within this file */ static int g(void)... void f(int a)

22 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); int i=2; j++; printf("block : %d %d %d\n",i,j,g); 22

23 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 2 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); int i=2; j++; printf("block : %d %d %d\n",i,j,g); 23

24 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 2 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); main() i j int i=2; j++; printf("block : %d %d %d\n",i,j,g); 24

25 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 2 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); main() i: 0 j: 0 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 25

26 Scope by Example #include <stdio.h> int g = 2; /* global variable */ void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); program g: 2 func() i main() i: 0 j: 0 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 26

27 Scope by Example #include <stdio.h> int g = 2; /* global variable */ void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); program g: 2 func() i: 0 copy (call by value) main() i: 0 j: 0 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 27

28 Scope by Example #include <stdio.h> int g = 2; /* global variable */ void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); program g: 2 func() i: 0 j: 3 main() i: 0 j: 0 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 28

29 Scope by Example #include <stdio.h> int g = 2; /* global variable */ void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); program g: 2 func() i: 1 j: 3 main() i: 0 j: 0 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 29

30 1 3 2 Scope by Example #include <stdio.h> int g = 2; /* global variable */ void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); program g: 2 func() i: 1 j: 3 main() i: 0 j: 0 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 30

31 Scope by Example #include <stdio.h> int g = 2; /* global variable */ void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); program g: 3 func() i: 1 j: 3 main() i: 0 j: 0 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 31

32 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 3 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); main() i: 0 j: 0 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 32

33 0 0 3 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 3 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); main() i: 0 j: 0 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 33

34 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 3 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); int i=2; j++; printf("block : %d %d %d\n",i,j,g); main() i: 0 j: 0 anonymous block 34

35 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 3 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); int i=2; j++; printf("block : %d %d %d\n",i,j,g); main() i: 0 j: 0 anonymous block i: 2 35

36 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 3 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); int i=2; j++; printf("block : %d %d %d\n",i,j,g); main() i: 0 j: 0 anonymous block i: 2 36

37 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 3 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); int i=2; j++; printf("block : %d %d %d\n",i,j,g); main() i: 0 j: 1 anonymous block i: 2 37

38 2 1 3 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 3 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); int i=2; j++; printf("block : %d %d %d\n",i,j,g); main() i: 0 j: 1 anonymous block i: 2 38

39 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 3 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); main() i: 0 j: 1 int i=2; j++; printf("block : %d %d %d\n",i,j,g); 39

40 Scope by Example #include <stdio.h> int g = 2; /* global variable */ program g: 3 void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); int i=2; j++; printf("block : %d %d %d\n",i,j,g); 40

41 Scope by Example #include <stdio.h> int g = 2; /* global variable */ void func(int i) int j=3; i++; printf("func: %d %d %d\n",i,j,g); g++; int main() int i=0, j=0; func(i); printf("main : %d %d %d\n",i,j,g); int i=2; j++; printf("block : %d %d %d\n",i,j,g); 41

42 Recursion Recursion is said to be more elegant and requires fewer variables, but function calls are costly in time and space. 42

43 Recursion Fibonacci numbers f i+1 = f i + f i-1 int fib(int n) if (n <= 1) return n; else return fib(n-1) + fib(n-2); Exponential increase in function calls 43

44 Recursion Iterative version int f[n+1]; int fib(int n) f[0] = 0; f[1] = 1; for (i = 2; i <= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; 44

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

46 void wrt_it(void) int c; if ((c = getchar())!= '\n') wrt_it(); putchar(c); 46

Sources: On the Web: Slides will be available on:

Sources: On the Web: Slides will be available on: C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

More information

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution 16. Recursion COMP 110 Prasun Dewan 1 Loops are one mechanism for making a program execute a statement a variable number of times. Recursion offers an alternative mechanism, considered by many to be more

More information

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint) TN203 Porting a Program to Dynamic C Introduction Dynamic C has a number of improvements and differences compared to many other C compiler systems. This application note gives instructions and suggestions

More information

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority) Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the

More information

Chapter 13 Storage classes

Chapter 13 Storage classes Chapter 13 Storage classes 1. Storage classes 2. Storage Class auto 3. Storage Class extern 4. Storage Class static 5. Storage Class register 6. Global and Local Variables 7. Nested Blocks with the Same

More information

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

More information

The C Programming Language course syllabus associate level

The C Programming Language course syllabus associate level TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

More information

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

KITES TECHNOLOGY COURSE MODULE (C, C++, DS) KITES TECHNOLOGY 360 Degree Solution www.kitestechnology.com/academy.php info@kitestechnology.com technologykites@gmail.com Contact: - 8961334776 9433759247 9830639522.NET JAVA WEB DESIGN PHP SQL, PL/SQL

More information

13 Classes & Objects with Constructors/Destructors

13 Classes & Objects with Constructors/Destructors 13 Classes & Objects with Constructors/Destructors 13.1 Introduction In object oriented programming, the emphasis is on data rather than function. Class is a way that binds the data & function together.

More information

Senem Kumova Metin & Ilker Korkmaz 1

Senem Kumova Metin & Ilker Korkmaz 1 Senem Kumova Metin & Ilker Korkmaz 1 A loop is a block of code that can be performed repeatedly. A loop is controlled by a condition that is checked each time through the loop. C supports two categories

More information

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; } Functions Recursion C++ functions Declare/prototype int myfunction (int ); Define int myfunction (int x){ int y = x*x; return y; Call int a; a = myfunction (7); function call flow types type of function

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)

More information

Tutorial on C Language Programming

Tutorial on C Language Programming Tutorial on C Language Programming Teodor Rus rus@cs.uiowa.edu The University of Iowa, Department of Computer Science Introduction to System Software p.1/64 Tutorial on C programming C program structure:

More information

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer About The Tutorial C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system.

More information

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions Phys4051: C Lecture 2 & 3 Functions (Review) Comment Statements Variables & Operators Branching Instructions Comment Statements! Method 1: /* */! Method 2: // /* Single Line */ //Single Line /* This comment

More information

Data Structures. Algorithm Performance and Big O Analysis

Data Structures. Algorithm Performance and Big O Analysis Data Structures Algorithm Performance and Big O Analysis What s an Algorithm? a clearly specified set of instructions to be followed to solve a problem. In essence: A computer program. In detail: Defined

More information

The programming language C. sws1 1

The programming language C. sws1 1 The programming language C sws1 1 The programming language C invented by Dennis Ritchie in early 1970s who used it to write the first Hello World program C was used to write UNIX Standardised as K&C (Kernighan

More information

CP Lab 2: Writing programs for simple arithmetic problems

CP Lab 2: Writing programs for simple arithmetic problems Computer Programming (CP) Lab 2, 2015/16 1 CP Lab 2: Writing programs for simple arithmetic problems Instructions The purpose of this Lab is to guide you through a series of simple programming problems,

More information

C++ INTERVIEW QUESTIONS

C++ INTERVIEW QUESTIONS C++ INTERVIEW QUESTIONS http://www.tutorialspoint.com/cplusplus/cpp_interview_questions.htm Copyright tutorialspoint.com Dear readers, these C++ Interview Questions have been designed specially to get

More information

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is preceded by an equal sign d. its name has undereline 2. Associations

More information

How To Write A Programming Program

How To Write A Programming Program 1 Preface... 6 Preface to the first edition...8 Chapter 1 - A Tutorial Introduction...9 1.1 Getting Started...9 1.2 Variables and Arithmetic Expressions...11 1.3 The for statement...15 1.4 Symbolic Constants...17

More information

arrays C Programming Language - Arrays

arrays C Programming Language - Arrays arrays So far, we have been using only scalar variables scalar meaning a variable with a single value But many things require a set of related values coordinates or vectors require 3 (or 2, or 4, or more)

More information

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas CS 110B - Rule Storage Classes Page 18-1 Attributes are distinctive features of a variable. Data type, int or double for example, is an attribute. Storage class is another attribute. There are four storage

More information

FEEG6002 - Applied Programming 5 - Tutorial Session

FEEG6002 - Applied Programming 5 - Tutorial Session FEEG6002 - Applied Programming 5 - Tutorial Session Sam Sinayoko 2015-10-30 1 / 38 Outline Objectives Two common bugs General comments on style String formatting Questions? Summary 2 / 38 Objectives Revise

More information

Computer Programming Tutorial

Computer Programming Tutorial Computer Programming Tutorial COMPUTER PROGRAMMING TUTORIAL by tutorialspoint.com tutorialspoint.com i ABOUT THE TUTORIAL Computer Prgramming Tutorial Computer programming is the act of writing computer

More information

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement? 1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members

More information

What Is Recursion? Recursion. Binary search example postponed to end of lecture

What Is Recursion? Recursion. Binary search example postponed to end of lecture Recursion Binary search example postponed to end of lecture What Is Recursion? Recursive call A method call in which the method being called is the same as the one making the call Direct recursion Recursion

More information

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1 Definition Course: Programming II - Abstract Data Types The ADT Stack The ADT Stack is a linear sequence of an arbitrary number of items, together with access procedures. The access procedures permit insertions

More information

Using the RDTSC Instruction for Performance Monitoring

Using the RDTSC Instruction for Performance Monitoring Using the Instruction for Performance Monitoring http://developer.intel.com/drg/pentiumii/appnotes/pm1.htm Using the Instruction for Performance Monitoring Information in this document is provided in connection

More information

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void 1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of

More information

Chapter 13 - The Preprocessor

Chapter 13 - The Preprocessor Chapter 13 - The Preprocessor Outline 13.1 Introduction 13.2 The#include Preprocessor Directive 13.3 The#define Preprocessor Directive: Symbolic Constants 13.4 The#define Preprocessor Directive: Macros

More information

System Calls and Standard I/O

System Calls and Standard I/O System Calls and Standard I/O Professor Jennifer Rexford http://www.cs.princeton.edu/~jrex 1 Goals of Today s Class System calls o How a user process contacts the Operating System o For advanced services

More information

The Plan Today... System Calls and API's Basics of OS design Virtual Machines

The Plan Today... System Calls and API's Basics of OS design Virtual Machines System Calls + The Plan Today... System Calls and API's Basics of OS design Virtual Machines System Calls System programs interact with the OS (and ultimately hardware) through system calls. Called when

More information

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms. COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms 1 Activation Records activation declaration location Recall that an activation

More information

Chapter 5 Functions. Introducing Functions

Chapter 5 Functions. Introducing Functions Chapter 5 Functions 1 Introducing Functions A function is a collection of statements that are grouped together to perform an operation Define a function Invoke a funciton return value type method name

More information

C++FA 5.1 PRACTICE MID-TERM EXAM

C++FA 5.1 PRACTICE MID-TERM EXAM C++FA 5.1 PRACTICE MID-TERM EXAM This practicemid-term exam covers sections C++FA 1.1 through C++FA 1.4 of C++ with Financial Applications by Ben Van Vliet, available at www.benvanvliet.net. 1.) A pointer

More information

6.S096 Lecture 1 Introduction to C

6.S096 Lecture 1 Introduction to C 6.S096 Lecture 1 Introduction to C Welcome to the Memory Jungle Andre Kessler Andre Kessler 6.S096 Lecture 1 Introduction to C 1 / 30 Outline 1 Motivation 2 Class Logistics 3 Memory Model 4 Compiling 5

More information

An Incomplete C++ Primer. University of Wyoming MA 5310

An Incomplete C++ Primer. University of Wyoming MA 5310 An Incomplete C++ Primer University of Wyoming MA 5310 Professor Craig C. Douglas http://www.mgnet.org/~douglas/classes/na-sc/notes/c++primer.pdf C++ is a legacy programming language, as is other languages

More information

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

More information

Simple C Programs. Goals for this Lecture. Help you learn about:

Simple C Programs. Goals for this Lecture. Help you learn about: Simple C Programs 1 Goals for this Lecture Help you learn about: Simple C programs Program structure Defining symbolic constants Detecting and reporting failure Functionality of the gcc command Preprocessor,

More information

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall-2015 1

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall-2015 1 QUIZ-II Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. (For

More information

Introduction. Earlier programs structured as methods that call one another in a disciplined, hierarchical manner Recursive methods

Introduction. Earlier programs structured as methods that call one another in a disciplined, hierarchical manner Recursive methods Recursion 1 2 Introduction Earlier programs structured as methods that call one another in a disciplined, hierarchical manner Recursive methods Call themselves Useful for some problems to define a method

More information

64-Bit NASM Notes. Invoking 64-Bit NASM

64-Bit NASM Notes. Invoking 64-Bit NASM 64-Bit NASM Notes The transition from 32- to 64-bit architectures is no joke, as anyone who has wrestled with 32/64 bit incompatibilities will attest We note here some key differences between 32- and 64-bit

More information

C PROGRAMMING FOR MATHEMATICAL COMPUTING

C PROGRAMMING FOR MATHEMATICAL COMPUTING UNIVERSITY OF CALICUT SCHOOL OF DISTANCE EDUCATION BSc MATHEMATICS (2011 Admission Onwards) VI Semester Elective Course C PROGRAMMING FOR MATHEMATICAL COMPUTING QUESTION BANK Multiple Choice Questions

More information

C++ Programming Language

C++ Programming Language C++ Programming Language Lecturer: Yuri Nefedov 7th and 8th semesters Lectures: 34 hours (7th semester); 32 hours (8th semester). Seminars: 34 hours (7th semester); 32 hours (8th semester). Course abstract

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Real Application Design Christian Nastasi http://retis.sssup.it/~lipari http://retis.sssup.it/~chris/cpp Scuola Superiore Sant Anna Pisa April 25, 2012 C. Nastasi (Scuola

More information

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015 PRI-(BASIC2) Table of content Introduction...2 New Comment...2 Long variable...2 Function definition...3 Function declaration...3 Function return value...3 Keyword return inside functions...4 Function

More information

Storage and linkage specifiers

Storage and linkage specifiers Storage and linkage specifiers P. Ronchese Dipartimento di Fisica e Astronomia G.Galilei Università di Padova Object oriented programming and C++ course Object oriented programming and C++ Storage and

More information

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result Parameter Passing Standard mechanisms Call by value Call by reference Other methods Call by value-result Call by name, result Terms Function definition where the details of the function are presented (type,

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java

More information

Assignment 09. Problem statement : Write a Embedded C program to switch-on/switch-off LED.

Assignment 09. Problem statement : Write a Embedded C program to switch-on/switch-off LED. Assignment 09 Problem statement : Write a Embedded C program to switch-on/switch-off LED. Learning Objective: -> To study embedded programming concepts -> To study LCD control functions -> How output is

More information

/* File: blkcopy.c. size_t n

/* File: blkcopy.c. size_t n 13.1. BLOCK INPUT/OUTPUT 505 /* File: blkcopy.c The program uses block I/O to copy a file. */ #include main() { signed char buf[100] const void *ptr = (void *) buf FILE *input, *output size_t

More information

Know or Go Practical Quest for Reliable Software

Know or Go Practical Quest for Reliable Software Know or Go Practical Quest for Reliable Software Dr.-Ing. Jörg Barrho Dr.-Ing. Ulrich Wünsche AVACS Project meeting 25.09.2014 2014 Rolls-Royce Power Systems AG The information in this document is the

More information

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements 9 Control Statements 9.1 Introduction The normal flow of execution in a high level language is sequential, i.e., each statement is executed in the order of its appearance in the program. However, depending

More information

Stack Allocation. Run-Time Data Structures. Static Structures

Stack Allocation. Run-Time Data Structures. Static Structures Run-Time Data Structures Stack Allocation Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers,

More information

Building Embedded Systems

Building Embedded Systems All Rights Reserved. The contents of this document cannot be reproduced without prior permission of the authors. Building Embedded Systems Chapter 5: Maintenance and Debugging Andreas Knirsch andreas.knirsch@h-da.de

More information

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example Microcontroller Systems ELET 3232 Topic 8: Slot Machine Example 1 Agenda We will work through a complete example Use CodeVision and AVR Studio Discuss a few creative instructions Discuss #define and #include

More information

Section IV.1: Recursive Algorithms and Recursion Trees

Section IV.1: Recursive Algorithms and Recursion Trees Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller

More information

6.s096. Introduction to C and C++

6.s096. Introduction to C and C++ 6.s096 Introduction to C and C++ 1 Why? 2 1 You seek performance 3 1 You seek performance zero-overhead principle 4 2 You seek to interface directly with hardware 5 3 That s kinda it 6 C a nice way to

More information

Introduction to Scientific Computing Part II: C and C++ C. David Sherrill School of Chemistry and Biochemistry Georgia Institute of Technology

Introduction to Scientific Computing Part II: C and C++ C. David Sherrill School of Chemistry and Biochemistry Georgia Institute of Technology Introduction to Scientific Computing Part II: C and C++ C. David Sherrill School of Chemistry and Biochemistry Georgia Institute of Technology The C Programming Language: Low-level operators Created by

More information

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu C / C++ and Unix Programming Materials adapted from Dan Hood and Dianna Xu 1 C and Unix Programming Today s goals ú History of C ú Basic types ú printf ú Arithmetic operations, types and casting ú Intro

More information

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Scope Scope and Lifetime Referencing Environments Named

More information

Tail call elimination. Michel Schinz

Tail call elimination. Michel Schinz Tail call elimination Michel Schinz Tail calls and their elimination Loops in functional languages Several functional programming languages do not have an explicit looping statement. Instead, programmers

More information

Semantic Analysis: Types and Type Checking

Semantic Analysis: Types and Type Checking Semantic Analysis Semantic Analysis: Types and Type Checking CS 471 October 10, 2007 Source code Lexical Analysis tokens Syntactic Analysis AST Semantic Analysis AST Intermediate Code Gen lexical errors

More information

Comp151. Definitions & Declarations

Comp151. Definitions & Declarations Comp151 Definitions & Declarations Example: Definition /* reverse_printcpp */ #include #include using namespace std; int global_var = 23; // global variable definition void reverse_print(const

More information

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00 MPLAB TM C30 Managed PSV Pointers Beta support included with MPLAB C30 V3.00 Contents 1 Overview 2 1.1 Why Beta?.............................. 2 1.2 Other Sources of Reference..................... 2 2

More information

Glossary of Object Oriented Terms

Glossary of Object Oriented Terms Appendix E Glossary of Object Oriented Terms abstract class: A class primarily intended to define an instance, but can not be instantiated without additional methods. abstract data type: An abstraction

More information

J a v a Quiz (Unit 3, Test 0 Practice)

J a v a Quiz (Unit 3, Test 0 Practice) Computer Science S-111a: Intensive Introduction to Computer Science Using Java Handout #11 Your Name Teaching Fellow J a v a Quiz (Unit 3, Test 0 Practice) Multiple-choice questions are worth 2 points

More information

C STYLE GUIDE AUGUST 1994 SOFTWARE ENGINEERING LABORATORY SERIES SEL-94-003. National Aeronautics and Space Administration

C STYLE GUIDE AUGUST 1994 SOFTWARE ENGINEERING LABORATORY SERIES SEL-94-003. National Aeronautics and Space Administration SOFTWARE ENGINEERING LABORATORY SERIES SEL-94-003 C STYLE GUIDE AUGUST 1994 National Aeronautics and Space Administration Goddard Space Flight Center Greenbelt, Maryland 20771 FOREWORD The Software Engineering

More information

Moving from CS 61A Scheme to CS 61B Java

Moving from CS 61A Scheme to CS 61B Java Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

More information

Pemrograman Dasar. Basic Elements Of Java

Pemrograman Dasar. Basic Elements Of Java Pemrograman Dasar Basic Elements Of Java Compiling and Running a Java Application 2 Portable Java Application 3 Java Platform Platform: hardware or software environment in which a program runs. Oracle

More information

Parallelization: Binary Tree Traversal

Parallelization: Binary Tree Traversal By Aaron Weeden and Patrick Royal Shodor Education Foundation, Inc. August 2012 Introduction: According to Moore s law, the number of transistors on a computer chip doubles roughly every two years. First

More information

Habanero Extreme Scale Software Research Project

Habanero Extreme Scale Software Research Project Habanero Extreme Scale Software Research Project Comp215: Java Method Dispatch Zoran Budimlić (Rice University) Always remember that you are absolutely unique. Just like everyone else. - Margaret Mead

More information

Selection Statements

Selection Statements Chapter 5 Selection Statements 1 Statements So far, we ve used return statements and expression ess statements. e ts. Most of C s remaining statements fall into three categories: Selection statements:

More information

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings: 9.1-9.

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings: 9.1-9. Code Generation I Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings: 9.1-9.7 Stack Machines A simple evaluation model No variables

More information

Parallel Computing. Shared memory parallel programming with OpenMP

Parallel Computing. Shared memory parallel programming with OpenMP Parallel Computing Shared memory parallel programming with OpenMP Thorsten Grahs, 27.04.2015 Table of contents Introduction Directives Scope of data Synchronization 27.04.2015 Thorsten Grahs Parallel Computing

More information

Unit 1. 5. Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Unit 1. 5. Write iterative and recursive C functions to find the greatest common divisor of two integers. [6] Unit 1 1. Write the following statements in C : [4] Print the address of a float variable P. Declare and initialize an array to four characters a,b,c,d. 2. Declare a pointer to a function f which accepts

More information

Introduction to Network Operating Systems

Introduction to Network Operating Systems As mentioned earlier, different layers of the protocol stack use different kinds of addresses. We can now see that the Transport Layer (TCP) uses port addresses to route data to the correct process, the

More information

1 Abstract Data Types Information Hiding

1 Abstract Data Types Information Hiding 1 1 Abstract Data Types Information Hiding 1.1 Data Types Data types are an integral part of every programming language. ANSI-C has int, double and char to name just a few. Programmers are rarely content

More information

Introduction to SPIN. Acknowledgments. Parts of the slides are based on an earlier lecture by Radu Iosif, Verimag. Ralf Huuck. Features PROMELA/SPIN

Introduction to SPIN. Acknowledgments. Parts of the slides are based on an earlier lecture by Radu Iosif, Verimag. Ralf Huuck. Features PROMELA/SPIN Acknowledgments Introduction to SPIN Parts of the slides are based on an earlier lecture by Radu Iosif, Verimag. Ralf Huuck Ralf Huuck COMP 4152 1 Ralf Huuck COMP 4152 2 PROMELA/SPIN PROMELA (PROcess MEta

More information

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system. http://www.tutorialspoint.com/java/java_quick_guide.htm JAVA - QUICK GUIDE Copyright tutorialspoint.com What is Java? Java is: Object Oriented Platform independent: Simple Secure Architectural- neutral

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Introduction to the C language Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) The C language

More information

System Calls Related to File Manipulation

System Calls Related to File Manipulation KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 12 System Calls Related to File Manipulation Objective: In this lab we will be

More information

csci 210: Data Structures Recursion

csci 210: Data Structures Recursion csci 210: Data Structures Recursion Summary Topics recursion overview simple examples Sierpinski gasket Hanoi towers Blob check READING: GT textbook chapter 3.5 Recursion In general, a method of defining

More information

Data Structure with C

Data Structure with C Subject: Data Structure with C Topic : Tree Tree A tree is a set of nodes that either:is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which

More information

The C Programming Language

The C Programming Language Chapter 1 The C Programming Language In this chapter we will learn how to write simple computer programs using the C programming language; perform basic mathematical calculations; manage data stored in

More information

C++ Overloading, Constructors, Assignment operator

C++ Overloading, Constructors, Assignment operator C++ Overloading, Constructors, Assignment operator 1 Overloading Before looking at the initialization of objects in C++ with constructors, we need to understand what function overloading is In C, two functions

More information

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved. 1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Programs and Methods Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Programs and Methods 1 / 8 The Anatomy of a Java Program It is customary

More information

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and: Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)

More information

Pseudo code Tutorial and Exercises Teacher s Version

Pseudo code Tutorial and Exercises Teacher s Version Pseudo code Tutorial and Exercises Teacher s Version Pseudo-code is an informal way to express the design of a computer program or an algorithm in 1.45. The aim is to get the idea quickly and also easy

More information

C Examples! Jennifer Rexford!

C Examples! Jennifer Rexford! C Examples! Jennifer Rexford! 1 Goals of this Lecture! Help you learn about:! The fundamentals of C! Deterministic finite state automata (DFA)! Expectations for programming assignments! Why?! The fundamentals

More information

60-141 Introduction to Programming II Winter, 2014 Assignment 2

60-141 Introduction to Programming II Winter, 2014 Assignment 2 60-141 Introduction to Programming II Winter, 2014 Assignment 2 Array In this assignment you will implement an encryption and a corresponding decryption algorithm which involves only random shuffling of

More information

Introduction to CUDA C

Introduction to CUDA C Introduction to CUDA C What is CUDA? CUDA Architecture Expose general-purpose GPU computing as first-class capability Retain traditional DirectX/OpenGL graphics performance CUDA C Based on industry-standard

More information

Computer Systems Architecture

Computer Systems Architecture Computer Systems Architecture http://cs.nott.ac.uk/ txa/g51csa/ Thorsten Altenkirch and Liyang Hu School of Computer Science University of Nottingham Lecture 10: MIPS Procedure Calling Convention and Recursion

More information

6. Control Structures

6. Control Structures - 35 - Control Structures: 6. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose,

More information

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 Oct 4, 2013, p 1 Name: CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 1. (max 18) 4. (max 16) 2. (max 12) 5. (max 12) 3. (max 24) 6. (max 18) Total: (max 100)

More information

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters The char Type ASCII Encoding The C char type stores small integers. It is usually 8 bits. char variables guaranteed to be able to hold integers 0.. +127. char variables mostly used to store characters

More information