AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Similar documents
Functions and Recursion

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Fundamentals of Programming Session 4

C: How to Program. Week /Mar/05

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Programming for Engineers Introduction to C

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Chapter 7 Functions. Now consider a more advanced example:

Programming and Data Structure

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

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

Chapter 2 - Introduction to C Programming

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

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

Lecture 2: C Programming Basic

Functions. Systems Programming Concepts

CSE 230 Intermediate Programming in C and C++ Functions

Unit 7. Functions. Need of User Defined Functions

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

AMCAT Automata Coding Sample Questions And Answers

Full file at C How to Program, 6/e Multiple Choice Test Bank

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Chapter 2, Part I Introduction to C Programming

Lecture 04 FUNCTIONS AND ARRAYS

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

C Functions. 5.2 Program Modules in C

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

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

Typescript on LLVM Language Reference Manual

UNIT- 3 Introduction to C++

Functions. (transfer of parameters, returned values, recursion, function pointers).

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Computer Science & Engineering 150A Problem Solving Using Computers

BSM540 Basics of C Language

Course Outline Introduction to C-Programming

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

C: How to Program. Week /Apr/23


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

EL2310 Scientific Programming

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

CS16 Exam #1 7/17/ Minutes 100 Points total

ANSI C Programming Simple Programs

Getting started with C++ (Part 2)

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

CSE123. Program Design and Modular Programming Functions 1-1

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Lecture 2 Tao Wang 1

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Preview from Notesale.co.uk Page 6 of 52

Chapter 1 & 2 Introduction to C Language

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

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

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Introduction to C Language

Data Types and Variables in C language

2. Numbers In, Numbers Out

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Fundamental of Programming (C)

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

Flow Control. CSC215 Lecture

Lecture 12 Modular Programming with Functions

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

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

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Subject: Fundamental of Computer Programming 2068

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

CHAD Language Reference Manual

Program Organization and Comments

Lexical Considerations

Chapter 4. Flow of Control

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Computer System and programming in C

Creating a C++ Program

CC112 Structured Programming

Fundamentals of Programming Session 8

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Syntax and Variables

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

LAB 6 FUNCTIONS I School of Computer and Communication Engineering

Lecture 8 Tao Wang 1

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Lecture 9 - C Functions

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

2. Numbers In, Numbers Out

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

Transcription:

AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University

FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier: printf( %5d, number); For floating-point values (floats and doubles), you can also specify the number of digits to display before/after the decimal point: printf( %5.3f, average);

Constants A constant is a value that cannot change Ex. numeric literals (42, 23, 3.14) Variables can be declared as constants using the keyword const: const double pi = 3.1415926; Strings (sequences of characters enclosed in double quotes) are also constants.

MORE ELABORATE LOOPS Recall that every loop contains a test As long as the test is true (has a nonzero value), the loop will continue to execute Tests don t have to be simple Boolean comparisons They can involve function calls...

RETURN VALUES REVISITED printf() and scanf() each return an integer value when they complete printf() returns the number of characters printed, or a negative value if an error occurred scanf() returns the number of successful conversions or the system-defined end-of-value.

#include <stdio.h> #include <stdlib.h> int main(void) int i; double x, min, max, sum, avg; if (scanf( %lf, &x)!= 1) printf( No data found - bye!\n ); exit(1); }

min = max = sum = avg = x; printf( %5s%9s%9s%9s%12s%12s\n, Count, Item, Min, Max, Sum, Average ); printf( %5s%9s%9s%9s%12s%12s\n\n, -----, ----, ---, ---, ---, ------- ); printf( %5d%9.1f%9.1f%9.1f%12.3f%12.3f\n, 1, x, min, max, sum, avg);

for (i = 2; scanf( %lf, &x) == 1; i++) if (x < min) min = x; else if (x > max) max = x; sum += x; avg = sum / i; printf( %5d%9.1f%9.1f%9.1f%12.3f%12.3f\n, i, x, min, max, sum, avg); } return 0; } /* end of main() */

FUNCTIONS

FUNCTIONS A function is a small block of code that can be called from another point in a program Functions enable reuse, and can be used to abstract out common tasks Ex. computing the factorial of a number Function results can be changed by supplying different input values

CALLING A FUNCTION To call a function, write its name, followed by a pair of parentheses, followed by a semicolon Ex. rand(); If the function takes any input, those values go inside the parentheses Ex. printf( %d, value);

FUNCTION ARGUMENTS Arguments are pieces of data that are passed into a function Different input can produce different results Arguments can be manipulated, like variables Arguments are normally passed as copies changes are not sent back when the function returns

RETURN VALUES Some functions pass a value back to the place where they were called Ex. factorial() sends back an answer The return value effectively replaces the function call in the original expression int answer = factorial(3); becomes int answer = 6;

RETURN VALUES If a function returns a value, it must contain a return statement: return value ; The return value must match the return type in the function header! A function may return any value of the specified type

FUNCTION EXECUTION Only one function can be active at a time When a function is called, the calling function is put on hold while the called function executes. When the called function completes (returns), control returns to the calling function Function calls can be nested (e.g., A calls B, which calls C when C completes, B resumes execution, then returns control to A when it s done)

DEFINING A FUNCTION A function definition consists of a function header and a function body The function header specifies the return type, name, and arguments list The function body is a brace-enclosed set of 0 or more program statements

GENERAL FORM return_type function_name ( arguments ) function body }

NOTES ON DEFINING FUNCTIONS Like variables, functions must be defined before they can be used Some programming conventions state that main() should come before any other functions in a program How can main() use the function if it hasn t been defined yet? Answer: Precede main() with one or more function prototypes

FUNCTION PROTOTYPES A function prototype tells the compiler: the number and types of arguments the function takes in the type of value that the function returns General form: return-type function-name (parameter type list) ; e.g., double pow (double x, double y);

EXAMPLE 1 void printdashedline () printf( -------------------- ); }

EXAMPLE 2 void clearscreen () int i; for (i = 0; i < 24; i++) printf( \n ); } }

EXAMPLE 3 void printsomestars (int n) int i; for (i = 0;i < n;i++) printf( * ); } printf( \n ); }

EXAMPLE 4 void print1ton (int n) int i; for (i = 1; i <= n; i++) printf( %d\n, i); }

EXAMPLE 5 int getyear () int value; printf( Enter the year: ); scanf( %d, &value); return value; }

EXAMPLE 6 int average (int a, int b, int c) int sum = a + b + c; return sum/3; }

EXAMPLE 7 int multiply (int first, int second) return (first * second); }

EXAMPLE 8 int factorial (int value) int fac; for (fac = 1; value > 1; value--) fac = fac * value; return fac; } /* value is unchanged in the calling ftn */

VARIABLE SCOPE Scope refers to the area of a program for which a variable is defined Scope is restricted to the smallest set of curly braces around the variable Ex. the function in which a variable is defined

SCOPE ILLUSTRATION int myfunction ()... int x;... /* x is in scope here */ } /* x is out of scope here */

GLOBAL VARIABLES A global variable is declared outside of any function Global variables are accessible from anywhere in a program Global variables are used to share data Constants are usually declared as globals

GLOBAL VARIABLES const float PI = 3.1415926; int main (void) float area = PI * 2 * 2;... }

SCOPE AND NAMING Several variables can have the same name, as long as they are in different scopes The most recently-declared variable takes precedence We say that it shadows the other variable

SAME NAMES int x = 5; /* this x is global */ void foo () int x = 10; /* this x shadows the global one */ printf( %d, x); /* prints 10 */ }