Docsity

C++ Functions: Understanding Arguments and Return Values - Prof. Keith E. Hellman, Lab Reports of Computer Science

Partial preview of the text

Download C++ Functions: Understanding Arguments and Return Values - Prof. Keith E. Hellman and more Lab Reports Computer Science in PDF only on Docsity!

Programming with Functions

July 29, 2009

Motivation

We Know A Lot about Functions Already

C++ functions are conceptually similar to the

f ( x ) you know from mathematics.

5 i n t main ( ) 6 { 7 d o u b l e x ; 8 c o u t << " Enter a value for the square root. " << e n d l ; 9 c i n >> x ; 10 11 d o u b l e x _ r o o t = s q r t ( x ) ; 12 c o u t << " The sqrt of " << x << " is " << x _ r o o t << e n d l ; 13 14 r e t u r n 0 ; 15 }

How many functions

are shown here?

Describe the input(s)

and output(s)

Motivation

f ( x , y ) ∼ double f( double x, double y )

◮ Put two values in (x and y), get one out.

1. The inputs are called arguments.

2. In mathematics we might say “the value of f ( x , y )”,

in C++ we say f returns the value...

3. In mathematics, all functions take some type of numerical inputs, in C++

functions might take string or bool inputs, they may not take any

arguments at all!

◮ Functional composition works as expected in C++:

z = f ( sin ( y ), 3 ) ≡ double z = f( sin(y), 3 );

◮ Recursion: an = an − 2 + an − 1.

Motivation

Why Functions?

◮ Provides re-usability of common or often used algorithms (such as

prompting a user for input, or calculating a complex multi-variable

equation).

◮ Let’s you maintain “algorithm focus” —

don’t clutter up any function (specifically main()) with “little bits of

logic” that aren’t intrinsic to the task at hand.

◮ Allows you to provide a single, explicit interface to data or algorithms

specific to your application. For instance, an interface for calculating

specialized equations, access to a laboratory device, or access to a

database of collected data,...

Motivation

Function Topics

Part I

Basic functions (1 or more parameters, returning values)

Header files, source files, and the programmer’s toolbox

Strange functions: void functions & empty argument lists

Part II

Passing parameters by value or reference

Arrays as function parameters

The const keyword with function parameters

Function overloading

Part III

C++ objects as function parameters

Input and Output functions in C++

Function parameter guidelines

Functions I

Functions I

Functions I Function Anatomy

Parts of a Function

6 ∗ R e t u r n s t h e p r o b a b i l i t y t h a t you w i l l f l i p h h e a d s o u t o f 7 ∗ N t o t a l c o i n t o s s e s w i t h an UNFAIR c o i n w i t h b i a s q f o r h e a d s 8 ∗ / 9 d o u b l e c o i n _ t o s s ( i n t h ead s , i n t N, d o u b l e q ) 10 { 11 d o u b l e c o m b i n a t i o n s = b i n o m i a l _ c o e f f i c i e n t ( N, N−h e a d s ) ; 12 d o u b l e o n e _ c o m b o _ p r o b a b i l i t y = pow ( q , h e a d s ) ∗ pow(1−q , N−h e a d s ) ; 13 r e t u r n c o m b i n a t i o n s ∗ o n e _ c o m b o _ p r o b a b i l i t y ; 14 }

◮ The type of value returned matches the return_type.

◮ The variables heads, N, and q are formal parameters of the cointoss

function.

◮ Everything between the () of the function header is called the parameter

declarations or argument list.

◮ The most pragmatic use of functions by engineers is too encapsulate

complex equations such as this one!

N

heads

qheads ( 1 − q ) N − heads^ =

∏ Ni = 1 i

∏ headsi = 1 i^ ∏ Ni =− 1 heads i

qheads ( 1 − q ) N − heads

Functions I Function Anatomy

Try questions 1–3.

Functions I Using Functions

Calling a Function from main()

