Functions in Programming
22 Questions
2 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary purpose of a function in programming?

  • To encapsulate code for reuse. (correct)
  • To create temporary variables.
  • To manage memory allocation.
  • To run code automatically on startup.
  • Which of the following statements correctly describes calling a function?

  • Functions are executed immediately upon declaration.
  • Functions can only be called once per program.
  • Functions must be defined before calling them. (correct)
  • Functions are called using their name followed by a comma.
  • What does the 'void' keyword indicate when declaring a function?

  • The function has parameters.
  • The function requires user input.
  • The function has no return value. (correct)
  • The function is mandatory for the program.
  • In the example given, what will be the output if myFunction() is called three times?

    <p>It will output 'I just got executed!' three times.</p> Signup and view all the answers

    What is meant by parameters in the context of functions?

    <p>They are inputs passed into functions for processing.</p> Signup and view all the answers

    What will happen if a function is called without being declared first?

    <p>It will cause a runtime error.</p> Signup and view all the answers

    Which part of the function declaration specifies the actions to be performed?

    <p>The body of the function within curly brackets</p> Signup and view all the answers

    What does the 'printf' function do in C programming?

    <p>It outputs text to the screen.</p> Signup and view all the answers

    What is an in-built function?

    <p>An in-built function is an expression in which an SQL keyword or special operator executes some operation.</p> Signup and view all the answers

    Numeric functions in SQL are sometimes called mathematical functions.

    <p>True</p> Signup and view all the answers

    What is the purpose of SQL string functions?

    <p>SQL string functions are used primarily for string manipulation.</p> Signup and view all the answers

    What does the CHAR_LENGTH() function do?

    <p>Finds the length of a word.</p> Signup and view all the answers

    What is the output of the SQL command SELECT ascii('t');?

    <p>116</p> Signup and view all the answers

    What is the function of the CONCAT() function in SQL?

    <p>Adds two words or strings.</p> Signup and view all the answers

    What does the LEFT(str,len) function return?

    <p>The leftmost len characters from the string str.</p> Signup and view all the answers

    What does the INSTR(str, substr) function do?

    <p>Returns the position of the first occurrence of substring substr in string str.</p> Signup and view all the answers

    What does the REVERSE(str) function return?

    <p>The string str with the order of the characters reversed.</p> Signup and view all the answers

    What format does the date() function return in SQL?

    <p>YYYY-MM-DD</p> Signup and view all the answers

    What does the COUNT() function do in SQL?

    <p>Counts the number of rows in a database table.</p> Signup and view all the answers

    Which SQL functions are considered aggregate functions?

    <p>COUNT, SUM, AVG, MIN, MAX</p> Signup and view all the answers

    What keyword can be used to exclude duplicate values from the aggregate function results?

    <p>DISTINCT</p> Signup and view all the answers

    What SQL clause is used to summarize or aggregate data?

    <p>GROUP BY Clause</p> 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 and printf() for output.
    • Example of using printf() to display "Hello World!" in main() 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 in myFunction().

    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 variables x and y.
    • 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
    • ASCII(): Finds ASCII value of a character.
      • Example: SELECT ascii('t'); Output: 116
    • CONCAT(): Concatenates two or more strings.
      • Example: SELECT CONCAT('My', 'S', 'QL'); Output: ‘MySQL’
    • LEFT(): Retrieves leftmost characters from a string; returns NULL if arguments are NULL.
      • Example: SELECT LEFT('foobarbar', 5); Output: fooba
    • 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.

    Quiz Team

    Related Documents

    UNIT 3 DMS.pdf

    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.

    More Like This

    Roles of Finance Function in Organisations
    5 questions
    Understanding Entity Creation and Function
    5 questions
    MATLAB Modeling and Simulation Quiz
    13 questions
    Use Quizgecko on...
    Browser
    Browser