Procedures, Parameters, Values and Variables. Steven R. Bagley

Similar documents
Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Presented By : Gaurav Juneja

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

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

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

C: How to Program. Week /Mar/05

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Syntax and Variables

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 2 - Introduction to C Programming

VARIABLES AND CONSTANTS

CS102: Variables and Expressions

A Fast Review of C Essentials Part I

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Basic Elements of C. Staff Incharge: S.Sasirekha

DEPARTMENT OF MATHS, MJ COLLEGE

CMPE-013/L. Introduction to C Programming

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Fundamentals of Programming

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Fundamental of Programming (C)

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Programming in C++ 4. The lexical basis of C++

BLM2031 Structured Programming. Zeyneb KURT

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Variables. Data Types.

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

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

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

Work relative to other classes

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Data types, variables, constants

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

C Language, Token, Keywords, Constant, variable

PROGRAMMAZIONE I A.A. 2018/2019

Pointers and scanf() Steven R. Bagley

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Data Types and Variables in C language

Variables and literals

COMP s1 Lecture 1

6.096 Introduction to C++ January (IAP) 2009

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Variables and Constants

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

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

Programming in C. Pointers and Arrays

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

ET156 Introduction to C Programming

Programming and Data Structures

EL2310 Scientific Programming

UEE1302 (1102) F10: Introduction to Computers and Programming

Chapter 1 & 2 Introduction to C Language

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

CSE 374 Programming Concepts & Tools

Kurt Schmidt. October 30, 2018

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Introduction to Computing Lecture 01: Introduction to C

Programming in C. What is C?... What is C?

Review of the C Programming Language for Principles of Operating Systems

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Chapter 2: Overview of C. Problem Solving & Program Design in C

Binghamton University. CS-211 Fall Variable Scope

Introduction to C# Applications

Binghamton University. CS-211 Fall Variable Scope

Programming. C++ Basics

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

3. Java - Language Constructs I

COMP 2355 Introduction to Systems Programming

Informática Ingeniería en Electrónica y Automática Industrial

C Programming Review CSC 4320/6320

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Lecture 02 C FUNDAMENTALS

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

An overview of Java, Data types and variables

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CS2141 Software Development using C/C++ C++ Basics

Review of the C Programming Language

ET156 Introduction to C Programming