2 ∗ Use a f u n c t i o n by d e f i n i n g i t b e f o r e i t i s u s e d. 3 ∗ / 4 # i n c l u d e < i o s t r e a m > 5 u s i n g n am esp ace s t d ; 6 / ∗ ∗ R e t u r n s t r u e i f l e g s a , b , c f o rm a t r i a n g l e ∗ / 7 b o o l v a l i d _ t r i a n g l e ( d o u b l e a , d o u b l e b , d o u b l e c ) 8 { 9 i f ( a > b && a > c ) { 10 / / a g r e a t e s t 11 r e t u r n b + c > a ; 12 } e l s e i f ( b > a && b > c ) { 13 / / b g r e a t e s t 14 r e t u r n a + c > b ; 15 } 16 17 / / o t h e r w i s e , c g r e a t e s t 18 r e t u r n a + b > c ; 19 } 20 i n t main ( ) 21 { 22 d o u b l e a l e g ( 3 ) , b l e g ( 4 ) , c l e g ( 5 ) ; 23 / / F u n c t i o n c a l l , r e s u l t s a v e d 24 b o o l o k T r i a n g l e = v a l i d _ t r i a n g l e ( a l e g , b l e g , c l e g ) ; 25 i f ( o k T r i a n g l e ) { 26 c o u t << " OK Triangle! " << a l e g << " " << b l e g << 27 " " << c l e g << e n d l ; 28 } 29 / / E x i t t h e main f u n c t i o n w i t h co d e 0 30 r e t u r n 0 ; 31 }

Everything Up Front

before main()

OK

Functions I Using Functions

Calling a Function from main()

6 i n t main ( ) 7 { 8 d o u b l e a l e g ( 3 ) , b l e g ( 4 ) , c l e g ( 5 ) ; 9 / / F u n c t i o n c a l l , r e s u l t s a v e d 10 b o o l o k T r i a n g l e = v a l i d _ t r i a n g l e ( a l e g , b l e g , c l e g ) ; 11 i f ( o k T r i a n g l e ) { 12 c o u t << " OK Triangle! " << a l e g << " " << b l e g << 13 " " << c l e g << e n d l ; 14 } 15 / / E x i t t h e main f u n c t i o n w i t h co d e 0 16 r e t u r n 0 ; 17 } 18 / ∗ ∗ R e t u r n s t r u e i f l e g s a , b , c f o rm a t r i a n g l e ∗ / 19 b o o l v a l i d _ t r i a n g l e ( d o u b l e a , d o u b l e b , d o u b l e c ) 20 { 21 i f ( a > b && a > c ) { 22 / / a g r e a t e s t 23 r e t u r n b + c > a ; 24 } e l s e i f ( b > a && b > c ) { 25 / / b g r e a t e s t 26 r e t u r n a + c > b ; 27 } 28 29 / / o t h e r w i s e , c g r e a t e s t 30 r e t u r n a + b > c ; 31 }

Everything Down Below

after main()

NOT OK

In function ’int main()’: 10: error: ’valid_triangle’ was not declared in this scope

Functions I Using Functions

Calling a Function from main()

7 / ∗ ∗ R e t u r n s t r u e i f l e g s a , b , c f o rm a t r i a n g l e ∗ / 8 b o o l v a l i d _ t r i a n g l e ( d o u b l e a , d o u b l e b , d o u b l e c ) ; 9 10 i n t main ( ) 11 { 12 d o u b l e a l e g ( 3 ) , b l e g ( 4 ) , c l e g ( 5 ) ; 13 / / F u n c t i o n c a l l , r e s u l t s a v e d 14 b o o l o k T r i a n g l e = v a l i d _ t r i a n g l e ( a l e g , b l e g , c l e g ) ; 15 i f ( o k T r i a n g l e ) { 16 c o u t << " OK Triangle! " << a l e g << " " << b l e g << 17 " " << c l e g << e n d l ; 18 } 19 / / E x i t t h e main f u n c t i o n w i t h co d e 0 20 r e t u r n 0 ; 21 } 22 23 / / NO DEFINITION FOR v a l i d _ t r i a n g l e!

Missing Function

Definition

NOT OK

This is a linking error!

