Skip to main content
Menu
CodeBabes
  • Create new account
  • Request new password

or

  • Sign in with Twitter
  • Code
  • Babes
  • Sign Up
  • Log In

Looks like you missed a quiz. Go back and pass that shit!


NO STRIP FOR YOU!

Share the Love

  • Tweet

Programming with Functions

Alright, are you ready for the “hard” stuff? We’ve talked about where you can stick your data, but what about storing actions, stuff that manipulates that data? We need functions.

We’ve already seen some functions in action, like prompt and alert. These functions are baked into the language. You can just use them. When you pass a value into them, that value is called an argument.

When you are using alert you are calling the alert function. You’re saying hey, function do some shit.

But you can also write your own functions which is where things start to get interesting. If you weren’t interested already. (Push up)

Let’s create a simple function named cockBlock. Write the keyword “function”, then the name of the function, cockBlock, then parentheses, which remain empty for a simple function like this, then a set of braces to define the block that the function will execute when called. Inside there we’ll put the alert, "keep it in your pants." It’s good to have a function name relate to what it’s actually doing.

Now let’s go back to our html page and refresh. You’ll see that nothing happens. This is because you have to call the function before it will run.

To call a function, just write out the function name, the two parentheses and a semicolon. When you refresh the page you’ll see an alert pop up.

Let’s get a little more advanced. Functions can also contain variables. So let’s create a function called addNumbers. In the function block we’ll add the variables A equals 2 and B equals 10. Then sum is equal to A plus B. We use the return keyword to get the function to output a value when called. We’ll do return sum. Now if we log the function call, we’ll see 12.

But that’s pretty rigid, the only thing we can get out is 12. That’s where arguments come in. Instead of putting the variables in the function block, we’ll put them in the parentheses after the function name, like this: (gesture)

Now the A and B values are going to be passed in as arguments when the function is called. Call the function addNumbers and then in the parentheses of the function call we put the arguments, let’s do 7 and 11. The great thing about functions is they can be used over an over. They’re a great way to make your code more modular.

So that’s it for the intro to programming lesson thanks for trying to pay attention.

Questions or Comments?

Lessons

  • Variables in Programming
  • Programming Intro Conditional Statements
  • Programming Intro to Arrays
  • Programming with Loops
  • Programming with Functions
Programming Virgin with Javascript

Downloads

SignUp & Subscribe

What the What?

Lessons: 41

Much more to come!

50/50 Entertainment and Education. Learning doesn't have to be boring :)

Our Philosophy

Contact

  • support
  • Tweets!
  • Youtube
  • Google Plus
  • Facebook

Stuff

  • Blog
  • Sign Up
  • All Babes
  • All Courses
  • Become a CodeBabe!
  • MAKE A LESSON WITH CODEBABES!

Recent Courses

Internet Virgin
SEO Virgin
HTML Virgin
CSS Virgin
Programming Virgin with Javascript

Learn to Code with Babes

Copyright © 2025 CodeBabes.com • Made with

  • Privacy Policy
  • Terms of Use
Close Menu
  • Blog

  • All Courses
  • Android Virgin
    • Android Studio Tour
    • App Layout in Android Studio
    • Java Variables, Java Data Types & Android Views
    • Java Event Handling in Android
    • Android Intents, OOP This, Variable Scope
    • Passing the Data with Android Intents & Finish
    • Complete the Android App with Launcher Icons & APK
    • Android Happy Ending
  • Internet Intro
    • Backend Development
    • Front End Web Development
    • Programming Tools you Need
    • Hosting and Domain Names
  • HTML Virgin
    • HTML Text Tags
    • HTML Links and Images
    • HTML Layout with HTML 5
    • HTML Tables
  • CSS Virgin
    • Where to Stick your CSS
    • CSS Properties, Sizes and Colors
    • CSS Selectors
    • CSS Box Model
  • Programming Virgin
    • Variables in Programming
    • Programming Intro Conditional Statements
    • Programming Intro to Arrays
    • Programming with Loops
    • Programming with Functions
  • PHP Virgin
    • PHP Variables
    • PHP Conditionals
    • PHP Arrays
    • PHP Loops
    • PHP Functions
  • SEO Virgin
    • Make Google Love You
    • Understanding Keyword SEO
    • Optimizing for SEO
    • Technical SEO