Podcast
Questions and Answers
What is the primary purpose of a function in programming?
What is the primary purpose of a function in programming?
Which of the following statements correctly describes calling a function?
Which of the following statements correctly describes calling a function?
What does the 'void' keyword indicate when declaring a function?
What does the 'void' keyword indicate when declaring a function?
In the example given, what will be the output if myFunction() is called three times?
In the example given, what will be the output if myFunction() is called three times?
Signup and view all the answers
What is meant by parameters in the context of functions?
What is meant by parameters in the context of functions?
Signup and view all the answers
What will happen if a function is called without being declared first?
What will happen if a function is called without being declared first?
Signup and view all the answers
Which part of the function declaration specifies the actions to be performed?
Which part of the function declaration specifies the actions to be performed?
Signup and view all the answers
What does the 'printf' function do in C programming?
What does the 'printf' function do in C programming?
Signup and view all the answers
What is an in-built function?
What is an in-built function?
Signup and view all the answers
Numeric functions in SQL are sometimes called mathematical functions.
Numeric functions in SQL are sometimes called mathematical functions.
Signup and view all the answers
What is the purpose of SQL string functions?
What is the purpose of SQL string functions?
Signup and view all the answers
What does the CHAR_LENGTH() function do?
What does the CHAR_LENGTH() function do?
Signup and view all the answers
What is the output of the SQL command SELECT ascii('t');?
What is the output of the SQL command SELECT ascii('t');?
Signup and view all the answers
What is the function of the CONCAT() function in SQL?
What is the function of the CONCAT() function in SQL?
Signup and view all the answers
What does the LEFT(str,len) function return?
What does the LEFT(str,len) function return?
Signup and view all the answers
What does the INSTR(str, substr) function do?
What does the INSTR(str, substr) function do?
Signup and view all the answers
What does the REVERSE(str) function return?
What does the REVERSE(str) function return?
Signup and view all the answers
What format does the date() function return in SQL?
What format does the date() function return in SQL?
Signup and view all the answers
What does the COUNT() function do in SQL?
What does the COUNT() function do in SQL?
Signup and view all the answers
Which SQL functions are considered aggregate functions?
Which SQL functions are considered aggregate functions?
Signup and view all the answers
What keyword can be used to exclude duplicate values from the aggregate function results?
What keyword can be used to exclude duplicate values from the aggregate function results?
Signup and view all the answers
What SQL clause is used to summarize or aggregate data?
What SQL clause is used to summarize or aggregate data?
Signup and view all the answers
Study Notes
Functions in Programming
- A function is a block of code that executes only when called.
- Data passed into a function is referred to as parameters.
- Functions promote code reuse: define once, use many times.
Predefined Functions
- Common functions include
main()
for execution andprintf()
for output. - Example of using
printf()
to display "Hello World!" inmain()
function.
Creating a Function
- Define a function by specifying its name, followed by parentheses and curly brackets.
- Example syntax:
void myFunction() { // code to be executed }
. -
void
indicates the function does not return a value.
Calling a Function
- Functions are executed when called, not immediately upon declaration.
- To call a function, write its name followed by parentheses and a semicolon.
- Example:
myFunction();
will execute the code defined inmyFunction()
.
Multiple Calls and Nested Functions
- Functions can be called multiple times within the same program.
- Functions can also call other functions, demonstrating hierarchical execution.
Example of Function Use
- Function
calculateSum()
computes the sum of two variablesx
andy
. - The output of
calculateSum()
displays "The sum of x + y is: 15". - The true potential of functions is realized by allowing dynamic inputs (parameters) for calculations.
Importance of Functions
- Functions help organize code and reduce redundancy by encapsulating behavior.
- The next chapter will cover how parameters enhance the functionality of functions.
In-built Functions
- In-built functions are expressions in SQL that execute operations using keywords or special operators.
- They are case-insensitive and identified as SQL92Identifiers.
- Utilized in SQL SELECT expressions for calculating values and manipulating data.
Types of In-built Functions
- Numeric Functions: Allow manipulation of numeric values, also known as mathematical functions.
- String Functions: Designed for string manipulation and returning output strings.
String Functions
-
CHAR_LENGTH(): Returns length of a word; use LEN() in SQL Server.
- Example:
SELECT char_length('Hello!');
Output: 6
- Example:
-
ASCII(): Finds ASCII value of a character.
- Example:
SELECT ascii('t');
Output: 116
- Example:
-
CONCAT(): Concatenates two or more strings.
- Example:
SELECT CONCAT('My', 'S', 'QL');
Output: ‘MySQL’
- Example:
-
LEFT(): Retrieves leftmost characters from a string; returns NULL if arguments are NULL.
- Example:
SELECT LEFT('foobarbar', 5);
Output: fooba
- Example:
- INSTR(): Returns position of the first occurrence of a substring in a string.
- REVERSE(): Reverses the order of characters in a string.
- STRCMP(): Compares two strings and returns 0 if equal, -1 if the first is smaller, or 1 if larger.
Date and Time Functions
- Utilizes ISO-8601 date and time formats.
- date(): Returns date in YYYY-MM-DD format.
- time(): Returns time in HH:MM:SS format.
- datetime(): Returns combined format "YYYY-MM-DD HH:MM:SS".
Aggregate Functions
- Performs calculations on a set of values returning a single value.
- Generally ignores null values (except for COUNT(*)).
- Functions are deterministic, yielding the same result for the same input values.
- Groups values according to specified criteria.
Key Aggregate Functions
-
COUNT(): Count of rows in a table, applicable to both numeric and non-numeric data.
-
COUNT(*)
returns total count, including duplicates and NULLs.
-
- SUM(): Calculates the sum of selected columns (numeric data only).
- AVG(): Computes average of numeric values, ignoring nulls.
- MAX(): Finds maximum value in a specified column.
- MIN(): Identifies minimum value in a specified column.
Additional Notes on Aggregate Functions
- MySQL supports standard aggregate functions: COUNT, SUM, AVG, MIN, MAX.
- DISTINCT excludes duplicates; ALL includes all data (duplicate inclusion is default).
- Aggregate functions are compatible with clauses like GROUP BY.
GROUP BY Clause
- Used for summarizing or aggregating data instead of retrieving individual records.
- Organizes data into groups and allows functions (like min(), max(), avg(), count(), sum()) to apply on grouped data.
- Employs a split-apply-combine strategy for effective data analysis.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz covers the concept of functions in programming, including predefined functions, how to create and call functions, and the benefits of using them. Test your knowledge on function syntax and execution in programming languages.