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
Let's change the pseudo code from the last
video to describe a reusable function
0:01
named student_grades.
0:05
In doing this, we're actually improving
our code by thinking through different
0:07
versions of our program, all without
actually having to write any code.
0:11
In other words, using pseudo code first,
as you think through and
0:14
improve the organization of
the code you're going to write.
0:18
If you'd like to follow along,
0:22
open the workspace associated with
this video in the gpa.text file.
0:24
With the function,
you want to identify the beginning and
0:28
the end of the function clearly like this.
0:32
In addition, you often pass one or
more values to a function.
0:34
Information the function can
use as part of its programming.
0:38
Here's a simple way to indicate that.
0:41
Most functions return to value.
0:43
So you'll add that usually
at the end like this.
0:44
Let's take the code from
the last video and change it.
0:47
I'll add a line to mark the beginning of
the function and the end of the function.
0:50
And then add a line indicate that you
pass in the grades for the student.
0:57
Finally, instead of printing the GPA,
we just returned it.
1:03
All of the rest of the code is the same,
it's just now wrapped into a function.
1:07
To use that function or call it,
you would do something like this.
1:12
First, create a list of grades.
1:16
Then create a new variable
named reggie_gpa that you store
1:19
the results of the running function.
1:23
You can use the word call to indicate
that you're calling the function and
1:26
use with to indicate what information
you're passing to the function.
1:29
Finally, you can do something with
that new variable like print it out.
1:33
That's most of what you need to know to
write a natural language description of
1:36
a program.
1:40
To help you start using pseudocode,
I've provided a reference sheet covering
1:41
the different pseudocode conventions
I've shown you in this workshop.
1:44
You'll see that after this video.
1:47
Have fun writing pseudocode.
1:50
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