FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Computer Programming w/ Eng. Applications
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 14: Overloading and Templates
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Basic Elements of C++ Chapter 2.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Computer Science 210 Computer Organization Introduction to C.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
Chapter 2: Using Data.
(continue) © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Data Types Declarations Expressions Data storage C++ Basics.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 4.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Windows Programming Lecture 03. Pointers and Arrays.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
LESSON 06.
Chapter Topics The Basics of a C++ Program Data Types
Computer Science 210 Computer Organization
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Basic Elements of C++.
Pointers and Pointer-Based Strings
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Java Programming: From Problem Analysis to Program Design, 4e
DATA HANDLING.
Basic Elements of C++ Chapter 2.
Chapter 9: Pointers.
Computer Science 210 Computer Organization
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Expressions and Assignment
Pointers and Pointer-Based Strings
Primitive Types and Expressions
Module 2 Variables, Data Types and Arithmetic
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

FUNCTIONS (CONT)

Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating point constants, by default, denote float type values. 24. Like variables, constants have a type. 25. Character constants are coded using double quotes. 26. Initialization is the process of assigning a value to a variable at the time of declaration. 27. The scanf function can be used to read only one value at a time. 28. All arithmetic operators have the same level of precedence. 29. The modulus operator % can be used only with integers. 30. The operators =, and != are all the same level of priority.

Midterm questions (31-40) 31. During modulo division, the sign of the result is positive, if both the operands are of the same sign. 32. In C, if a data item is zero, it is considered false. 33. The expression !(x y. 34. A unary expression consists of only one operand with no operators. 35. Associativity is used to decide which of several different expressions is evaluated first. 36. An expression statement is terminated with a period. 37. During the evaluation of mixed expressions, an implicit cast is generated automatically. 38. An explicit cast can be used to change the expression. 39. Parentheses can be used to change the order of evaluation expressions. 40. When if statements are nested, the last else gets associated with the nearest if without an else.

The keyword void is a data type in C ? It's considered a data type (for organizational purposes), but it is basically a keyword to use as a placeholder where you would put a data type, to represent "no data". Hence, you can declare a routine which does not return a value as: void MyRoutine(); But, you cannot declare a variable like this : void bad_variable; However, when used as a pointer, then it has a different meaning: void* vague_pointer; this declare a pointer, but without specifying what data type it is pointing to.

Floating point constants, by default, denote float type values. To confirm this statement the following program is implemented: So, by default all floating point variables are of type double

During modulo division, the sign of the result is positive, if both the operands are of the same sign.

Compiling Programs with Two or More Source Code Files The simplest approach to using several functions is to place them in the same file. When functions are too long it is better sort them in groups and save in separate files; Suppose that file1.c and file2.c are two files containing C functions. Linux –gcc file1.c file2.c –In addition, two object files called file1.o and file2.o are produced. –If you change file1.c you don’t need to recompile file2.c –gcc file1.c file2.o DOS Command-Line Compilers Similar to Linux, object files have.obj extension. a.out

Compiling Programs with Two or More Source Code Files Windows and Macintosh Compilers are project oriented. When you create a file you should include all your source code files (the ones with the.c extension) in the project. Microsoft Visual C++ Using header files If you put main() in one file and your function definitions in a second file, the first file still needs the function prototypes. Rather than type them in each time you use the function file, you can store the function prototypes in a header file. Make the #define directives available to each file. To do so, place the #define directives in a header file and then use the #include directive in each source code file.

The usehotel.c control module The hotel.h header file

The hotel.c Function Support Module The values of code are checked only after it is determined that scanf() succeeded in reading an integer value

Finding Addresses: The & Operator scanf() uses addresses & for arguments. The unary & operator gives you the address where a variable is stored. Any C function that modifies a value in the calling function without using a return value uses addresses! %p is the specifier for addresses 24 0B76will be printed out on the screen The address where pooh is stored is 0B76 You can think of the address as a location in memory!

loccheck.c -- checks to see where variables are stored the two poohs and bahs have different addresses just the value was transferred

Altering Variables in the Calling Function Suppose you have two variables called x and y and you want to swap their values: temp = x; x = y; y = temp; Nothing changed! Why?

Declaring Pointers A pointer is a variable (or, more generally, a data object) whose value is a memory address. int * pi; /* pi is a pointer to an integer variable */ char * pc; /* pc is a pointer to a character variable */ float * pf, * pg; /* pf, pg are pointers to float variables */ The declaration float* sunmass; says that feet is a pointer and that *sunmass is type float i.e. the value occupies 4 bytes starting from address float* sunmass; Memory can be represented as series of bytes Machine address that pointer contains 4 bytes

Pointers ptr = &pooh; /* assigns pooh's address to ptr */ ptr = &bah; /* make ptr point to bah instead of to pooh */ Now you can use the indirection operator (dereferencing operator) * to find the value stored in bah. val = *ptr; /* finding the value ptr points to */ The statements ptr = &bah; val = *ptr; taken together amount to the following statement: val = bah;

Using Pointers to Communicate Between Functions The program that uses pointers to make the interchange() function work. pointers Get memory address of variables

Review questions 1. Write C function headings for the following functions. (Just write the headings, not the functions’ bodies). donut() takes an int argument and prints that number of 0s. gear() takes two int arguments and returns type int. stuff_it() takes a double and the address of a double variable and stores the first value in the given location. n_to_char() takes and int argument and returns a char. digits() take a double argument and an int argument and returns an int. random() takes no argument and returns an int. 2. Devise a function called alter() that take two int variables, x and y, and changes their values to their sum and their difference, respectively.