In function ‘main’: 14: undefined reference to ‘valid_triangle(double, double, double)’ ld returned 1 exit status

Functions I Using Functions

Calling a Function from main()

4 / ∗ ∗ R e t u r n s t r u e i f l e g s a , b , c f o rm a t r i a n g l e ∗ / 5 b o o l v a l i d _ t r i a n g l e ( i n t a , i n t b , i n t c ) ; 6 i n t main ( ) 7 { 8 d o u b l e a l e g ( 3 ) , b l e g ( 4 ) , c l e g ( 5 ) ; 9 / / F u n c t i o n c a l l , r e s u l t s a v e d 10 b o o l o k T r i a n g l e = v a l i d _ t r i a n g l e ( a l e g , b l e g , c l e g ) ; 11 i f ( o k T r i a n g l e ) { 12 c o u t << " OK Triangle! " << a l e g << " " << b l e g << 13 " " << c l e g << e n d l ; 14 } 15 / / E x i t t h e main f u n c t i o n w i t h co d e 0 16 r e t u r n 0 ; 17 } 18 b o o l v a l i d _ t r i a n g l e ( d o u b l e a , d o u b l e b , d o u b l e c ) 19 { 20 i f ( a > b && a > c ) {

Missing or Wrong

Prototype

NOT OK

In function ’int main()’: 10: warning: conversion to ’int’ from ’double’ may alter its value 10: 10: warning:warning: conversionconversion toto ’int’’int’ fromfrom ’double’’double’ maymay alteralter itsits valuevalue In function ‘main’: 10: undefined reference to ‘valid_triangle(int, int, int)’ ld returned 1 exit status

Functions I Using Functions

Separating Prototype and Implementation

Header File: triangles.h 1 / ∗ ∗ 2 ∗ Header f i l e f o r t r i a n g l e u t i l i t i e s 3 ∗ / 4 # pragma o n ce 5 # i n c l u d e < i o s t r e a m > 6 u s i n g n am esp ace s t d ; 7 / ∗ ∗ R e t u r n s t r u e i f l e g s a , b , c f o rm a t r i a n g l e ∗ / 8 b o o l v a l i d _ t r i a n g l e ( d o u b l e a , d o u b l e b , d o u b l e c ) ; Implementation File: triangles.cpp 1 / ∗ ∗ 2 ∗ U t i l i t i e s f o r t r i a n g l e s 3 ∗ / 4 # i n c l u d e " triangles. h " 5 b o o l v a l i d _ t r i a n g l e ( d o u b l e a , d o u b l e b , d o u b l e c ) 6 { 7 i f ( a > b && a > c ) { 8 / / a g r e a t e s t 9 r e t u r n b + c > a ; 10 } e l s e i f ( b > a && b > c ) { 11 / / b g r e a t e s t 12 r e t u r n a + c > b ; 13 } 14 / / o t h e r w i s e , c g r e a t e s t 15 r e t u r n a + b > c ; 16 }

Keep in the same folder or

directory.

Note the

#pragma once

in the header file.

Note the

#include "triangles.h"

in the implementation file.

Functions I Using Functions

Separating Prototype and Implementation

4 # i n c l u d e < i o s t r e a m > 5 u s i n g n am esp ace s t d ; 6 7 # i n c l u d e " triangles. h " 8 9 i n t main ( ) 10 { 11 d o u b l e a l e g ( 3 ) , b l e g ( 4 ) , c l e g ( 5 ) ; 12 / / F u n c t i o n c a l l , r e s u l t s a v e d 13 b o o l o k T r i a n g l e = v a l i d _ t r i a n g l e ( a l e g , b l e g , c l e g ) ; 14 i f ( o k T r i a n g l e ) { 15 c o u t << " OK Triangle! " << a l e g << " " << b l e g << 16 " " << c l e g << e n d l ; 17 } 18 / / E x i t t h e main f u n c t i o n w i t h co d e 0 19 r e t u r n 0 ; 20 }

Keep in the same folder or

directory.

Note the

#include "triangles.h"

in the application source

file.