Title: Writing Functions
1Chapter 4
2Functions
- We have seen printf(), scanf() and various math
functions - Top-down, modular design easily implemented by
translating each module into a separate function - C depends very heavily on functions
- ANSI-C standard functions
- Compiler provided functions
- User defined functions
3Why Use Functions?
- Modularity
- Readability
- Debugability
- Repeatability
- Reusability
4Function Execution
- The function call transfers execution to the
function - Execution continues with the statements in the
function until there are no more - At that point, execution will return to where it
left off at the call
5The Function Definition
- All the statements in the function
- Tells C what actions to take in the function
- Also called the body of the function
- Must be enclosed inside
void CalculateSquare(void) int number
printf("Enter a number ") scanf("i",
number) printf("Your number squared is
i.\n", number number)
6The Function Call
- The function call (or invocation) is where the
function is used in the code of some other
function
void CalculateSquare(void) void main(void)
printf("CALCULATING SQUARES.\n\n")
CalculateSquare() printf("\nWe are
done!\n")
7The Function Prototype
- The function declaration tells C that the
function can be called later in the program - The declaration must appear before the function
is called - Also called the function Declaration
void CalculateSquare(void) void main(void)
printf("CALCULATING SQUARES.\n\n")
CalculateSquare() printf("\nWe are
done!\n")
8Example of a Function
include ltstdio.hgt void CalculateSquare(void) voi
d main(void) printf("CALCULATING
SQUARES.\n\n") CalculateSquare()
printf("\nWe are done!\n") void
CalculateSquare(void) int number
printf("Enter a number ") scanf("i",
number) printf("Your number squared is
i.\n", number number)
Output
CALCULATING SQUARES.
Enter a number
4
Your number squared is 16.
We are done!
9Return Values
- Sometimes we want some value back from a function
- done with return values - A return value is substituted for the function
call when the function is done - The return value must have a data type, this is
the first word in the declaration (and
definition) - void means no return value
10The return Statement
- A function executes until
- The closing brace is reached
- Or a return statement is executed
- The return statement determines the return value
- return expression
- return / for return type void /
- Usually the return statement should be the last
statement in the function
11Function Return Example
include ltstdio.hgt int CalculateSquare(void) void
main(void) int square
printf("CALCULATING SQUARES.\n\n") square
CalculateSquare() printf("Your number
squared is i.\n", square) printf("\nWe are
done!\n") int CalculateSquare(void) int
number printf("Enter a number ")
scanf("i", number) return numbernumber
Output CALCULATING SQUARES. Enter a number
4 Your number squared is 16. We are done!
12Passing Values
- Giving a functions values to work with
- Function calls can pass several values
- FunctionName(argument, ..., argument)
- You need to declare parameters for this
- returnType FunctionName(declaration, ...,
declaration) - There must be a match between the function call
and the function declaration/definition
13Example
include ltstdio.hgt int CalculateSquare(int
num) void main(void) int square int
number printf("CALCULATING
SQUARES.\n\n") printf("Enter a number ")
scanf("i", number) square
CalculateSquare(number) printf("Your number
squared is i.\n", square) printf("\nWe are
done!\n") int CalculateSquare(int num) int
numSquared num num return numSquared
Output CALCULATING SQUARES Enter a number
4 Your number squared is 16 We are done!
14Example
include ltstdio.hgt void CalculateInterest(int
balance, double interestRate) main() int
balance double interestRate
printf("Enter balance and interest rate ")
scanf("i f", balance, interestRate)
CalculateInterest(balance, interestRate) void
CalculateInterest(int balance, double
interestRate) double totalInterest
totalInterest balance interestRate / 100
printf("The total interest is .2lf\n",
totalInterest)
15Example
include ltstdio.hgt double CalculateInterest(int
balance, double interestRate) main() int
balance double interestRate,
totalInterest printf("Enter balance and
interest rate ") scanf("i lf", balance,
interestRate) totalInterest
CalculateInterest(balance, interestRate)
printf("The total interest is .2lf\n",
totalInterest) double CalculateInterest(int
balance, double interestRate) double
totalInterest totalInterest balance
interestRate / 100 return totalInterest
16Function Prototype
- This is the ANSI-C standards for function
declarations - The prototype must state
- The return type
- The function name
- The data types of the parameters
- It need not state the names of the parameters
- But it is a good idea to include the names
17Assignment
- Write a program that will calculate the area of a
circle using functions - Function to get the data Ask the user to enter
the diameter (integer type) and then validate the
diameter (gt 0). This is to be done in a function
that will also return the diameter to the calling
function. - Function to calculate the area Pass the
diameter, calculate the area, and then return the
area to the calling function. - Function to display the area Pass the area,
display it, and return nothing to the calling
function.