Department of Computer
Science and Information
Technology
CSC 201
Computer
Programming I
Lab 8:
Functions
TA. Fatima AbdelMagid
Student Name Nouran Issam Elharouny
Student ID 1093408
Section Number 11
Lab Rules and Grading Policy
1. Submission:
o Submit solutions as a Word/PDF via Blackboard, including screenshots of MATLAB
commands and output. No Teams submissions will be accepted.
o Include your name as a comment in the MATLAB file before taking
screenshots. Screenshot answers with no name and ID will not be considered.
2. Grading:
o Labs: 9 labs / 1 mark each.
o Lab Tests: 3 lab tests / 2 marks each.
o Full marks if submitted correctly within the lab duration.
3. Late Submission:
o Same Day midnight 11:59: 30% deduction.
o Next Day and after: Zero marks.
4. Attendance:
o Zero marks if you submit the lab without attending the session.
5. Makeup Labs:
o One makeup lab allowed for medical emergencies with proof, at the end of the
semester.
6. Academic Integrity:
o Any student found attempting to copy will receive a score of zero for the lab.
Lab 8: Functions
I. Define a Function:
Typically, a function is saved in a .m file with the same name as the function:
function [output1, output2] = function_name(input1, input2)
% Function code
end
II. Call the function:
To use the created function, you can call it with the wanted inputs and save its outputs:
[output_1, output_2] = function_name(input_1, input_2);
III. Anonymous Functions:
Anonymous functions are single-line functions can be defined without a separate file
using @:
Hypotenuse_func = @(a,b) a^2 + 3^2; % Define an anonymous
function for finding the hypotenuse of a right angle triangle
h = Hypotenuse_func(2,3); % Call the function
Questions: Add a comment with your name and ID.
IV. Task 1 – 0.3 Marks
A. Question
• Write a user-defined MATLAB function for the following math function:
𝑟(𝜃) = 3𝑠𝑖𝑛(3 𝑐𝑜𝑠(0.5 𝜃))
• The input to the function is 𝜽 (theta) (in radians) and the output is r. Write the function
such that 𝜃 can be a vector.
o Use the function to calculate r(π/6) and r(5π/6)
o Use the function to find 𝑟(𝜃) for 0 < 𝜃 < 4 𝜋
B. Answer Screenshot:
V. Task 2 – 0.3 Marks
A. Question
• Write a MATLAB function that determines whether a given integer is a prime number. A
prime number is a positive integer greater than 1 that has no positive divisors other than
1 and itself. Do not use MATLAB's built-in isprime function. Complete the below code:
???? = isPrimeNumber(????)
% Check if n is less than or equal to 1
if ??????
result = ????; % not a prime number
return;
?????
% Check divisibility from 2 up to the square root of n
for i = 2:sqrt(????)
if ?????????????
??????????? % Not a prime number if divisible
?????????
?????
end
% If no divisors were found, it's a prime
??????????
end
B. Answer Screenshot:
VI. Task 3 – 0.4 Marks
A. Question
• Write an anonymous MATLAB function that converts speed from meters per second (m/s)
to kilometers per hour (km/h).
• For the function name and arguments, use kmh = msToKmh(ms).
1 𝑚/𝑠 𝑖𝑠 𝑒𝑞𝑢𝑎𝑙 𝑡𝑜 3.6 𝑘𝑚/ℎ
• The input argument ms is the speed in meters per second, and the output argument kmh
is the speed in kilometers per hour (rounded to the nearest integer).
• Use the function to convert a speed of 15 m/s to km/h.
B. Answer Screenshot: