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

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

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

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

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

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

CT 229 Java Syntax Continued

BSM540 Basics of C Language

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

Computer System and programming in C

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

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

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

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

ECET 264 C Programming Language with Applications

Data types, variables, constants

DECLARAING AND INITIALIZING POINTERS

Type Conversion. and. Statements

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

ANSI C Programming Simple Programs

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

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

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?

Operators and Expressions:

Computers Programming Course 6. Iulian Năstac

CC112 Structured Programming

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

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

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

Pointers. 10/5/07 Pointers 1

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

Chapter 6 - Notes User-Defined Functions I

CS3157: Advanced Programming. Outline

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

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

Fundamentals of Programming & Procedural Programming

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

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

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

Introduction to C Language

C++ Programming Lecture 11 Functions Part I

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

C Programming

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

Introduction to Programming

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

Variation of Pointers

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

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

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

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

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

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:

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

Variables and Operators 2/20/01 Lecture #

Lecture 3: C Programm

Lesson 3 Introduction to Programming in C

Pointers and Arrays 1

Introduction to C. Systems Programming Concepts

Data Type Fall 2014 Jinkyu Jeong

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

C Programs: Simple Statements and Expressions

Lecture 2: C Programming Basic

Chapter 2. Outline. Simple C++ Programs

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

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

Arrays and Pointers. Lecture Plan.

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

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

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

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

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

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

CSCE 110 PROGRAMMING FUNDAMENTALS

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

Operators in C. Staff Incharge: S.Sasirekha

More Arrays. Last updated 2/6/19

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

Using Free Functions

BSM540 Basics of C Language

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

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

C Programming Multiple. Choice

POINTER & REFERENCE VARIABLES

Engineering Problem Solving with C++, Etter/Ingber

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.

Programming and Data Structures

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

Transcription:

Last Updated 10/30/18

Program Structure Includes Function Declarations void main(void){ foo = fun1(a, b); fun2(2, c); if(fun1(c, d)) { } } Function 1 Definition Function 2 Definition 2 tj

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 3 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; } 4 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 5 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, 6 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) 7 tj

Pointer To declare a pointer variable follow the type declaration with a * int* myvar1_ptr; // declare a pointer variable with name myvar1_ptr // that holds the memory location of an integer variable float* myvar2_ptr; // declare a pointer variable with name myvar2_ptr // that holds the memory location of a float variable 8 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) 9 tj

Pointer int foo1; float foo2; int foo3; int foo4; var1 0x2000 0010 0x2000 0014 int var1; float var2; // define a variable of type int // define a variable of type float var2 0x2000 0018 0x2000 001C 0x2000 0020 10 tj

Pointer int foo1; float foo2; int foo3; int foo4; int var1; float var2; // define a variable of type int // define a variable of type float var1 var2 5 12.0 0x2000 0010 0x2000 0014 0x2000 0018 int var1 = 5; // stored in memory location 0x2000 0010 float var2 = 12.0; // stored in memory location 0x2000 0014 0x2000 001C 0x2000 0020 11 tj

Pointer int foo1; float foo2; int foo3; int foo4; int var1; float var2; // define a variable of type int // define a variable of type float var1 var2 5 12.0 0x2000 0010 0x2000 0014 0x2000 0018 int var1 = 5; // stored in memory location 0x2000 0010 float var2 = 12.0; // stored in memory location 0x2000 0014 0x2000 001C int* ptr1; float* ptr2; // define a pointer to a variable of type int // define a pointer to a variable of type float ptr1 0x2000 0020 ptr2 12 tj

Pointer int foo1; float foo2; int foo3; int foo4; int var1; float var2; // define a variable of type int // define a variable of type float var1 var2 5 12.0 0x2000 0010 0x2000 0014 0x2000 0018 int var1 = 5; // stored in memory location 0x2000 0010 float var2 = 12.0; // stored in memory location 0x2000 0014 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 0010 ptr2 = &var2; // set ptr2 to 0x2000 0014 ptr1 ptr2 0x 2000 0010 0x 2000 0014 0x2000 001C 0x2000 0020 13 tj

Pointer int foo1; float foo2; int foo3; int foo4; int var1; float var2; // define a variable of type int // define a variable of type float int var1 = 5; // stored in memory location 0x2000 0010 float var2 = 12.0; // stored in memory location 0x2000 0014 int* ptr1; float* ptr2; // define a pointer to a variable of type int // define a pointer to a variable of type float var1 var2 5 12.0 0x2000 0010 0x2000 0014 0x2000 0018 ptr1 = &var1; // set ptr1 to 0x2000 0010 ptr2 = &var2; // set ptr2 to 0x2000 0014 foo1 = *ptr1; // set foo1 to 5 foo2 = *ptr2; // set foo2 to 12.0 foo3 = &ptr1; // set foo3 to 0x2000 001C foo4 = &ptr2; // set foo4 to 0x2000 0020 ptr1 ptr2 0x 2000 0010 0x 2000 0014 0x2000 001C 0x2000 0020 14 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 15 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); 16 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; } 17 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 18 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; } 19 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; } 20 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; } 21 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; } 22 tj

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

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

Standard Functions Math Functions #include <math.h> 25 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 26 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 27 tj

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

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