Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Similar documents
Function I/O. Last Updated 10/30/18

Program Flow. Instructions and Memory. Why are these 16 bits? C code. Memory. a = b + c. Machine Code. Memory. Assembly Code.

Structured Programming. Dr. Mohamed Khedr Lecture 4

Expressions and Precedence. Last updated 12/10/18

Computer Science & Engineering 150A Problem Solving Using Computers

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

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

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

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

C Program Structures

EE C Program Elements

Chapter 4: Basic C Operators

Question 2. [5 points] Given the following symbolic constant definition

CT 229 Java Syntax Continued

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

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

BSM540 Basics of C Language

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Computer System and programming in C

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

ECET 264 C Programming Language with Applications

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

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

Data types, variables, constants

ANSI C Programming Simple Programs

Operators and Expressions:

DECLARAING AND INITIALIZING POINTERS

Lab 3. Pointers Programming Lab (Using C) XU Silei

Type Conversion. and. Statements

The Math Class. Using various math class methods. Formatting the values.

Functions. Systems Programming Concepts

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Computers Programming Course 6. Iulian Năstac

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

CC112 Structured Programming

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Chapter 6 - Notes User-Defined Functions I

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

CS3157: Advanced Programming. Outline

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Pointers. 10/5/07 Pointers 1

Fundamentals of Programming & Procedural Programming

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

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

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

C++ Programming Lecture 11 Functions Part I

Introduction to C Language

Introduction to Programming

C Programming

Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0.

At the end of this module, the student should be able to:

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

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

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

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Variation of Pointers

Chapter 2. Outline. Simple C++ Programs

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Variables and Operators 2/20/01 Lecture #

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Data Type Fall 2014 Jinkyu Jeong

File I/O. Last updated 10/30/18

C Programs: Simple Statements and Expressions

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Lesson 3 Introduction to Programming in C

Introduction to C. Systems Programming Concepts

Pointers and Arrays 1

C++ Programming: From Problem Analysis to Program Design, Third Edition

Lecture 2: C Programming Basic

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

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Arrays and Pointers. Lecture Plan.

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

CSCE 110 PROGRAMMING FUNDAMENTALS

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

Using Free Functions

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

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

Engineering Problem Solving with C++, Etter/Ingber

Operators in C. Staff Incharge: S.Sasirekha

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

First of all, it is a variable, just like other variables you studied

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

BSM540 Basics of C Language

C Programming Multiple. Choice

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

POINTER & REFERENCE VARIABLES

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Programming and Data Structures

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point

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

Transcription:

Function Input and Output Input through actual parameters Output through return value Only one value can be returned Input/Output through side effects printf scanf 2 tj

Function Input and Output int main(void){ float checking; float savings; float int_rate; checking = update_acct(checking, int_rate); savings = update_acct(savings, int_rate); return 0; } float update_acct(float balance, float int_rate){ balance += balance * int_rate; return balance; } 3 tj

Pointer Review variables in memory address for myvar1 0x2000 0020 address for myvar2 0x2000 0024 address for myvar3 0x2000 0028 4 Byte word x x x x x x x x x 0x2000 0020 0x2000 0024 0x2000 0028 4 tj

Pointer A special Type A variable that holds the memory location of another variable Holds an address in our case 32 bits Each pointer must be tied to a specific data type int, float, char, 5 tj

