Week 4 Lecture 1. Expressions and Functions

Similar documents
Programming in C++ 5. Integral data types

Operators. Java operators are classified into three categories:

Fundamentals of Programming CS-110. Lecture 3

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Add Subtract Multiply Divide

Unit 3. Operators. School of Science and Technology INTRODUCTION

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Lecture 3 Tao Wang 1

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Operators & Expressions

Operators in C. Staff Incharge: S.Sasirekha

Computers Programming Course 6. Iulian Năstac

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Chapter 2: Using Data

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

UNIT- 3 Introduction to C++

Lecture 3. More About C

Department of Computer Science

CS201 Some Important Definitions

Declaration and Memory

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

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

Chapter 4: Basic C Operators

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Basics of Java Programming

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

CHIL CSS HTML Integrated Language

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Data Types and Variables in C language

COMP Primitive and Class Types. Yi Hong May 14, 2015

Chapter 12 Variables and Operators

Operators And Expressions

More Programming Constructs -- Introduction

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Information Science 1

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Full file at

2 nd Week Lecture Notes

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Chapter 2. Lexical Elements & Operators

Information Science 1

Fundamental of Programming (C)

LECTURE 3 C++ Basics Part 2

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

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

Expressions and Precedence. Last updated 12/10/18

Homework #3 CS2255 Fall 2012

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Variables and Operators 2/20/01 Lecture #

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

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Computer Programming CS F111

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Basics of Programming

BITG 1233: Introduction to C++

4. Inputting data or messages to a function is called passing data to the function.

Chapter 3 Structure of a C Program

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

University of Kelaniya Sri Lanka

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

UNIT 3 OPERATORS. [Marks- 12]

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

Java Primer 1: Types, Classes and Operators

A flow chart is a graphical or symbolic representation of a process.

These are reserved words of the C language. For example int, float, if, else, for, while etc.

SECTION II: LANGUAGE BASICS

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Programming with Java

CIS133J. Working with Numbers in Java

Chapter 3: Operators, Expressions and Type Conversion

Work relative to other classes

CHAD Language Reference Manual

Computer System and programming in C

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

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

ME 461 C review Session Fall 2009 S. Keres

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Variables and literals

Language Reference Manual simplicity

Lecture 3: C Programm

CT 229 Java Syntax Continued

CS110: PROGRAMMING LANGUAGE I

CS Kangmei Yang. Page 1

(2-1) Numeric Expressions in C H&K Chapter 2. Instructor - Andrew S. O Fallon CptS 121 (August 27, 2018) Washington State University

1 Lexical Considerations

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Transcription:

Lecture 1 Expressions and Functions

Expressions A representation of a value Expressions have a type Expressions have a value Examples 1 + 2: type int; value 3 1.2 + 3: type float; value 4.2 2

More expression examples If you declare two int variables: int a = 1; int b = 2; Expression with value 1: a Expression with value 2: a + 1 Expression with value 6: 2 * (a + b) Expression with value 2: ++a Expression with value 1: a++ Huh? 3

Operators Expressions comprise operations with variable or constants Examples 1+2 2 * (a + b) -1 + b 4

Arithmetic Operators Assignment In C = does not mean equals It means put the value on the right in the location on the left. Modulo (%) Int only %: remainder after int division Division (/) Int: the integer part of division e.g., 3/2 == 1 Float: the closes float to the result of the division (3.0/2 == 1.4) From Wikipedia: Operators in C and C++ 5

Division and modulus Integer division Integer modulus Float division 6

Increment and Decrement 7

Assignment Operators The value on the left of the '=' is treated as an Operat location or The value on the right is a = b a += b put in that location Meaning Simple assignment a=a+b a -= b a=a b a *= b a=a*b a /= b a=a/b a %= b a=a%b 8

Precedence Precedence Operator Associativity 1 (highest) ++ (suffix) (suffix) () [] Left-to-right 2 ++ (prefix) (prefix) + (unary) - (unary)! (<type>) * (value at) & (address of) Right-to-left 3 * / % Left-to-right 4 + - Left-to-right Week 4 9

Precedence Precedence Operator Associativity 5 < <= > >= Left-to-right 6 ==!= Left-to-right 7 && Left-to-right 8 Left-to-right 9?: Right-to-left 10 = += -= *= /= %= Right-to-left 10

Precedence Moral Always parenthesize your expressions. 11

Function Declaration <type> <name>(<parameter_list>); E.g., int add(int a, int b); Declares a function called add that returns an int when passed two ints. The first int passed will be called a inside the function; the second will be called b. The compiler knows it is a function declaration by the type, parentheses and semi-colon. Function declarations indicate the syntax of the function Functions must be declared before they are called. 12

Function Definition <type> <name>(<parameters>) { } E.g., int add (int a, int b) { return a + b; } Defines the function to return the sum of its two parameters. Body: { return a + b } The compiler can tell it is a function definition by the type, parentheses and curly brackets. Function definitions indicate what the function does. 13

Function Call <name>(<parameters>); E.g., add(2, 3); Executes the body of the function. Compiler recognized a function call because it has parentheses, but no type. A function call is an expression whose value is the return value. 14

Scope example 2 Parameter Local Return value 15

() Operator () is an operator. It is applied to an pointer. When you define a function, you define a name that points to a location in memory that contains executable code. When you call a function, you execute that code The value of the expression is the value the function returns. Style: the () operator goes immediately after the function name [i.e., no space; e.g., func()] 16

Functions are expressions This is important: function are like variables They can be used wherever variables are used Well almost: you cannot assign values to a function But you can assign functions to functions. E.g., Expression using (badly named) function f and g. (f() + 1) * 3) g() == f() (f() * g()) + 3 17

Expressions set function parameters E.g., int f(int a, int b); int g(int a); f(1+2, 3*4) f(g(1+2), 5) f(g(1+2), g(3*4) g(f(1,2)) 18

Variable lifetime Local variables disappear when the function returns. The keyword static gives the variable the same lifetime as a global variable Can return strings. Can share information between functions calls. 19

Static local variables (1) 20

Static local variables (2) We need to have the memory allocated before we can give it back to a calling function. By declaring the local variable static, the variable continues to live after the function is gone. The variable is inaccessible from main. 21

Important: good names == clarity 22

Clarity changes This program is clearer to an experienced programmer. It has less code and it easy to see that it does what it says. 23

Read Programs You learn to write English by reading English You learn to write Hindi by reading Hindi You learn to write C by reading C. You learn to write good C by reading good C. Look at Kernigan and Richie Look at the Linux kernel code 24