Control Flow, Functions and Basic Linkage

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

Fundamental of Programming (C)

EL2310 Scientific Programming

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

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

Chapter 11 Introduction to Programming in C

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

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 11 Introduction to Programming in C

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

WARM UP LESSONS BARE BASICS

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

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

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

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

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

Arithmetic type issues

Chapter 11 Introduction to Programming in C

CpSc 1111 Lab 4 Formatting and Flow Control

Course Outline Introduction to C-Programming

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

Formatted Input/Output

Introduction to C. Systems Programming Concepts

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Lecture 5: C programming

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

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

Fundamentals of Programming

Pace University. Fundamental Concepts of CS121 1

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key

Chapter 11 Introduction to Programming in C

CSC 270 Survey of Programming Languages

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

UIC. C Programming Primer. Bharathidasan University

Lecture 9 - C Functions

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Fundamentals of Programming

Week 2 C Fundamentals; Formatted Input/Output; int and float Types

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Introduction to C Programming (Part A) Copyright 2008 W. W. Norton & Company. All rights Reserved

CpSc 1111 Lab 5 Formatting and Flow Control

Computers Programming Course 7. Iulian Năstac

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

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Display Input and Output (I/O)

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

Informática Ingeniería en Electrónica y Automática Industrial

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Week 2 / Lecture 2 15 March 2017 NWEN 241 Control constructs, Functions. Alvin Valera

Informatica e Sistemi in Tempo Reale

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

Programming for Electrical and Computer Engineers. Loops

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

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Introduction to C Part 1 HLL s and the Basic Syntax. (Ch11 & 12)

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Lecture 6. Statements

Structured programming

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Computer Programming

Chapter 5 C Functions

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

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Programming Language A

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

Lecture 04 FUNCTIONS AND ARRAYS

BSM540 Basics of C Language

Repetition Structures II

Syntax and Variables

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

CSI33 Data Structures

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

C: How to Program. Week /Mar/05

Chapter 3: Arrays and More C Functionality

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

The Basics of C Programming

2. Numbers In, Numbers Out

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

Problem Solving and 'C' Programming

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

C Programming Basics

C Functions. 5.2 Program Modules in C

Topic 6: A Quick Intro To C

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

Transcription:

Control Flow, Functions and Basic Linkage MATH 5061: Fundamentals of Computer Programming for Scientists and Engineers Dr. Richard Berger richard.berger@temple.edu Department of Mathematics Temple University 10/13/2016

Outline Decision and Control Flow Statements if- and else-statement Conditional Expressions switch-statement while-statement for-statement do-while-statement Functions Declaration and Definition void type Examples Compilation - Part 2: Basic Linkage

Outline Decision and Control Flow Statements if- and else-statement Conditional Expressions switch-statement while-statement for-statement do-while-statement Functions Declaration and Definition void type Examples Compilation - Part 2: Basic Linkage

