Functions in Programming

SimplestLapisLazuli avatar
SimplestLapisLazuli
·
·
Download

Start Quiz

Study Flashcards

24 Questions

What is the data type of the variable txt4 after the execution of the given statements?

String

What is the result of the expression '5' + 5 in JavaScript?

55

What is the purpose of the conditional operator in JavaScript?

To assign a value to a variable based on some condition

What is the syntax to define a JavaScript function?

function functionname()

What is the data type of the variable x after the statement 'x = 5 + 5'?

Number

What is the purpose of logical operators in JavaScript?

To determine equality or difference between variables or values

What is the purpose of the assignment operator in JavaScript?

To assign a value to a variable

What is the data type of the variable x after the statement 'x = 5 + "5"'?

String

What is the purpose of the return statement in a function?

To specify the value that a function should return

What is the correct syntax for declaring a function with three parameters?

function functionName(parameter1, parameter2, parameter3)

What is the output of the following code: document.write(product(4,3))?

16

What is the purpose of the if statement in JavaScript?

To execute a block of code only if a condition is true

What is the correct syntax for an if statement in JavaScript?

if(condition) {code to be executed}

What will happen if you use uppercase letters (IF) instead of lowercase letters (if) in an if statement?

The code will generate a JavaScript error

What is the purpose of the switch statement in JavaScript?

To select one of many blocks of code to be executed

What is the correct way to declare a variable in JavaScript?

var d = new Date(); var time = d.getHours();

What is a characteristic of JavaScript variable names?

They must begin with a letter, an underscore, or a dollar sign

What is the scope of a variable declared within a JavaScript function?

Local

What is the result of the expression txt3=txt1+txt2 assuming txt1='What a very' and txt2='nice day'?

What a verynice day

What is the purpose of assignment operators in JavaScript?

To assign values to variables

What is the difference between local and global variables in JavaScript?

Local variables can only be accessed within a function, while global variables can be accessed anywhere

What is the result of the expression alert(' Global y = ' + y) assuming y=5?

Global y = 5

What is the purpose of the var keyword in JavaScript?

To declare a local variable

What is the result of the expression txt4=txt1+' '+txt2 assuming txt1='What a very' and txt2='nice day'?

What a very nice day

Study Notes

Functions in JavaScript

  • A function is a block of code designed to perform a particular task and can be executed by an event or by a call to the function.
  • A JavaScript function is defined with the function keyword, followed by a name, and then parentheses ().
  • Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

Function Syntax

  • function functionName(parameter1, parameter2, …) { code to be executed }

Return Statement

  • Functions that return a value must use the return statement.
  • Example: function product(a, b) { return a * b; }

Conditional Statements

  • if statement: executes code if a specified condition is true.
  • if...else statement: executes code if the condition is true and another code if the condition is false.
  • if...else if....else statement: selects one of many blocks of code to be executed.
  • switch statement: selects one of many blocks of code to be executed.

Conditional Statement Syntax

  • if (condition) { code to be executed if condition is true }

Example of if Statement

  • if (time < 12) { write a "Good morning" greeting }

Adding Strings and Numbers

  • When you add a number and a string, the result will be a string.

Examples of Adding Strings and Numbers

  • var x = 5 + 5;
  • document.write(x + "");
  • x = "5" + "5";
  • document.write(x + "");
  • x = 5 + "5";
  • document.write(x + "");
  • x = "5" + 5;
  • document.write(x + "");

Comparison Operators

  • Used in logical statements to determine equality or difference between variables or values.

Logical Operators

  • Used to combine conditions in logical statements.

Conditional Operator

  • Assigns a value to a variable based on some condition.
  • Syntax: variablename = (condition) ? value1 : value2;

Variables in JavaScript

  • Variable names are case sensitive.
  • Variable names must begin with a letter, an underscore (_), or a dollar sign ($).
  • Subsequent characters can be letters, digits, underscores, or dollar signs.

Local and Global Variables

  • Local variables: declared within a JavaScript function and can only be accessed within that function.
  • Global variables: declared outside a function and can be accessed by all scripts and functions on the web page.

Example of Local and Global Variables

  • function displaymessage() { var x = 3; alert("Local x = " + x + " , Global y=" + y); }
  • var y = 5; alert(" Global y = " + y);

Arithmetic Operators

  • Used for arithmetic operations such as addition, subtraction, multiplication, division, etc.

Assignment Operators

  • Used to assign values to JavaScript variables.

The + Operator

  • Can be used to add string variables or text values together.
  • Example: txt1 = "What a very"; txt2 = "nice day"; txt3 = txt1 + txt2; txt4 = txt1 + " " + txt2;

This quiz covers the basics of functions in programming, including the syntax and usage of function definitions and the return statement.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Function Fundamentals
3 questions

Function Fundamentals

InvincibleBeige avatar
InvincibleBeige
Python3 Function Definitions Quiz
10 questions
Untitled
10 questions

Untitled

SmoothestChalcedony avatar
SmoothestChalcedony
Acute Renal Failure: Causes and Definitions
16 questions
Use Quizgecko on...
Browser
Browser