Pointer To find the memory location of a variable use the address of operator: & &myvar1 0x2000 0020 &myvar2 0x2000 0024 &myvar3 0x2000 0028 Precedence Operator Description Associativity ++ -- Prefix increment and Right-to-left decrement + - Unary plus and minus! ~ Logical NOT and bitwise NOT 2 (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of _Alignof Alignment requirement(c11) 6 tj

Pointer To declare a pointer variable follow the type declaration with a * Precedence Operator Description Associativity ++ -- Prefix increment and Right-to-left decrement + - Unary plus and minus! ~ Logical NOT and bitwise NOT 2 (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of _Alignof Alignment requirement(c11) int* myvar1_ptr; // declare a pointer variable with name myvar1_ptr // that points to an integer variable float* myvar2_ptr; // declare a pointer variable with name myvar2_ptr // that points to an float variable 7 tj

Pointer Precedence Operator Description Associativity ++ -- Prefix increment and Right-to-left decrement + - Unary plus and minus! ~ Logical NOT and bitwise NOT 2 (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of _Alignof Alignment requirement(c11) To determine the value of a variable pointed to by a pointer variable precede the pointer variable with * (dereference operator) *myvar1_ptr; // provides the value held in the memory location // pointed to by myvar1_ptr as an int *myvar2_ptr; // provides the value held in the memory location // pointed to by myvar2_ptr as a float 8 tj

Pointer int var1 = 5; // stored in memory location 0x2000 1010 float var2 = 12.0; // stored in memory location 0x2000 0220 int foo1; float foo2; int* ptr1; float* ptr2; // define a pointer to a variable of type int // define a pointer to a variable of type float ptr1 = &var1; // set ptr1 to 0x2000 1010 ptr2 = &var2; // set ptr2 to 0x2000 0220 foo1 = *ptr1; // set foo1 to 5 foo2 = *ptr2; // set foo2 to 12.0 9 tj

Pointers and functions Pointers allow us to use called functions to change values in the calling function Instead of passing variables in the parameter list (remember copies are made and then destroyed) we can pass pointers Pointers allow us to modify the passed variables by memory reference 10 tj

Pointers and functions Declaration Indicate that a pointer is being passed in the Formal Parameter List void update_acct(float* balance_ptr, float int_rate); 11 tj

Pointers and functions Definition Indicate that a pointer is being passed in the Formal Parameter List Operate on the variables pointed to by the pointers via the dereference operator void update_acct(float* balance_ptr, float int_rate){ *balance_ptr += *balance_ptr * int_rate; return; } 12 tj

Pointers and functions Usage Pass a pointer variable in the Actual Parameter List Pass the address to the variable in the Actual Parameter List int main(void){ float checking; float savings; float int_rate; float* check_ptr = &checking; update_acct(check_ptr, int_rate); update_acct(&savings, int_rate); return 0; } // ptr variable to a float variable 13 tj

Pointers and functions int main(void){ float checking; float savings; float int_rate; float* check_ptr = &checking; clear_acct(check_ptr); clear_acct(&savings); return 0; } // ptr variable to a float variable void clear_acct(float* balance_ptr){ *balance_ptr =0.0; return; } 14 tj

Pointers and functions int main(void){ int a; int b; swap(&a, &b); return 0; } void swap(int* x, int* y){ int tmp; tmp = *x; *x = *y; *y = tmp; return; } 15 tj

Pointers and functions int main(void){ int num; int den; int quo; int rem; divide(num, den, &quo, &rem); return 0; } void divide(int num, int den, int* quo, int* rem){ *quo = num / den; *rem = num % den; return; } 16 tj

Pointers and functions * circle.c Calculate the area and circumference of a circle Created by tj Rev 0, 11/15/16 */ #include <stdio.h> #define PI 3.14159 int main(void){ setbuf(stdout, NULL); // disable buffering // Local variables float radius; float circumference; float area; // Get input for radius printf("please enter a value for radius: "); scanf("%f", &radius); // Calculate circumference and area circumference = 2 * PI * radius; area = PI * radius * radius; // Output results printf("circumference = %f\n", circumference); printf("area = %f\n", area); return 0; } 17 tj

Standard Functions C standard functions C function reference https://en.wikipedia.org/wiki/c_standard_library 18 tj

Standard Functions I/O Functions C functions #include <stdio.h> printf() scanf() 19 tj

Standard Functions Math Functions Absolute value abs(x), fabs(x) labs, llabs, fabsf, fabsl if(fabs(a-b) < 0.000001){ 20 tj

Standard Functions Math Functions #include <math.h> 21 tj

Standard Functions Math Functions Ceiling ceil(x) ceilf, ceill Smallest integer value the operand ceil(4.5) 5 ceil(-12.7) -12 Floor floor(x) floorf, floorl Largest integer value the operand floor(4.5) 4 floor(-12.7) -13 22 tj

Standard Functions Math Functions Truncate trunc(x) truncf, truncl Rounds toward 0 trunc(4.5) 4 trunc(-12.7) -12 Round round(x) roundf, roundl Rounds to nearest integer round(4.5) 5 round(-12.7) -13 23 tj

Standard Functions Math Functions Power pow(x,y) powf, powl x to the power y pow(3.0, 4.0) 81 pow(3.4, 2.3) 16.687893 Square Root sqrt(x) sqrtf, sqrtlf Square root of x sqrt(25) 5 24 tj

Standard Functions Math Functions Power pow(x,y) powf, powl x to the power y pow(3.0, 4.0) 81 pow(3.4, 2.3) 16.687893 Square Root sqrt(x) sqrtf, sqrtlf Square root of x sqrt(25) 5 25 tj