if Statement executing a block of code if a condition is true Python if condition: statement1 if condition: statement1 statement2 statement3 C++ if (condition) { statement1; if (condition) { statement1; statement2; statement3;

else Statement else-statement is only executed if the condition of the if-statement is false Python if condition: statementa else: statementb C++ if (condition) statementa; else statementb;

else Statement Python if condition: statementa1 statementa2 statementa3 else: statementb1 statementb2 statementb3 C++ if (condition) { statementa1; statementa2; statementa3; else { statementb1; statementb2; statementb3;

Dangling else if (condition) if (condition2) if (condition3) statementa; else statementb; if (condition) if (condition2) if (condition3) statementa; else statementb; if (condition) if (condition2) if (condition3) statementa; else statementb; These 3 variants are the same in C++ In Python indentation rules would make them different In C++ the parsing rules state that the else belongs to the inner-most if-statement Recommendation: Always define a block using curly braces, even for single line if-statements

Dangling else if (condition) { if (condition2) { if (condition3) { statementa; else { statementb; Using curly braces makes intent explicit Might seem like more typing, but worth the effort vs. looking for the bug later if (condition) { if (condition2) { if (condition3) { statementa; else { statementb;

Common mistake if you don t use curly braces if (condition) statementa1; now extend with an additional statement if (condition) statementa1; statementa2;

Common mistake if you don t use curly braces if (condition) statementa1; now extend with an additional statement if (condition) statementa1; statementa2; you wanted this if (condition) { statementa1; statementa2;

Common mistake if you don t use curly braces if (condition) statementa1; now extend with an additional statement if (condition) statementa1; statementa2; you wanted this if (condition) { statementa1; statementa2; but compiler will see this if (condition) { statementa1; statementa2;

Conditional Expressions if (a > 0) b = a; else b = -a; Alternative: b = (a > 0)? a : -a; an expression which evaluates to one of two alternatives based on a condition expr 1? expr 2 : expr 3 ;

Alternative to if-cascades if (expr == consta) {... else if(expr == constb) {... else if(expr == constc) {... else {...

Alternative to if-cascades if (expr == consta) {... else if(expr == constb) {... else if(expr == constc) {... else {... the switch statement takes an expression and jumps to the case which matches its value cases have to be constants like integer literals or character literals switch(expr) { case consta:... break; case constb:... break; case constc:... break; default:...

switch Statement - with integer literals int dayofweek =...; switch(dayofweek) { case 0: printf("monday"); break; case 1: printf("tuesday"); break; case 2: printf("wednesday"); break; default: // in all other cases do this

switch Statement - with character literals char answer =...; switch(answer) { case y : printf("the answer is YES!\n"); break; each case defines a constant and ends with a break case n : printf("the answer is NO!\n"); break; case m : printf("the answer is MAYBE!\n"); break; default: // invalid selection

switch Statement - with character literals char answer =...; switch(answer) { case y : printf("the answer is YES!\n"); break; case n : printf("the answer is NO!\n"); break; each case defines a constant and ends with a break after the break-statement execution continues after the switch statement case m : printf("the answer is MAYBE!\n"); break; default: // invalid selection

switch Statement - with character literals char answer =...; switch(answer) { case y : printf("the answer is YES!\n"); break; case n : printf("the answer is NO!\n"); break; case m : printf("the answer is MAYBE!\n"); break; each case defines a constant and ends with a break after the break-statement execution continues after the switch statement the optional default case is reached if none of the cases match default: // invalid selection

switch Statement - combining cases char answer =...; switch(answer) { case y : case Y : printf("the answer is YES!\n"); break; case n : case N : printf("the answer is NO!\n"); break; if a break statement is missing between cases, execution continues with the next case. No further comparison is made, execution just continues. default: // invalid selection

switch Statement - combining cases char answer =...; switch(answer) { case y : case Y : printf("the answer is YES!\n"); break; case n : case N : printf("the answer is NO!\n"); break; if a break statement is missing between cases, execution continues with the next case. No further comparison is made, execution just continues. default: // invalid selection

switch Statement - combining cases char answer =...; switch(answer) { case y : case Y : printf("the answer is YES!\n"); break; case n : case N : printf("the answer is NO!\n"); break; if a break statement is missing between cases, execution continues with the next case. No further comparison is made, execution just continues. default: // invalid selection

while loop Python while condition: statement while condition: statement1 statement2 statement3 C++ while (condition) statement; while (condition) { statement1; statement2; statement3;

Example: Temperature Table Let s write a program which generates a table showing the celsius temperature for fahrenheit values from 0 to 300 in increments of 20 degrees. C = 5 9 ( F 32) F C 0-17 20-6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 148

Live Demo: Example 1a

Example: Temperature Table #include <stdio.h> int main() { int lower = 0; // lower limit of table int upper = 300; // upper limit int step = 20; // step size int fahrenheit = lower; printf("f\tc\n"); while (fahrenheit <= upper) { int celsius = 5*(fahrenheit - 32) / 9; printf("%d\t%d\n", fahrenheit, celsius); fahrenheit += step; return 0; F C 0-17 20-6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 148

Live Demo: Example 1b

Example: Temperature Table using floats #include <stdio.h> int main() { int lower = 0; // lower limit of table int upper = 300; // upper limit int step = 20; // step size float fahrenheit = lower; printf("f\tc\n"); while (fahrenheit <= upper) { float celsius = 5.0*(fahrenheit - 32.0) / 9.0; printf("%d\t%f\n", fahrenheit, celsius); fahrenheit += step; return 0; F C 0.000000-17.777779 20.000000-6.666667 40.000000 4.444445 60.000000 15.555555 80.000000 26.666666 100.000000 37.777779 120.000000 48.888889 140.000000 60.000000 160.000000 71.111115 180.000000 82.222221 200.000000 93.333336 220.000000 104.444443 240.000000 115.555557 260.000000 126.666664 280.000000 137.777771 300.000000 148.888885

Controlling formatting in printf Placeholders can be further configured You can specify how wide a placeholder should be. By default it will add spaces when necessary %3d Print decimal number so that it is at least 3 characters wide, insert spaces if necessary %03d Print decimal number so that it is at least 3 characters wide and insert leading zeros if necessary %6.1f Print floating point number so that it is at least 6 characters wide and have 1 number after the decimal point

Live Demo: Example 1c

Example: Formatted Temperature Table #include <stdio.h> int main() { int lower = 0; // lower limit of table int upper = 300; // upper limit int step = 20; // step size int fahrenheit = lower; printf("f\tc\n"); while (fahrenheit <= upper) { float celsius = 5.0*(fahrenheit - 32.0) / 9.0; printf("%3d %6.1f\n", fahrenheit, celsius); fahrenheit += step; return 0; F C 0-17.8 20-6.7 40 4.4 60 15.6 80 26.7 100 37.8 120 48.9 140 60.0 160 71.1 180 82.2 200 93.3 220 104.4 240 115.6 260 126.7 280 137.8 300 148.9

Looping over number ranges int i = 0; while (i < 10) { printf("%d\n", i); ++i; 0 1.. 8 9 very common in programming, which is why there is a special loop for it

Looping over number ranges int i = 0; while (i < 10) { printf("%d\n", i); ++i; 0 1.. 8 9 very common in programming, which is why there is a special loop for it for (int i = 0; i < 10; ++i ) { printf("%d\n", i);

for Statement Regular usage: for ( init ; condition ; increment ) { // loop body General form: for ( expr1 ; expr2 ; expr3 ) { // loop body expr1; while ( expr2 ) { // loop body expr3;

for Statement Loop with 10 iterations Output for (int i = 0; i < 10; ++i ) { printf("%d\n", i);

for Statement Loop with 10 iterations 1 for (int i = 0; i < 10; ++i ) { printf("%d\n", i); Output

for Statement Loop with 10 iterations for (int i = 0; i < 10; ++i ) { printf("%d\n", i); 2 Output

for Statement Loop with 10 iterations for (int i = 0; i < 10; ++i ) { printf("%d\n", i); 3 Output 0

for Statement Loop with 10 iterations for (int i = 0; i < 10; ++i ) { printf("%d\n", i); 4 Output 0

for Statement Loop with 10 iterations for (int i = 0; i < 10; ++i ) { printf("%d\n", i); 5 Output 0

for Statement Loop with 10 iterations for (int i = 0; i < 10; ++i ) { printf("%d\n", i); 6 Output 0 1

for Statement Loop with 10 iterations for (int i = 0; i < 10; ++i ) { printf("%d\n", i); 7 Output 0 1

for Statement Loop with 10 iterations for (int i = 0; i < 10; i++ ) { printf("%d\n", i); Loop with an increment of 2 for (int i = 0; i < 10; i += 2 ) { printf("%d\n", i);

Live Demo: Example 1d

Example: Formatted Temperature Table using for-loop #include <stdio.h> int main() { int lower = 0; // lower limit of table int upper = 300; // upper limit int step = 20; // step size printf("f\tc\n"); for (int fahrenheit = lower; fahrenheit <= upper; fahrenheit += step) { float celsius = 5.0*(fahrenheit - 32.0) / 9.0; printf("%3d %6.1f\n", fahrenheit, celsius); return 0;

do-while Loop do { statementa1; statementa2; statementa3; statementa4; while (condition); Body of the loop is executed at least once Loop condition is only evaluated at the end

Example: number guessing game Guess a number between 1 and 100 Enter a number: 50 nope... the number is higher Enter a number: 70 nope... the number is higher Enter a number: 90 nope... the number is higher Enter a number: 99 nope... the number is lower Enter a number: 95 nope... the number is lower Enter a number: 92 Congratz! You found it!

Live Demo: Example 2

Example: number guessing game #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(0)); // seed random number generator with current time int secret_value = (rand() % 100) + 1; // create a random integer between 1 and 100 int guess = 0; printf("guess a number between 1 and 100\n"); do { printf("enter a number: "); scanf("%d", &guess); if (guess < secret_value) { printf("nope... the number is higher\n"); else if (guess > secret_value) { printf("nope... the number is lower\n"); while(guess!= secret_value); printf("congratz! You found it!\n"); return 0;

break Statement while (condition) { statementa1; statementa2; if (condition) { statementb1; statementb2; break; statementa3; statementa4; Escape from the inside loop.

break Statement while (condition) { statementa1; statementa2; if (condition) { statementb1; statementb2; break; statementa3; statementa4; Escape from the inside loop.

continue Statement while (condition) { statementa1; if (condition2) { statementb1; continue; statementa2; Abort the current iteration of the loop and continue with the next

continue Statement while (condition) { statementa1; if (condition2) { statementb1; continue; statementa2; Abort the current iteration of the loop and continue with the next

Infinite Loops these are loops which do not stop unless you break or return from them while (true) {... for ( ; ; ) {...

Outline Decision and Control Flow Statements if- and else-statement Conditional Expressions switch-statement while-statement for-statement do-while-statement Functions Declaration and Definition void type Examples Compilation - Part 2: Basic Linkage

Functions and the DRY Principle Functions are a way to organize and structure your code DRY = Don t Repeat Yourself Avoid needless duplication of code by extracting common code into functions. This has multiple effects: it makes your code shorter less code, less possibility for bugs with good function names, you create more readable code your code becomes reusable

Declaration and Definition of Functions double rect_area ( double w, double h ) { return w * h;

Declaration and Definition of Functions function signature double rect_area ( double w, double h ) { return w * h;

Declaration and Definition of Functions function signature double rect_area ( double w, double h ) { return w * h; return type

Declaration and Definition of Functions function signature double rect_area ( double w, double h ) { return w * h; return type function name

Declaration and Definition of Functions function signature double rect_area ( double w, double h ) { return w * h; return type arguments function name each argument has to be declared with a data type

Declaration and Definition of Functions double rect_area ( double w, double h ) { return w * h; function body the return value must be compatible with the return type

void Type functions which do not return anything, but only execute code can be declared with a special return type of void function declared as void can use a simple return; statement without any value. a return in a void function is optional Example: print_vector function void print_vector(float x, float y) { printf("(%f, %f)", x, y); return; // optional

Declaration and Definition of Functions A: Define functions first #include <stdio.h> void print_vector(float x, float y) { printf("(%f, %f, %f)\n", x, y); int main() { print_vector(1.0f, 0.0f); print_vector(0.0f, 1.0f); return 0;

Declaration and Definition of Functions A: Define functions first #include <stdio.h> void print_vector(float x, float y) { printf("(%f, %f, %f)\n", x, y); int main() { print_vector(1.0f, 0.0f); print_vector(0.0f, 1.0f); return 0; This does not compile #include <stdio.h> int main() { print_vector(1.0f, 0.0f); print_vector(0.0f, 1.0f); return 0; void print_vector(float x, float y) { printf("(%f, %f)\n", x, y);

Declaration and Definition of Functions A: Define functions first #include <stdio.h> void print_vector(float x, float y) { printf("(%f, %f, %f)\n", x, y); int main() { print_vector(1.0f, 0.0f); print_vector(0.0f, 1.0f); return 0; B: Declare function and define later #include <stdio.h> void print_vector(float x, float y); int main() { print_vector(1.0f, 0.0f); print_vector(0.0f, 1.0f); return 0; void print_vector(float x, float y) { printf("(%f, %f)\n", x, y);

Example: Temperature Conversion functions let s create functions which do the conversion C F for us this is a small reusable piece of code float fahrenheit2celsius(float fahrenheit); float celsius2fahrenheit(float celsius);

Example: Temperature Conversion functions float fahrenheit2celsius(float fahrenheit) { return 5.0*(fahrenheit - 32.0) / 9.0; float celsius2fahrenheit(float celsius) { return (celsius * 9.0 / 5.0) + 32.0;

Live Demo: Rewrite Temperature Table Example

Outline Decision and Control Flow Statements if- and else-statement Conditional Expressions switch-statement while-statement for-statement do-while-statement Functions Declaration and Definition void type Examples Compilation - Part 2: Basic Linkage

Example: Temperature Converter Let s write a small utility which converts any temperature value User is first asked what units should be used (enter c or C and f or F ) Then asks for a temperature and converts it

Live Demo: Example 3

temp_converter.cpp - Part 1 (Incomplete) #include <stdio.h> char ask_for_units(); int main() { char units = ask_for_units(); float value, converted; printf("enter the temperature (in %c): ", units); scanf("%f", &value); if (units == C ) { converted = celsius2fahrenheit(value); printf("%6.1f C = %6.1f F\n", value, converted); else { // units == F converted = fahrenheit2celsius(value); printf("%6.1f F = %6.1f C\n", value, converted); return 0;

temp_converter.cpp - Part 2 char ask_for_units() { char selected_unit; while(true) { printf("please select unit (c or C / f or F): "); scanf(" %c", &selected_unit); switch(selected_unit) { case c : case C : return C ; case f : case F : return F ; default: printf("invalid input!\n");

Working with multiple source files We already implemented the conversion functions for one program By moving them into their own source file we can reuse that code in another application temp_converter.cpp main logic of the program temp_utilities.cpp utility functions for conversion

Live Demo: Create temp_utilities.cpp

Working with multiple source files When we compile the program we have to compile two source files g++ -o converter temp_converter.cpp temp_utilities.cpp

Working with multiple source files When we compile the program we have to compile two source files g++ -o converter temp_converter.cpp temp_utilities.cpp However we get this compile error: temp_converter.cpp: In function int main() : temp_converter.cpp:14:45: error: celsius2fahrenheit was not declared in this scope converted = celsius2fahrenheit(value); ^ temp_converter.cpp:18:45: error: fahrenheit2celsius was not declared in this scope converted = fahrenheit2celsius(value);

Recall: Compilation source file machine code hello.cpp compiler hello a compiler translates C++ code (text) into machine code (binary) each source file corresponds to one translation unit

Compilation is a Multi-Step process source file object file a.cpp compiler a.o linker program each source file corresponds to one translation unit a compiler translates C++ code (text) into machine code (binary) each translation unit becomes one object file an object file contains machine code, but has placeholders for symbols (names) it expects from libraries or other translation units a separate program, the linker, finds these symbols, replaces the placeholders and generates the final program

Compilation Steps Single command: g++ -o hello hello.cpp Separate Steps: g++ -o hello.o -C hello.cpp g++ -o hello hello.o

Compiling multiple files If you compile multiple files, each is compiled separately source file object file a.cpp compiler a.o b.cpp compiler b.o c.cpp compiler c.o

Linking Object files are finally combined by the linker to create an executable program source files object files a.cpp compiler a.o b.cpp compiler b.o linker program c.cpp compiler c.o

Example: Temperature Converter In our example each file is compiled separately The compiler of temp_converter.cpp does not see the symbols (names) of the other file This is why we have to declare these functions in temp_converter.cpp if we want to use them source files object files temp_converter.cpp compiler temp_converter.o linker program temp_utils.cpp compiler temp_utils.o

Header Files: sharing declarations while we can simply write the function declarations into the source files, it is common to move declarations of functions shared with the outside world in its own header file header files usually have the same name as their.cpp file and end with.h so in our case, since our functions are defined in temp_utils.cpp, we define a header temp_utils.h which only contains the function declarations the benefit of headers is that any other source file can simply #include them if Note needed local headers are included with #include "file.h" system/library headers are included with #include <file.h>

Live Demo: Example 3 Completion

temp_utilities.h float celsius2fahrenheit(float celsius); float fahrenheit2celsius(float fahrenheit); temp_utilities.cpp float fahrenheit2celsius(float fahrenheit) { return 5.0*(fahrenheit - 32.0) / 9.0; float celsius2fahrenheit(float celsius) { return (celsius * 9.0 / 5.0) + 32.0;

temp_converter.cpp - Part 1 (Completed) #include <stdio.h> #include "temp_utils.h" char ask_for_units(); int main() { char units = ask_for_units(); float value, converted; printf("enter the temperature (in %c): ", units); scanf("%f", &value); if (units == C ) { converted = celsius2fahrenheit(value); printf("%6.1f C = %6.1f F\n", value, converted); else { // units == F converted = fahrenheit2celsius(value); printf("%6.1f F = %6.1f C\n", value, converted); return 0;

Example: Compilation Steps Single command: g++ -o converter temp_converter.cpp temp_utils.cpp Separate Steps: # create temp_converter.o g++ -C temp_converter.cpp # create temp_utils.o g++ -C temp_utils.cpp # create final program converter g++ -o converter temp_converter.o temp_utils.o