CEN-103
Introduction to Programming
Fall 2025
(Lecture 6)
(Assoc. Prof. Deniz Dal)
FUNCTIONS
Functions are also computer programs. Problems to
be solved with the help of the computers are divided
into small manageable blocks called functions. This
method is called Divide and Conquer. Functions
might be built-in (e.g. sqrt) or user-defined. Each
function:
• has a name.
• usually gets input argument(s)
(parameter(s)). (scalar, vector, or matrix)
• usually returns a value or values back.
(scalar, vector, or matrix)
FUNCTION M-FILES IN MATLAB
A function m-file has two components:
1–The signature or prototype of the function (defined
in the first line.)
2–The definition of the function (starts from the
second line.)
FUNCTION SIGNATURE OR PROTOTYPE
function outputParameter=FunctionName(inputParameters 1, 2, …n)
function [outputParameters 1, 2,…, n]=FunctionName(inputParameters 1, 2, …n)
function outputParameter=FunctionName()
function outputParameter=FunctionName
function FunctionName(inputParameters 1, 2, …n)
The first line of a function m-file is called the signature or prototype of
that particular function. Before we start writing a function, we need to
think carefully about its signature. (Should it take input parameter(s)
or return value(s)?)
Rules to Consider While Creating Function M-Files
1-Each MATLAB function must start with the function keyword.
2-The FunctionName must be the same as the name of the function
m-file.
3-A MATLAB function should be called from the command window
with the function name and the parameters (if any) separated by
commas from each other in parentheses.
4-When calling a function, the caller and the callee must have an
equal number of input parameters.
Exercise 1: Write a function m-file that finds the distance between two
points in the two-dimensional space.
x1: x coordinate of point 1 x2: x coordinate of point 2
y1: y coordinate of point 1 y2: y coordinate of point 2
Distance.m
function dist=Distance(x1,y1,x2,y2)%Signature
dist=sqrt((x2-x1)^2+(y2-y1)^2);%Definition
COMMAND WINDOW
>> result=Distance(3,4,1,2)
>> Distance(3,4,1,2)%Returned value is assigned to ans
SCRIPT M-FILE SOLUTION OF THE PREVIOUS EXAMPLE
Distance.m
x1=input('Enter x1 coordinate: ');
y1=input('Enter y1 coordinate: ');
x2=input('Enter x2 coordinate: ');
y2=input('Enter y2 coordinate: ');
dist=sqrt((x2-x1)^2+(y2-y1)^2);
fprintf('Distance between 2 points entered: %f\n',dist);
COMMAND WINDOW
>> Distance
!!! IMPORTANT !!!
Function names in the signature lines of the function m-files can be
defined by Pascal case. In other words, Pascal case can be used for
naming function m-files, just like script m-files.
Variables used to represent input and output parameters in the
signature lines of the function m-files can be defined by Camel case.
In function m-files, data is usually not received from the user using the
input command. The data to be evaluated within the function is
transferred to the program as input parameter(s).
Function m-files ARE NOT EXECUTED by clicking the Run button in the
editor window. They are executed by entering parameter(s) from the
command window.
MAIN FUNCTION AND SUBFUNCTION
We can use another function (subfuction) that is already defined in a
function m-file, and exists under your working directory (current folder).
FunctionA.m (Main) FunctionB.m (Sub)
function result1=FunctionA(n) function result2=FunctionB(m)
result1=FunctionB(n)+5; result2=m*sqrt(m);
What does FunctionA basically do?
Order of Calls
Command Window -> FunctionA -> FunctionB ->
FunctionB(result) -> FunctionA(result) -> Command Window(result)
EXERCISE 2
Write a MATLAB program (in the form of a function m-file) that calculates
the result of the following function based on the x and y values it takes
as its input parameters. (Use MathFunction.m as the file name.)
2 ln( x) 1 x
f(x, y) x y x.y tan( )
log10 ( y ) y
What should be the signature of this function m-file?
Name?
Input Parameter(s)?
Output Parameter(s)?
What should be the definition of this function m-file?
HOW TO USE FUNCTIONS THAT RETURN ONLY 1
OR 0 AS A CONDITION IN if AND while
STRUCTURES
In our previous lectures, we used only relational and/or
logical operators when creating conditions that would be
used to control if and while statements.
From now on, we can also utilize user-defined
subfunctions that return either 1 or 0 as a condition in if
and while statements.
if IsPrime(n) while ~IfDigitsDifferent(n)
%do something %do something
end end
PROGRAMMING TIP
In computer programming, a notation is used for naming
the functions that return a value of 1 or 0 where Is or If is
added to the beginning of the MATLAB function names.
In this way, we can easily distinguish them from other
functions.
Examples of such function names are IsAnOddNumber,
IsAPrimeNumber, or IfDigitsDifferent.
EXERCISE 3
Write a MATLAB function that returns a value of 1 when
its input parameter is odd, and 0 otherwise. Name your
function m-file as IsAnOddNumber.m.
function result=IsAnOddNumber(number)
result=mod(number,2);
COMMAND WINDOW
>> IsAnOddNumber(9)
ans=
1
>> IsAnOddNumber(8)
ans=
0
EXERCISE 4
Write a MATLAB function that returns the number of odd
numbers between the lower and the upper limit it takes
as its input parameters.
In order for the program to work,
this subfunction must be defined
(available) under the current
function oddCounter=Exercise4(lowerLimit,
folder. upperLimit)
oddCounter=0;
for i=lowerLimit:upperLimit
if IsAnOddNumber(i)%Subfunction that returns 1 or 0
COMMAND WINDOW
oddCounter=oddCounter+1;
>> Exercise4(1,100)
end ans=
end 50
return Command
You can use the return command to early
terminate your MATLAB program.
If the return command is used in the main
program, the control is transferred into the
command window. If the return command is used
in a subfunction, the control is transferred to the
main function that calls it.
Exercise 5: Draw the flowchart of the program that finds the
roots of the second-order polynomial given in the form of
Ax²+Bx+C=0.
Start
Get A, B and C from the user
𝐝𝐞𝐥𝐭𝐚 = 𝐁𝟐 −𝟒 𝐀𝐂
True
delta<0
False
Print “Roots are Complex”
𝐱 𝟐=(− 𝐁 − √𝐝𝐞𝐥𝐭𝐚 )/(𝟐 𝐀 )
Print x1 ve x2
End
SOLUTION 1 SOLUTION 2
A=input('Enter Coefficient A: '); A=input('Enter Coefficient A: ');
B=input('Enter Coefficient B: '); B=input('Enter Coefficient B: ');
C=input('Enter Constant C: '); C=input('Enter Constant C: ');
delta=B^2-4*A*C; delta=B^2-4*A*C;
if delta<0 if delta<0
disp('Roots are Complex'); disp('Roots are Complex');
else return;%Early Termination
x1=(-B+sqrt(delta))/(2*A); end
x2=(-B-sqrt(delta))/(2*A); x1=(-B+sqrt(delta))/(2*A);
fprintf('Root 1: %f\n', x1); x2=(-B-sqrt(delta))/(2*A);
fprintf('Root 2: %f\n', x2); fprintf('Root 1: %f\n', x1);
end fprintf('Root 2: %f\n', x2);
SCRIPT M-FILES
SOLUTION 3
function Exercise5(A,B,C)
delta=B^2-4*A*C;
if delta<0 Note that the solution does not
disp('Roots are Complex'); return a specific value because
else the roots are not calculated in
x1=(-B+sqrt(delta))/(2*A); both the if and else block (it is
x2=(-B-sqrt(delta))/(2*A); done only in the else block).
fprintf('Root 1: %f\n', x1); Therefore, the assignment
fprintf('Root 2: %f\n', x2); operator is not used in the
end signature line.
1st M-FUNCTION SOLUTION
SOLUTION 4
function Exercise5(A,B,C) Note that the assignment
delta=B^2-4*A*C; operator is not used in the
if delta<0 signature line since the function
disp('Roots are Complex'); does not return a value.
return;%Early Termination
end
x1=(-B+sqrt(delta))/(2*A);
x2=(-B-sqrt(delta))/(2*A);
fprintf('Root 1: %f\n', x1);
fprintf('Root 2: %f\n', x2);
2nd M-FUNCTION SOLUTION
SOLUTION 5
function [isComplex,x1,x2]=Exercise5(A,B,C)
3rd M-FUNCTION SOLUTION
Exercise 6
Write a MATLAB function that returns the smallest and
largest of the 3 numbers it receives as its input
parameters.
Name your function m-file as MinimumMaximum.m.
Proof By Contradiction
In logic and mathematics, proof by contradiction
is a form of proof that establishes the truth or
the validity of a proposition, by showing that
assuming the proposition to be false leads to a
contradiction.
Exercise 7
Write a MATLAB function that returns a value of 1 when
its input parameter is even, and 0 otherwise. Name your
function m-file as IsAnEvenNumber.m.
function result=IsAnEvenNumber(number)
result=0;%Assume that the number is not even
COMMAND WINDOW
if mod(number,2)==0
>> IsAnEvenNumber(8)
result=1;%Change
ans= your assumption
end 1
>> IsAnEvenNumber(9)
ans=
0
Exercise 8
Write a MATLAB function that returns a value of 1 when
its input parameter is a prime number, and 0 otherwise.
Name your function m-file as IsPrime.m.
MysteriousFunction.m
function MysteriousFunction(n) What is the functionality of
clc; the MysteriousFunction?
if (n<2)||(n>20) What does
disp('Entry is not in the Expected Range.'); MysteriousFunction(10)
return; print on the screen when it
end is called from the
for i=1:n command window?
if (i==1)||(i==n)
for j=1:n
fprintf('#');%Print # on the Screen
end Solution: In the function,
fprintf('\n'); you must put the number
else 10 wherever you see n and
fprintf('@');%Print @ on the Screen run the function line-by-
for j=2:(n-1) line, as if you were the
fprintf(' ');%Print 1 Empty Space on the Screen MATLAB interpreter.
end
fprintf('@\n');
end
end