Title: MINS114
1Chapter 3
Functions
2Overview
- 3.2 Using C functions
- Passing arguments
- Header files libraries
- 3.4-5 Writing C functions
- Prototype
- Definition
- Parameters
3Functions
- Every C program must have a main function
- execution starts by looking for a main
- All other functions are called directly from
main, or indirectly through a chain of function
called from main - Function Calls
- One function calls another by using the name of
the called function next to ( ) enclosing an
argument list - ex. strlen(FirstName)
- A function call temporarily transfers control
from the calling function to the called function
4Function call syntax
- FunctionName(Argument List)
- The argument list is a way for functions to
communicate with each other by passing
information - The argument list can contain 0 or more actual
arguments, separated by commas
5What does the function do?
- The function uses it actual arguments (inputs)
- to return a calculation
- To produce a side-effect
- The function's return value is substituted for
the function call in the expression - If the function does things other than a
calculation (such as print a message to the
screen), it is said to produce side-effects
6Example of calling a function
include ltiostreamgt include ltcmathgt using
namespace std int main() float x, root
cout ltlt "Enter a number " cin gtgt x root
sqrt(x) cout ltlt endl ltlt "The square root of "
ltlt x ltlt " is " ltlt root ltlt "." ltlt endl
return 0
7Using library functions
- We've already seen how to use library-provided
objects like cin and cout to manage I/O - Libraries also contain the implementation of
functions - A library has 2 parts
- Interface (stored in a header file) tells what
items are in the library and how to use them - Implementation (stored in another file) contains
the definitions of the items in the library - include ltiostreamgt
- Refers to the header file for the iostream
library needed for use of cout and endl
8Some Mathematical Library Functions
9Some Mathematical Library Functions
10User Defined Functions
- In addition to the functions in libraries,
programmers can use functions that they write on
their own - Using functions for common code segments can
improve your program in several ways - more readable
- is easier to update
- modular code
11Two parts of a function definition
Heading
- int Cube ( int n )
-
- return n n n
-
- The heading declares the functions name,
specifying its return type and the name and type
of each of its parameters - The body is a compound statement (block) which
defines the behavior of the function
Body
12What is in a heading?
int Cube ( int n )
13What is in a prototype?
- A prototype looks like the heading, followed by a
semicolon - parameter list must contain the type of each
parameter, but names are optional - Prototypes are used to declare functions before
they are defined - float volume(float, float)
- //C does require parameter names in prototypes
- float volume(float height, float radius)
- //it is good style to include them
14- include ltiostreamgt
- int GCD (int n1, int n2) // prototype
- using namespace std
- int main()
-
- int x, y
- cout ltlt "enter 2 positive integers "
- cin gtgt x gtgt y
- cout ltlt "The GCD of " ltlt x ltlt " and " ltlt y
- ltlt " is " ltlt GCD( x, y) ltlt endl
- cout ltlt "The GCD of " ltlt x ltlt " and 100 "
- ltlt " is " ltlt GCD( x, 100) ltlt endl
- cout ltlt "The GCD of " ltlt y ltlt " and " ltlt xx - 1
- ltlt " is " ltlt GCD( y, xx -1) ltlt endl
- return 0
15A program with several functions
function prototypes main function
Square function
Cube function
16A complete program prototypes
- include ltiostreamgt
- int Square (int) // prototypes
- int Cube (int)
- using namespace std
- int main()
-
- cout ltlt "The square of 27 is "
- ltlt Square (27) ltlt endl
- cout ltlt "The cube of 27 is "
- ltlt Cube (27) ltlt endl
- return 0
17A complete program definitions
- int Square (int n)
-
- return n n
-
- int Cube (int n)
-
- return n n n
-
18void function has no return value
-
- void DisplayMessage(int n)
-
- cout ltlt "I have liked math for "
- ltlt n ltlt " years" ltlt endl
- return
-
19Classified by location
Always appear in a function call, using
variables whose scope is within the calling
statement.
Always appear in the function heading, or
function prototype. Their scope is only within
the function definition.
20Equivalent terms
- The term "argument", as used in this slideshow,
is equivalent to - Actual argument ? used in our text
- Actual parameter (confusing, but used by others)
- The term "parameter", as used in this slideshow,
is equivalent to - Formal parameter ? used in our text
21Scope
- A variable or constant can be declared outside of
any function - Such variables or constants are known as globals
- Globals can be accessed from anywhere in a
program - Variables declared in a function are known as
local variables - Local variables can only be referenced from
within the function in which they are declared - You may use global constants, but no CS201
program should use global variables
22Drivers and Stubs
- When building a large project, it is sometimes
helpful to test each function in isolation - This allows the programmer to test a single
function before the entire program is written - The function to be tested must be called
- A simple main function, called a stub, can call
the function with appropriate arguments - The function to be tested might call other
functions which have not yet been implemented - Use functions which match the signature of these,
but which do no real work, are called stubs
23Strings
- C allows programmers to create new data types,
called classes - Many classes have been implemented in the
standard libraries - The ltstringgt library implements the string class,
which is used to store a sequence of characters
24Declaring a string
- If you include the string library in your
program, you can declare string variables - include ltstringgt string first_name,
last_name - You can assign string literals to a string
variable - last_name "Doe" first_name "John"
25String operations
- Mathematical operations, such as multiplication
and division, have no meaning for strings - But there are operations that do make sense
- String concatenation full_name first_name
last_name - Nth position
- char first_initial first_name0
26String member functions
- There are several functions designed for strings
- Called member functions, they are invoked
differently int size first_name.length() - int location first_name.find("o")
- There is also a function that allows a programmer
to read an entire line of input into a string
cin gtgt my_string //reads one word getline(cin,
my_string) //reads whole line