This presentation includes custom animations.
To view the animations, you must view the presentation in Slide Show mode
and activeX controls must be allowed.
If you have opened this lesson in PowerPoint, use the PowerPoint menus to
view it in slide show mode.
If you have opened this lesson in a browser and see a bar similar to that below,
click on the Slide Show icon
A notice similar to the one below may appear warning that ActiveX or
other scripts are disabled. Enable the controls for this website in order
to see the animations.
User-Defined Functions
This lesson discusses the purpose and syntax of user-defined functions in ANSI C:
Vocabulary:
body
calling function
header
function call
library function
prototype
user-defined function
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
A function is …
A named group of statements that can be called and evaluated and can return a value to the
calling statement. (www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/vac7.0/html/glossary/czgf.htm)
A named group of statements in a program that perform a task when called on.
(www.metromemetics.com/thelexicon/f.asp)
A subprogram which returns a value of a specified type which is invoked as part of an
expression. (www.adaic.org/docs/craft/html/glossary.htm)
Functions are not a new concept. You have been using functions in every program in this course.
A library (or built-in) function is one that is included as part of the language.
In C, library functions are defined in the header files (stdio.h, math.h, ctype.h, etc).
Some library functions that you have used are: printf(), scanf(), getchar().
A programmer can create new customized functions. A new function that is not part of the
library and is written by the programmer, it is called a user-defined function (UDF).
One user-defined function that you have written is main()
User-defined functions other than main() are sometimes called subfunctions.
User Defined Functions in ANSI C
A user defined function is a collection of code written by the programmer that can be
called as a unit from within another function in the program.
Functions serve several purposes:
1. Eliminate repetitious code
2. Improve modularization which
a) Eases understanding for humans
b) Simplifies maintenance
c) Increases portability
Consider how a user-defined function can be used to solve the following problem.
Final grades are based on the average of 2 midterms and a final. The midterms
are each worth 25% of the final grade and the final exam is worth 50%.
Example:
Test 1 84/100
0.25 * .84 = 0.21
Test 2 80/100
0.25 * .80 = 0.20
Final
0.50 * .90 = 0.45
90/100
= 0.86
Example: Click through to become familiar with
the following flowchart and code snippet.
main
GET
Test1Score
A
int main()
{
double Test1Score, Test1Possible;
double Test2Score, Test2Possible;
double Test3Score, Test3Possible;
printf("Enter Score on Test 1: ");
scanf("\n%lf", &Test1Score);
GET
FinalScore
printf("Enter points possible on Test 1: ");
scanf("\n%lf", &Test1Possible);
GET
Test1Possible
GET
FinalPossible
Test1Percent
= Test1Score /
Test1Possible
FinalPercent =
FinalScore /
FinalPossible
Test1Percent = Test1Score / Test1Possible;
printf("Enter Score on Test 2: ");
scanf("\n%lf", &Test2Score);
printf("Enter points possible on Test 2: ");
scanf("\n%lf", &Test2Possible);
Test2Percent = Test2Score / Test2Possible;
GET
Test2Score
CALCULATE
OVERALL
SCORE
printf("Enter Score on Final Exam: ");
scanf("\n%lf", &FinalScore);
OverallScore = 0.25 * Test1Percent
+ 0.25 * Test2Percent
+ 0.50 * FinalPercent
printf("Enter points possible on Final Exam: ");
scanf("\n%lf", &FinalPossible);
GET
Test2Possible
DISPLAY
OverallScore
Test2Percent
= Test2Score /
Test2Possible
STOP
FinalPercent = FinalScore / FinalPossible;
OverallScore = 0.25 * Test1Percent
+ 0.25 * Test2Percent
+ 0.50 * FinalPercent;
printf("\nThe overall score is %5.2f", OverallScore;
return 0;
{
A
main
A
GetTest()
GET
Test1Score
GET
FinalScore
TestScore
GET
Test1Possible
GET
GET
FinalPossible
TestPossible
Test1Percent
= Test1Score /
Test1Possible
FinalPercent ==
TestPercent
FinalScore
TestScore //
FinalPossible
TestPossible
GET
Test2Score
CALCULATE
OVERALL
SCORE
GET
TestScore
GET
TestPossible
OverallScore = 0.25 * Test1Percent
+ 0.25 * Test2Percent
+ 0.50 * FinalPercent
TestPercent =
TestScore /
TestPossible
GET
Test2Possible
DISPLAY
OverallScore
RETURN
TestPercent
Test2Percent
= Test2Score /
Test2Possible
A
STOP
main()
GET
Test1Score
Test1Percent =
GetTest()
GET
Test1Possible
Test1Percent
= Test1Score /
Test1Possible
GET
Test2Score
A
GetTest()
GET
FinalScore
FinalPercent =
GetTest()
GET
FinalPossible
GET
TestScore
FinalPercent =
FinalScore /
FinalPossible
CALCULATE
OVERALL
SCORE
GET
TestPossible
OverallScore = 0.25 * Test1Percent
+ 0.25 * Test2Percent
+ 0.50 * FinalPercent
TestPercent =
TestScore /
TestPossible
Test2Percent
=
GET
GetTest()
Test2Possible
DISPLAY
OverallScore
RETURN
TestPercent
Test2Percent
= Test2Score /
Test2Possible
A
STOP
main()
GetTest()
Test1Percent =
GetTest()
GET
TestScore
Test2Percent =
GetTest()
GET
TestPossible
FinalPercent =
GetTest()
CALCULATE
OVERALL
SCORE
OverallScore = 0.25 * Test1Percent
+ 0.25 * Test2Percent
+ 0.50 * FinalPercent
TestPercent =
TestScore /
TestPossible
RETURN
TestPercent
DISPLAY
OverallScore
STOP
main()
GetTest()
Test1Percent =
GetTest()
GET
TestScore
Test2Percent =
GetTest()
GET
TestPossible
FinalPercent =
GetTest()
CALCULATE
OVERALL
SCORE
OverallScore = 0.25 * Test1Percent
+ 0.25 * Test2Percent
+ 0.50 * FinalPercent
TestPercent =
TestScore /
TestPossible
RETURN
TestPercent
DISPLAY
OverallScore
STOP
main()
GetTest()
Test1Percent =
GetTest()
GET
TestScore
Test2Percent =
GetTest()
GET
TestPossible
FinalPercent =
GetTest()
CALCULATE
OVERALL
SCORE
OverallScore = 0.25 * Test1Percent
+ 0.25 * Test2Percent
+ 0.50 * FinalPercent
TestPercent =
TestScore /
TestPossible
RETURN
TestPercent
DISPLAY
OverallScore
STOP
There are 3 steps to coding and using a function:
•
•
•
Definition
Prototype
Call
The definition of a function is that part of the source code where the
programmer lists the instructions that will be executed when the
function is invoked. The definition comes AFTER main();
DEFINITION
int CalcArea( int Width, int Length)
{
int Area;
Area = Width * Length;
return Area;
}
The prototype of a function is an outline of the function provided to
the compiler. The prototype comes BEFORE main()
PROTOTYPE
int CalcArea( int Width, int Length) ;
DEFINITION
int CalcArea( int Width, int Length)
{
int Area;
Area = Width * Length;
return Area;
}
The function call is a line of code in one function that invokes another
(or the same) function.
PROTOTYPE
CALL
DEFINITION
int CalcArea( int Width, int Length) ;
int main()
{
int Side1;
int Side2;
int SquareFootage;
scanf("\n%d %d",&Side1, &Side2);
SquareFootage = CalcArea(Side1, Side2);
printf("\nTotal Square Feet is: %d", SquareFootage);
return 0;
}
int CalcArea( int Width, int Length)
{
int Area;
Area = Width * Length;
return Area;
}
What flowcharting symbol is used to represent a function call?
How many values can a function return?
0 or 1
What flowcharting symbol is used to represent a returned value?
RETURN
TestPercent
What flowcharting symbol is used when there is more text than
can fit into a regular shape?
After all steps in a function are completed, what is the next instruction executed?
The first instruction following the function call.
© Copyright 2026 Paperzz