Download STC 2016 Programming Language Storytime

Document related concepts

JavaScript syntax wikipedia , lookup

Functional programming wikipedia , lookup

Recursion (computer science) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Go (programming language) wikipedia , lookup

Comment (computer programming) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Subroutine wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Dirac delta function wikipedia , lookup

JavaScript wikipedia , lookup

C syntax wikipedia , lookup

Name mangling wikipedia , lookup

APL syntax and symbols wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Standard ML wikipedia , lookup

Function object wikipedia , lookup

C++ wikipedia , lookup

Transcript
Programming Language
Storytime
Sarah Kiniry
cPanel, Inc.
About Me
@SarahKiniry
#STC16
Sarah Kiniry (me!)
Technical Writer, former Stage
Manager and Box Office Manager,
needs-an-intervention level
Trekkie.
technicolorwriter.com
@SarahKiniry
Jason (hubby!)
Backend Perl developer,
extremely patient about being
my in-home tutor, taught me a
whole lot of The Things.
If you can learn to read,
you can learn to read code.
Just read the story!
1
What does the application do?
2
What text will the user see?
3
Which files or settings?
@SarahKiniry
#STC16
How do I find the story?
Code Clues
@SarahKiniry
#STC16
Function Names
Code Comments
1
What does the application do?
Code comments, function names,
setting names, and file locations.
2
What text will the user see?
Display methods, like print, and
other output functions.
Directories
Variable (Setting) Names
Variables
Print Functions
File Locations
Files
3
Which files or settings?
File names, file paths, and other
variables.
Comments
Text in code that the computer doesn’t try to run.
Instead, it’s there for the developer’s reference.
Sometimes, it’s also used to temporarily “turn off”
code without deleting it.
Comments
@SarahKiniry
#STC16
Single-line comments…
// Single-line comments
// require a comment
// for each line.
do(thing); // comment
Multi-line comments…
•
Comment character at the beginning and
end of the comment.
•
Generally, these exist on separate lines
from the code.
•
Comment character at the beginning of the
comment only.
•
A line break ends the comment.
•
Can be used on the same line as code (a
trailing comment).
/* Multi-line
comments use beginning
and end characters for
multiple lines. */
Single Line
@SarahKiniry
#STC16
//
Java, JavaScript, C, C#, PHP
#
PHP, Perl, Ruby, Python
Multi Line
@SarahKiniry
#STC16
/* Comment */
Java, JavaScript, C, C#, PHP
// Comment \
Comment
C
=pod
=cut
Perl
=begin
=end
Ruby
""" """
Python
Functions
Reusable code that performs one or more actions.
Sometimes, the code that you’re looking at is a
function, while sometimes it uses existing functions.
* This is a lazy use of the term. These actions can be
subroutines, methods, procedures, etc.
Functions
sub do_something {
my $value = @_;
if $value {
return "Yay!";
}
…
Function use…
•
Could be a custom-written function, or one
that’s built into the programming language.
•
Often, custom functions are defined in
other code, not in the same file.
@SarahKiniry
#STC16
Function definition…
•
Creating a function that’s used elsewhere.
•
Sets the function name.
•
Includes all of the code that will run
anytime the function is used.
do_something($value);
Using Functions
function($value)
function "$value"
@SarahKiniry
#STC16
Creating Functions
@SarahKiniry
#STC16
// This is the format to define a function.
class type functionname(parameters) {
code
}
*Java uses a “class” structure, so when you see this in code it’s going to be nested in a class.
Java*, C#
Creating Functions
@SarahKiniry
#STC16
// This is the format to define a function.
class type functionname(parameters) {
code
}
// This example defines a function.
public static void printyay() {
System.out.println ("Yay!");
}
*Java uses a “class” structure, so when you see this in code it’s going to be nested in a class.
Java*, C#
Creating Functions
@SarahKiniry
#STC16
// This is the format to define a function.
class type functionname(parameters) {
code
}
// This example defines a function.
public static void printyay() {
System.out.println ("Yay!");
}
// This uses the defined function.
printyay();
*Java uses a “class” structure, so when you see this in code it’s going to be nested in a class.
Java*, C#
Creating Functions
@SarahKiniry
#STC16
// This is the format to define a function.
class type functionname(parameters) {
code
}
// This example defines a function.
public static void printyay() {
System.out.println ("Yay!");
}
// This uses the defined function.
printyay();
*Java uses a “class” structure, so when you see this in code it’s going to be nested in a class.
Java*, C#
Yay!
Creating Functions
// This is the format to define a function.
type functionname(parameters) {
code
}
C
@SarahKiniry
#STC16
Creating Functions
// This is the format to define a function.
type functionname(parameters) {
code
}
// This example defines a function.
int printyay() {
printf("Yay!");
}
C
@SarahKiniry
#STC16
Creating Functions
// This is the format to define a function.
type functionname(parameters) {
code
}
// This example defines a function.
int printyay() {
printf("Yay!");
}
// This uses the defined function.
printyay();
C
@SarahKiniry
#STC16
Creating Functions
@SarahKiniry
#STC16
// This is the format to define a function.
type functionname(parameters) {
code
}
// This example defines a function.
int printyay() {
printf("Yay!");
}
// This uses the defined function.
printyay();
C
Yay!
Creating Functions
// This is the format to define a function.
function functionname(parameters) {
code
}
JavaScript, PHP
@SarahKiniry
#STC16
Creating Functions
// This is the format to define a function.
function functionname(parameters) {
code
}
// This example defines a function.
function multiply(a,b) {
return a * b;
}
JavaScript, PHP
@SarahKiniry
#STC16
Creating Functions
// This is the format to define a function.
function functionname(parameters) {
code
}
// This example defines a function.
function multiply(a,b) {
return a * b;
}
// This uses the defined function.
multiply(3,2);
JavaScript, PHP
@SarahKiniry
#STC16
Creating Functions
@SarahKiniry
#STC16
// This is the format to define a function.
function functionname(parameters) {
code
}
// This example defines a function.
function multiply(a,b) {
return a * b;
}
// This uses the defined function.
multiply(3,2);
JavaScript, PHP
6
Creating Functions
# This is the format to define a function.
sub functionname {
code
}
Perl
@SarahKiniry
#STC16
Creating Functions
# This is the format to define a function.
sub functionname {
code
}
# This example defines a function.
sub my_fn {
$variables = @_;
do_something($variables);
return $variables;
}
Perl
@SarahKiniry
#STC16
Creating Functions
# This is the format to define a function.
sub functionname {
code
}
# This example defines a function.
sub dothingstovars {
$variables = @_;
do_something($variables);
return $variables;
}
# This uses the defined function.
dothingstovars(“red”);
Perl
@SarahKiniry
#STC16
Creating Functions
@SarahKiniry
#STC16
# This is the format to define a function.
sub functionname {
code
}
# This example defines a function.
sub dothingstovars {
$variables = @_;
do_something($variables);
return $variables;
}
# This uses the defined function.
dothingstovars(“red”);
Perl
blue
Creating Functions
# This is the format to define a function.
def functionname(parameters)
code
end
Ruby
@SarahKiniry
#STC16
Creating Functions
# This is the format to define a function.
def functionname(parameters)
code
end
# This example defines a function.
def Texas(name)
var = "Howdy, " + name
return var
end
Ruby
@SarahKiniry
#STC16
Creating Functions
# This is the format to define a function.
def functionname(parameters)
code
end
# This example defines a function.
def Texas(name)
var = "Howdy, " + name
return var
end
# This uses the defined function.
Texas(Bob)
Ruby
@SarahKiniry
#STC16
Creating Functions
@SarahKiniry
#STC16
# This is the format to define a function.
def functionname(parameters)
code
end
# This example defines a function.
def Texas(name)
var = "Howdy, " + name
return var
end
# This uses the defined function.
Texas(Bob)
Ruby
Howdy,
Bob
Creating Functions
# This is the format to define a function.
def functionname(parameters):
code
return[value]
Python
@SarahKiniry
#STC16
Creating Functions
# This is the format to define a function.
def functionname(parameters):
code
return[value]
# This example defines a function.
def print_return(my_words)
print my_words
return[]
Python
@SarahKiniry
#STC16
Creating Functions
# This is the format to define a function.
def functionname(parameters):
code
return[value]
# This example defines a function.
def print_return(my_words)
print my_words
return[]
# This uses the defined function.
print_return(“Hey everybody!")
Python
@SarahKiniry
#STC16
Creating Functions
@SarahKiniry
#STC16
# This is the format to define a function.
def functionname(parameters):
code
return[value]
# This example defines a function.
def print_return(my_words)
print my_words
return[]
# This uses the defined function.
print_return(“Hey everybody!")
Python
Hey
everybody!
Variables
The names of stored values that code uses to
perform actions. This can mean strings (text),
numbers, or boolean values (true/false).
There are also methods of storing groups of values,
such as arrays or hashes.
Variables
(and arrays
and hashes)
@SarahKiniry
#STC16
variable_name
Java, JavaScript, C, C#, Python
$variable_name
PHP, Perl, Ruby
@variable_name
Perl, Ruby
%variable_name
Perl
Important Value Types
@SarahKiniry
#STC16
Files
example.txt, example.jpg, example.php
Directories
Linux: /example/directory or example/directory/
Windows: C:\example\directory or
..\example\directory\
Settings
managed, unmanaged; blue, green, red; 0, 1
Variables
my $file = example.txt;
$color = blue;
$do_something = 1;
@SarahKiniry
#STC16
Hello World!
The Hello World Story
1
What does the application do?
2
What text will the user see?
3
Which files or settings?
@SarahKiniry
#STC16
The Hello World Story
1
What does the application do?
This application displays a message to the user.
2
What text will the user see?
3
Which files or settings?
@SarahKiniry
#STC16
The Hello World Story
1
What does the application do?
This application displays a message to the user.
2
What text will the user see?
The user will see the text “Hello World.”
3
Which files or settings?
@SarahKiniry
#STC16
The Hello World Story
1
What does the application do?
This application displays a message to the user.
2
What text will the user see?
The user will see the text “Hello World.”
3
Which files or settings?
No other files or settings are involved.
@SarahKiniry
#STC16
Hello, Java!
/* This is a Hello World script. */
class HelloWorldApp {
public static void main(String[] args)
{
// Display the string.
System.out.println("Hello World!");
}
}
@SarahKiniry
#STC16
Hello, Java!
/* This is a Hello World script. */
class HelloWorldApp {
public static void main(String[] args)
{
// Display the string.
System.out.println("Hello World!");
}
}
@SarahKiniry
#STC16
Hello, Java!
/* This is a Hello World script. */
class HelloWorldApp {
public static void main(String[] args)
{
// Display the string.
System.out.println("Hello World!");
}
}
@SarahKiniry
#STC16
Hello, JavaScript!
<script language=“Javascript">
// Write to browser window.
document.write("Hello World!");
</script>
@SarahKiniry
#STC16
Hello, JavaScript!
<script language=“Javascript">
// Write to browser window.
document.write("Hello World!");
</script>
@SarahKiniry
#STC16
Hello, JavaScript!
<script language=“Javascript">
// Write to browser window.
document.write("Hello World!");
</script>
@SarahKiniry
#STC16
Hello, C!
/* Hello World */
void main()
{
printf("Hello World!");
}
@SarahKiniry
#STC16
Hello, C!
/* Hello World */
void main()
{
printf("Hello World!");
}
Hello, C!
/* Hello World */
void main()
{
printf("Hello World!");
}
Hello, C#!
/// Let’s say Hello.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
}
}
}
@SarahKiniry
#STC16
Hello, C#!
/// Let’s say Hello.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
}
}
}
Hello, C#!
/// Let’s say Hello.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
}
}
}
Hello, PHP!
// Tell them hello in PHP.
<?php
echo "Hello World!";
?>
@SarahKiniry
#STC16
Hello, PHP!
// Tell them hello in PHP.
<?php
echo "Hello World!";
?>
Hello, PHP!
// Tell them hello in PHP.
<?php
echo "Hello World!";
?>
Hello, Perl!
#!/usr/bin/perl
# We’ll call this a Perl of wisdom.
print "Hello World!";
@SarahKiniry
#STC16
Hello, Perl!
#!/usr/bin/perl
# We’ll call this a Perl of wisdom.
print "Hello World!";
Hello, Perl!
#!/usr/bin/perl
# We’ll call this a Perl of wisdom.
print "Hello World!";
Hello, Ruby!
#!/usr/bin/ruby -w
# First Perl, now Ruby? Shiny.
puts "Hello, world!"
=begin
Really though, who knew there were two
programming languages named after
gemstones, even if one is kind of
misspelled?
=end
@SarahKiniry
#STC16
Hello, Ruby!
#!/usr/bin/ruby -w
# First Perl, now Ruby? Shiny.
puts "Hello, world!"
=begin
Really though, who knew there were two
programming languages named after
gemstones, even if one is kind of
misspelled?
=end
Hello, Ruby!
#!/usr/bin/ruby -w
# First Perl, now Ruby? Shiny.
puts "Hello, world!"
=begin
Really though, who knew there were two
programming languages named after
gemstones, even if one is kind of
misspelled?
=end
Hello, Python!
# A lot of programming languages use
# hashes for their comment character.
print("Hello, World!")
""" But they tend to diverge when it comes
to multiline comments. """
@SarahKiniry
#STC16
Hello, Python!
# A lot of programming languages use
# hashes for their comment character.
print("Hello, World!")
""" But they tend to diverge when it comes
to multiline comments. """
Hello, Python!
# A lot of programming languages use
# hashes for their comment character.
print("Hello, World!")
""" But they tend to diverge when it comes
to multiline comments. """
The Harder Stuff
More JavaScript
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
JavaScript code example from w3schools.com
@SarahKiniry
#STC16
More JavaScript
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
JavaScript code example from w3schools.com
@SarahKiniry
#STC16
More JavaScript
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
JavaScript code example from w3schools.com
@SarahKiniry
#STC16
More JavaScript
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
JavaScript code example from w3schools.com
@SarahKiniry
#STC16
More JavaScript
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
JavaScript code example from w3schools.com
@SarahKiniry
#STC16
More JavaScript
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
JavaScript code example from w3schools.com
@SarahKiniry
#STC16
More JavaScript
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
JavaScript code example from w3schools.com
@SarahKiniry
#STC16
What’s The Story?
1
What does the application do?
2
What text will the user see?
3
Which files or settings?
@SarahKiniry
#STC16
What’s The Story?
1
What does the application do?
This application switches between two images.
2
What text will the user see?
3
Which files or settings?
@SarahKiniry
#STC16
What’s The Story?
1
What does the application do?
This application switches between two images.
2
What text will the user see?
“Javascript Can Change Images”
3
Which files or settings?
@SarahKiniry
#STC16
What’s The Story?
1
What does the application do?
This application switches between two images.
2
What text will the user see?
“Javascript Can Change Images”
3
Which files or settings?
pic_bulbon.gif and pic_bulboff.gif, width=100,
height=180
@SarahKiniry
#STC16
More JavaScript
JavaScript code example from w3schools.com
@SarahKiniry
#STC16
More JavaScript
JavaScript code example from w3schools.com
@SarahKiniry
#STC16
@SarahKiniry
#STC16
Questions?