CS240: Programming in C

Similar documents
CS240: Programming in C

CS240: Programming in C

Lecture 3: C Programm

G Programming Languages - Fall 2012

Fundamentals of Programming Session 12

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Functions. Systems Programming Concepts

CS558 Programming Languages

PROGRAMMAZIONE I A.A. 2017/2018

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Run-time Environments - 2

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS-201 Introduction to Programming with Java

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Lecture 9 - C Functions

Computer Programming

CS61, Fall 2012 Section 2 Notes

CS 345. Functions. Vitaly Shmatikov. slide 1

Lecture 2: C Programm

Lexical Considerations

CSE 230 Intermediate Programming in C and C++ Functions

CS1150 Principles of Computer Science Methods

1 Lexical Considerations

Implementing Subprograms

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS111: PROGRAMMING LANGUAGE II

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

CSCI565 Compiler Design

Programming Languages Third Edition. Chapter 7 Basic Semantics

C Programming for Engineers Functions

Today's Topics. CISC 458 Winter J.R. Cordy

CS558 Programming Languages

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Short Notes of CS201

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CS201 - Introduction to Programming Glossary By

CS558 Programming Languages

G52CPP C++ Programming Lecture 9

A Fast Review of C Essentials Part II

Weeks 6&7: Procedures and Parameter Passing

CS3210: Tutorial Session 2. Kyuhong Park-- edited by Kyle Harrigan

A brief introduction to C programming for Java programmers

Variation of Pointers

OBJECTIVE QUESTIONS: Choose the correct alternative:

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

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Programming II (CS300)

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Overview COMP Microprocessors and Embedded Systems. Lectures 18 : Pointers & Arrays in C/ Assembly

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

ECE 15B COMPUTER ORGANIZATION

CSE 3302 Programming Languages Lecture 5: Control

Concepts Introduced in Chapter 7

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

CS1150 Principles of Computer Science Methods

Lexical Considerations

Contents. 8-1 Copyright (c) N. Afshartous

Function Call Stack and Activation Records

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Lecture 5: Methods CS2301

Programming for Engineers Iteration

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Guidelines for Writing C Code

Memory Allocation in C

Today s Learning Objectives

CSE 374 Programming Concepts & Tools

Name, Scope, and Binding. Outline [1]

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

Implementing Subprograms

FORM 2 (Please put your name and form # on the scantron!!!!)

Procedural programming with C

Chapter 9 Subprograms

Introduction to Programming (Java) 4/12

C - Func(on, Pointer, Array. Zhaoguo Wang

Dynamic memory allocation (malloc)

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Semantic Analysis. Lecture 9. February 7, 2018

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

Motivation was to facilitate development of systems software, especially OS development.

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

Object-Oriented Programming

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

Syntax Errors; Static Semantics

CS 314 Principles of Programming Languages. Lecture 13

A Fast Review of C Essentials Part I

CS 61c: Great Ideas in Computer Architecture

CS558 Programming Languages

Programming Languages: Lecture 12

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

Lecture 3. Variables. Variables

C Programming Language

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Transcription:

CS240: Programming in C Lecture 5: Functions. Scope. 1

Functions: Explicit declaration Declaration, definition, use, order matters. Declaration: defines the interface of a function; i.e., number and types of parameters, type of return value A C PROTOTYPE gives an explicit declaration void solve(int [], int, int); Prototypes improve safety and robustness Improved interaction with the type-checker 2

Functions: Implicit declaration First use of a function without a preceding prototype declaration implicitly declares the function If prototype follows the first use, error will say prototype is wrong Put prototypes of function at the beginning of the source file! 3

Functions: Definition DEFINITION gives the implementation of a function int my_strlen(char s[]) { int i = 0; while(s[i]!= \0 ) ++i; return i; Are functions a form of abstraction? The notion of an abstraction is a central concept in programming languages 4

int my_strlen(char* s) { int i = 0; while(*(s+i)!= \0 ) ++i; return i; 5

int my_strlen(char* s) { int i = 0; while(*(s+i)!= \0 ) ++i; return i; What does this signify? 5

Extern modifier Placed before a function declaration ensures that caller params are interpreted correctly extern void solve(int [], int, int); Actual function definition may be in another source file, but can also be later in the same file Often appears in header files, and included by callee and all the callers 6

Static modifier Placed before a function declaration or definition declares a local function static void solve(int [], int, int); Limits use / visibility of function to the local file Functions without static are global; i.e., visible to all other source files 7

Return statement return [(] [expression] [)]; Terminates the execution of a function and returns control to the calling function Parenthesis are optional Converted to declared return type Return without expression gives garbage if return type is not void Return value can be ignored by caller 8

Examples void my_printf() { if( ) return;. int min( int a, int b ) { return ( (a < b)? a : b ); 9

Variables: Declaration Declaration specifies type of variable extern modifier possible extern int fahr; Actual variable definition may be in another source file, although it can also be in the same file Unlike functions, variables cannot be used before declaration 10

Variables: Definition Definition allocates storage for the variable extern int i; // declaration extern char msg[]; // array declaration doesn t need dimension int i; // both declaration and definition int i = 10; // var definition can be initialized // but extern declarations should not char msg[100]; // array definition must have dimension There should be one and only one definition of a variable among all source files that make up a C Program 11

Static modifier If variable is not inside a block, means the scope of the variable is local to the source file If variable is inside a function, means variable is initialized only on first call, and survives across function calls; 12

Static modifier: Example int good_memory(void) { static int val = 10; printf("val %d\n", val++); int bad_memory(void) { int val = 10; printf("val %d\n", val++); What is the result of good_memory invoked twice? What about bad_memory? 13

Variables visibility { int i; printf( %d\n, i); Braces delimit blocks, variables declared within a block live only for the duration of the block Storage for i can be conceptually reclaimed after block exits registers stack 14

Parameter passing ALL C parameters are passed by value A callee s copy of the param is made on function entry, initialized to value passed from caller Updates of param inside callee made only to callee s copy Caller s copy is not changed (i.e., updates to param not visible after return) What are the implications of using call-by-value? Why did C not adopt a call-by-reference strategy? What does e.g., Java do? 15

What s wrong with this code? void swap(int a, int b){ int tmp; tmp = a; a = b; b = tmp; int x = 10; int y = 20; swap(x,y) 16

What s wrong with this code? void swap(int a, int b){ int tmp; tmp = a; x a = b; b = tmp; int x = 10; int y = 20; swap(x,y) a y 10 20 b 10 20 16

How does this fix the problem? void swap(int* a, int* b){ int tmp; x tmp = *a; *a = *b; *b = tmp; a y 10 20 b Call swap(&x,&y) for integers x and y 17

Fixing the problem Although caller s param can not be changed by the callee, what s "referenced" by the param can void swap2(int vec[]) { int tmp; tmp = vec[0]; vec[0] = vec[1]; vec[1] = tmp; int main() { int vec[2] = {10, 20; swap2(vec); return 0; 18

Readings for This Lecture K&R Chapter 4 19 20