Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Plan Ahead with Pseudocode!
You have completed Plan Ahead with Pseudocode!
Preview
Learn basic conventions for describing functions including function signature, passing arguments, and returning values.
Basic function format
function function_name
endfunction
Accept arguments
function function_name
pass in num1, num2, num3
endfunction
Return a value
function sum_numbers
pass in num1, num2, num3
set result to num1 + num2 + num3
return result
end function
Call a function
set sum to call sum_numbers with 5, 6, 7
Example pseudocode from video
function calculate_gpa
pass in student_grades
set grade_total to 0
for each grade in student_grades
if grade is not a 1, 2, 3, or 4
print "invalid grade"
print grade
print "can't complete calculation"
exit function
else add grade to grade_total
endif
endfor
set gpa to grade_total / number of grades
return gpa
endfunction
set reggie_grades to 4, 4, 3, 4
set reggie_gpa to call calculate_gpa with reggie_grades
print reggie_gpa
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In doing this, we're actually improving
our code by thinking through different
0:00
versions of our program, all without
actually having to write any code.
0:02
In other words, using pseudo code first,
as you think through and
0:05
improve the organization of
the code you're going to write.
0:09
If you'd like to follow along,
0:13
open the workspace associated with
this video in the gpa.text file.
0:15
With the function,
you want to identify the beginning and
0:19
the end of the function clearly like this.
0:23
In addition, you often pass one or
more values to a function.
0:25
Information the function can
use as part of its programming.
0:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up