Title: Statements?Functions
1Statements?Functions
- Big programs get complicated very fast. Small
programs are simpler. - Key CS idea Decomposition
- Break big complicated sets of instructions into
smaller, simpler modules - In C, these modules are functions
- function a separate named block of statements
that you call by name in order to execute
a block of statements statement1
statement2 ...
2A function...
- ...is a "black box", a mini program with
- a simple set of inputs
- simple output
- a body of statements
- ...performs one well-defined task
- e.g. printf only prints to the screen
3Function essentials
- A function has
- a unique name
- a body
- some (optional) inputs (called arguments)
- one (optional) output (called return value)
- Just like variables, before a function can be
used it must be - declared ( specify name, arguments, return value
type) - defined ( write the body)
4Functions you know already
- main()
- the first (and last) function executed when the
program runs - printf(), scanf()
- You can invoke (call) other functions from within
main() - You can write your own functions which in turn
may call other functions and so on.
5How to use existing functions
- Use include directives to tell C where to find
the declarations of these functionsinclude
ltstdio.hgt / includeltXgt "search the usual
places for file X"/include "coatrack.h"/
include "X" "search current directory for
file X"/ - Call the functions by name in your
programs.scanf("d",numhats)isFull
hatCheck(numhats)
6How to write a new function
- Declare the function
- Write a function prototype that specifies
- the name of the function
- the type of its return value
- its list of arguments and their types
- Define the function
- Write the block of statements (body) of the
function
7Declaring a function
- The syntax of a function declaration (formally
called prototype) contains - The type of the return value of the function
- if the function does not return anything, the
type is void - The name of the function
- same rules as for variable naming
- An argument list made up of the input variables
names and types. This is enclosed in parentheses - return_type function_name ( argument_list )
- Function prototypes are placed between the
preprocessor directives and main().
8Defining a function
- The syntax of a function definition is
- return_type function_name ( argument_list )
- statements
-
similar to prototype but no semicolon
If the return type is not void, the function
must have a return statement which stops the
function and sets its output value
9Local variables
- Variables declared inside a function are
meaningful ONLY within that function. - This means that two different functions can use
the same names for their local variables without
conflict (just like two different people can have
the same name)
10Global variables
- Variables declared outside of all functions
(before main()) apply to ALL parts of a program. - They can be used and modified in any function.
- When a global variable is modified, the new value
is available anywhere in the program. - Global variables should be avoided
- They make it difficult to split the program in
separate pieces - They make it difficult to debug the program
- They are almost never necessary.
11Examples of function prototypes
- double convert_F_to_C (double degreesF)
- function convert_F_to_C takes as argument a
double variable named degreesF and returns a
double value - int gcd(int num1, int num2, int num3)
- function gcd takes three integers as arguments
and returns an integer value. - void make_tea ( double water, int tea_bags)
- This function does not return value. Its return
type is void.
12Example without functions
includeltstdio.hgt int main() /Farenheit-to-Ce
lsius / double degF,degC, ratio printf("Ente
r degrees F ") scanf("lf", degF)
/ F-to-C conversion
/ ratio 5.0 / 9.0 / We will unify this
/ degC (degF-32)ratio / set of
statements to / /
make a new function. / printf("f degrees F
are f degrees C\n",degF,degC)return
0 What if we need this conversion step in
many different places in our program?
13Example with functions
includeltstdio.hgt double F_to_C (double
degreesF) int main() /Farenheit-to-Celsius
/ double degF,degC, ratio printf("Enter
degrees F ") scanf("lf", degF) degC
F_to_C (degF) printf("f degrees F are f
degrees C\n",degF,degC)return 0 double
F_to_C (double degreesF) const double ratio
5.0/9.0 return (degreesF-32)ratio
Declare the function (Function prototype)
Call the function when you need it.
Define the function.
14return statement
- A function returns a computed value back to its
caller via a return statement. - A function with a non-void return type must
always have a return statement. - Code that follows a return statement is never
executed.
15return statement
includeltstdio.hgt double compute_average (int
num1, int num2) int main () double
average average compute_average(24,
68) return 0 double compute_average (int
num1, int num2) double average average
(num1 num2) / 2.0 return average
this becomes the value of that expression
16return statement
int main () printf("f\n", compute_average(24,
68) ) return 0 double compute_average (int
num1, int num2) double average average
(num1 num2) / 2.0 return average
A call to a function that returns a value
mayappear in a printf(). It is an expression!
17return statement
int main () print_info() return 0 void
print_info() printf("Name Vana
Doufexi\n") printf("Phone 847-491-5708\n") re
turn / optional /