Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Pseudocode for Functions1:52 with Reggie Williams
Learn basic conventions for describing functions including function signature, passing arguments and returning values.
Basic function format
function function_name endfunction
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
Let's change the pseudo code from the last video to describe a reusable function
In doing this, we're actually improving our code by thinking through different
versions of our program, all without actually having to write any code.
In other words, using pseudo code first, as you think through and
improve the organization of the code you're going to write.
If you'd like to follow along,
open the workspace associated with this video in the gpa.text file.
With the function, you want to identify the beginning and
the end of the function clearly like this.
In addition, you often pass one or more values to a function.
Information the function can use as part of its programming.
Here's a simple way to indicate that.
Most functions return to value.
So you'll add that usually at the end like this.
Let's take the code from the last video and change it.
I'll add a line to mark the beginning of the function and the end of the function.
And then add a line indicate that you pass in the grades for the student.
Finally, instead of printing the GPA, we just returned it.
All of the rest of the code is the same, it's just now wrapped into a function.
To use that function or call it, you would do something like this.
First, create a list of grades.
Then create a new variable named reggie_gpa that you store
the results of the running function.
You can use the word call to indicate that you're calling the function and
use with to indicate what information you're passing to the function.
Finally, you can do something with that new variable like print it out.
That's most of what you need to know to write a natural language description of
To help you start using pseudocode, I've provided a reference sheet covering
the different pseudocode conventions I've shown you in this workshop.
You'll see that after this video.
Have fun writing pseudocode.
You need to sign up for Treehouse in order to download course files.Sign up