Computer Components. Software{ User Programs. Operating System. Hardware

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Tokens, Expressions and Control Structures

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Creating, Compiling and Executing

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Transcription:

Procedures, Parameters, Values and Variables Steven R. Bagley

Recap A Program is a sequence of statements (instructions) Statements executed one-by-one in order Unless it is changed by the programmer e.g. by calling a function

/* Defines a function called PrintHello() */ void PrintHello() { printf( Hello World\n ); } int main(int argc, char *argv[]) { PrintHello(); /* Call PrintHello function */ } Note the semicolons -- they are significant void means doesn t return anything

Note the semicolons -- they are significant void means doesn t return anything IF they don t match, you ll get compile errors /* Declare function called PrintHello() exists */ void PrintHello(); These must match int main(int argc, char *argv[]) { PrintHello(); /* Call PrintHello function */ } /* Define PrintHello() function */ void PrintHello() { printf( Hello World\n ); }

Data Programs = Algorithms + Data Algorithms manipulate the data to perform their task In C, data is stored in variables (or in things access via variables) Algorithms in C are implemented by manipulating the values stored in variables Algorithm == the code we write. Several standard ones that are often used (e.g. several to sort things)

Variables Variables are a place to store some data Given a name that we can use to access it Usually very small amount of data (a number, or a character) Value can change (vary) as program runs In C anyway, some other languages have single assignment variables

Variables Some values are stored for a long time Player s position in a game Used and accessed in lots of places Other values are only needed for a short period To calculate temporary values

Average Algorithm sum = 0; count = 0; while(morevalues()) { sum = sum + nextvalue() count = count + 1 } average = sum / count; THIS IS NOT VALID C, this is psuedo code Only need sum and count while calculating the average -- after that it can be thrown away

Variable Type In C, variables have type This type defines what the variable can store C provides some basic types Possible to define our own complex types And specify where they are stored

C Types Type Stores Size char a single byte, holds one ASCII character 8 bits int An Integer 32 bits short An Integer 16 bits long An Integer 64 bits long long An Integer 64 bits float single-precision floating point (e.g. 3.141) 32 bits double double-precision floating point 64 bits Note size of highlighted types isn t fixed

size is number of bits they can store, which also means the size of the largest number they can store Windows has longs as 32bit Size and Signedness Integers in C can be both signed and unsigned prefix with unsigned to specify Compiler/OS defines size of integer types Not defined in the standard short <= int <= long Values in table as found on 64-bit Linux

Function Anatomy return-type function-name(parameter declarations) { declarations statements } Variables need to be declared in the declaration section

Declaring Variables Variables must be declared in the declaration section before use Declared by putting the type (e.g. int) Followed by the name (e.g. fred) but use something sensible Names must begin with a letter, followed by any number of letters or digits The underscore character _ is also classed as a letter

Reserved Keywords auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while Can t use any of these as names for a variable...

Naming Conventions C convention is to use lowercase variable names Use the underscore or camel case to improve readability: characterreadfromkeyboard character_read_from_keyboard characterreadfromkeyboard

Naming Conventions Use meaningful names Related to what the variable stores Shorter names for local variables or loop indices (i is often used for loops)

double celsius; /* Declares a variable called celsius as a double */ int count; /* Declares a variable called count as an int */ long long bignumber; /* Declares bignumber as a long long */ short smallnumber; /* Declares smallnumber as a short */ char c; /* Declares a variable called c as a char */

Assigning values The values stored in a variable is set using the assignment operator = Give the variable name, followed by the assignment operator, followed by the value Can be combined with the declaration to set an initial value

double celsius; /* Declares a variable called celsius as a double */ int count; /* Declares a variable called count as an int */ celsius = 32.5; /* sets the value of celsius to 32.5 */ count = 3; /* set count to 3 */ int newval = 4; /* Declares a new variable called newval with the initial value 4 */ celsius = 16.5; /* change the value in celsius to 16.5 */ If a new value is set to a variable then the old value is overwritten No way to get it back, unless you store it somewhere else.

Values A value assigned to a variable can be a number But it can also be expressed in terms of another variable or the return value from a function (providing the types match)

Values In C, values have type too 3 is an integer, 3.0 is a double This can cause some interesting side-effects double f = 3 / 2; What is the value of the variable f? f is 1.0000 (the division is between two integers, so divided as integers)

Values In C, values have type too 3 is an integer, 3.0 is a double This can cause some interesting side-effects double f = 3.0 / 2; Can also happen when using the value of variables f is 1.5000 (the division is between a double and an integer, so divided as floating-point)

Accessing Variables Value of a variable can be accessed by name Wherever the variable name is used, the value stored in it is looked up and used Can be used anywhere a value is allowed

Printing Variables Can check this using printf() printf will replace %d in the output string with the textual replacement of the integer And %f for floating-point values Type of parameter must match placeholder Demo using printf See http://en.wikipedia.org/wiki/printf for full list of placeholders

printf( The value is %d and %d\n, 12, 34); /* will printout: The value is 12 and 34 */ printf( This is a floating-point value %f, 3.14159); /* will printout: This is a floating-point value 3.14159 */ int foo = 42; printf( The value of foo is %d\n, foo); /* will printout: The value of foo is 42 */ printf interpolates the textual representation of the parameters in order for each placeholder (%d) very powerful function

Scope Variable is accessible for the function it is declared in Not accessible by anything else (even if called by the function it is declared in) If you need to pass a value to a function, it should be a parameter Variables are local to that function

Local Variables Each time a function is run it gets a new set of variables The values don t persist from one call to the next

Global Variables Exception are Global Variables These are defined at the same level as functions And are accessible anywhere in the program Must be declared before it is accessed

Global or Local What happens if a global and local variable have the same name? Local variable takes precedence that function won t be able to access the global variable

Parameters Parameters to functions work in exactly the same way as local variables Assigned a copy of value passed in when function called Parameter s value can be changed (like any local variable) Remember, this won t update the original

void PrintNumber(int number) { printf( Hello World\n ); } int main(int argc, char *argv[]) { /* Call PrintNumber function with the parameter number set to 42 */ PrintNumber(42); }

/* Defines a function called PrintHello() */ void PrintNumbers(int number, float f) { printf( %d %f\n, number, float); } int main(int argc, char *argv[]) { /* Call PrintHello function with the parameter number set to 42 and f set to 3.141*/ PrintNumbers(42,3.141); }

Return Value Type of return value is specified before function name Value returned using return keyword Can be anywhere in the function, it stops the execution

Using Return Value Can use it anywhere a value is expected Store in a variable Use as part of a mathematical expression Use as a parameter to another function