Introduction to Programming

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Computer Science 1620 Function Scope & Global Variables.
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
Introduction to Computer Algorithmics and Programming Ceng 113 Variables and Operators in C.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
Functions g g Data Flow g Scope local global part 4 part 4.
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Chapter 2. Variable and Data type
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
C++ Lesson 1.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Test 2 Review Outline.
Chapter 7: User-Defined Functions II
Exercise 1 – Datentypen & Variablen
Functions and Structured Programming
Functions and an Introduction to Recursion
A Lecture for the c++ Course
Student Book An Introduction
Global & Local Identifiers
DATA HANDLING.
User-defined Functions
Chapter 9 Scope, Lifetime, and More on Functions
Conversions of the type of the value of an expression
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Stack Memory 2 (also called Call Stack)
FUNCTIONS& FUNCTIONS OVERLOADING
אבני היסוד של תוכנית ב- C++
אבני היסוד של תוכנית ב- C++
User-defined Functions
CS150 Introduction to Computer Science 1
Anatomy of a Function Part 1
Local Variables, Global Variables and Variable Scope
Reference Parameters.
Default Arguments.
Local, Global Variables, and Scope
Functions and an Introduction to Recursion
Overloading functions
Scope of variables class scopeofvars {
2. Second Step for Learning C++ Programming • Data Type • Char • Float
CS150 Introduction to Computer Science 1
Programming Language C Language.
CS150 Introduction to Computer Science 1
Predefined Functions Revisited
C++ Programming Basics
Names of variables, functions, classes
Module 2 Variables, Data Types and Arithmetic
CS1201: Programming Language 2
Anatomy of a Function Part 1
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
C Language B. DHIVYA 17PCA140 II MCA.
Parameters and Arguments
Scope Rules.
Presentation transcript:

Introduction to Programming

Expressions There are five atomic data types in C: char, int, float, double, and void! Type Typical Size in Bits Minimal Range char 8 −127 to 127 unsigned char 8 0 to 255 signed char 8 −127 to 127 int 16 or 32 −32,767 to 32,767 unsigned int 16 or 32 0 to 65,535 signed int 16 or 32 −32,767 to 32,767 short int 16 −32,767 to 32,767 unsigned short int 16 0 to 65,535 signed short int 16 −32,767 to 32,767 long int 32 −2,147,483,647 to 2,147,483,647 signed long int 32 same as long int unsigned long int 32 0 to 4,294,967,295 Float 32 Six digits of precision double 64 Ten digits of precision long double 80 Ten digits of precision

Variables Variables will be declared in three basic places: inside functions, in the definition of function parameters, and outside of all functions. Local Variables Formal Parameters Global Variables

Local Variables Variables that are declared inside a function, (automatic variables). Local variables are not known outside their own code block. What is a block?! {…}! The most common code blockFunctions. So see an example!

An example! #include <iostream> using namespace std; void f(); int main() { int i; for(i=0; i<10; i++) f(); return 0; } void f(void){ int j = 10; cout<<j<<endl; j++; Now what happened?!Let’s see…

Formal Parameters The function argumentsbehave like any other local variables inside the function. int f(int array[], int size){…} Keep in mind that : Even though these variables receive the value of the arguments passed to the function, you can use them like any other local variable!

Global Variables Unlike local variables, global variables are known throughout the program and may be used by any piece of code. Declared outside of all functions! What happened If a global variable and a local variable have the same name! Let’s see!

Another example! #include <iostream> int count; /* count is global */ void func1(); Void func2(); int main(){ count = 100; func1(); return 0; } void func1(){ func2(); cout<<“count is :”<<count<<endl; void func2(){ int count; for(count=1; count<10; count++) cout<<“Salam!”

Access Modifiers There are two modifiers that control how variables may be accessed or modified. const volatile! Variables of type const may not be changed by your program. A const variable can be given an initial value, however! const int a=10; Now who can say what is the profit of const variables?!

Let’s see a benefit of const! protect the objects pointed to by the arguments to a function from being modified by that function. int mathFunc(const int operation, int a, int b){ if(operation == 0) return a – b; else if(operation == 1) return a + b; else operation++;//!!! }

Any question? Thank you!