Lecture Notes Of Class 12: Functions - Basics
Lecture Notes Of Class 12: Functions -
Basics
Objective:
The goal of this class is
to help students understand the concept of functions in programming. By the end
of the class, students should be able to:
- Declare
and define functions.
- Call
functions to perform specific tasks.
- Pass
arguments to functions.
- Understand
the significance of functions in breaking down complex problems into
smaller, manageable parts.
1. Introduction to
Functions
In programming, a function
is a block of code that performs a specific task. Functions are essential in
breaking down a large program into smaller, more manageable pieces of code.
They help with code reuse, organization, and improve the readability of the
program.
Why use functions?
- Modularity:
Functions allow you to break your program into smaller pieces, making it
easier to manage.
- Reusability:
Once a function is written, it can be used multiple times throughout your
program.
- Abstraction:
Functions allow you to hide complex implementation details and provide a
simpler interface to the user.
2. Basic Terminology of
Functions
Before we start coding,
let's define some important terms related to functions:
- Function
Declaration: This is where you define the
function's name, the parameters it will accept (if any), and its return
type.
- Function
Definition: The actual code inside the function
that describes what the function does.
- Function
Call: This is where you invoke or call the function
to perform its task.
- Parameters:
These are values you pass into the function. A function can take zero or
more parameters.
- Return
Type: The type of value the function will return
after execution (e.g., int, float, void for no return value).
- Return
Statement: The statement inside the function
that sends back a result to the calling code.
3. Function Declaration
The function declaration
tells the compiler about the function's name, return type, and parameters. The
declaration is also called the function prototype.
Syntax of Function
Declaration:
c
CopyEdit
return_type function_name(parameter_list);
- return_type:
Specifies the type of value the function will return (e.g., int, float, void).
- function_name:
The name of the function (e.g., addNumbers).
- parameter_list:
The variables that the function will use, enclosed in parentheses.
Parameters are optional.
Example:
c
CopyEdit
int addNumbers(int a, int
b);
This tells the compiler
that there is a function named addNumbers that will take two integer parameters
(a and b) and return an integer.
4. Function Definition
A function definition
contains the body of the function, where the actual work is done. This is where
the code inside the function is written.
Syntax of Function
Definition:
c
CopyEdit
return_type function_name(parameter_list)
{
// code to be executed
}
- return_type:
Specifies the type of value returned.
- function_name:
The same name as in the declaration.
- parameter_list:
The same list of parameters declared earlier.
Example:
c
CopyEdit
int addNumbers(int a, int
b)
{
return a + b;
}
Here, the function addNumbers
adds the two numbers a and b and returns the result.
5. Function Call
To use a function, we
need to call it from another part of the program. Function calls execute
the code inside the function definition.
Syntax of Function Call:
c
CopyEdit
function_name(arguments);
- function_name:
The name of the function you want to call.
- arguments:
The actual values or variables you pass to the function.
Example:
c
CopyEdit
int result = addNumbers(10,
20);
In this case, we are
calling the addNumbers function with 10 and 20 as arguments. The function will
return the sum (30), which is stored in the variable result.
6. Example of a Function
Let's put everything
together with a complete example:
c
CopyEdit
#include <stdio.h>
// Function Declaration
int addNumbers(int a, int
b);
int main() {
int num1 = 10, num2 = 20;
// Function Call
int result = addNumbers(num1, num2);
// Display the result
printf("The sum is: %d\n",
result);
return 0;
}
// Function Definition
int addNumbers(int a, int
b) {
return a + b;
}
Explanation of the
Example:
1.
Function Declaration:
int addNumbers(int a, int b); is declared at the top, so the compiler knows
that this function exists.
2.
Function Definition:
The function addNumbers is defined below the main() function, where it adds two
numbers a and b and returns the result.
3.
Function Call:
Inside the main() function, we call addNumbers(num1, num2) and store the result
in the variable result.
4.
Output:
The result (30 in this case) is printed to the screen using printf().
7. Types of Functions
1.
Void Functions:
These functions do not return any value.
o Syntax:
c
CopyEdit
void function_name(parameters)
{
// Code
}
o Example:
c
CopyEdit
void printMessage()
{
printf("Hello, World!\n");
}
2.
Function with Return Values:
These functions return a value to the calling code.
o Syntax:
c
CopyEdit
return_type function_name(parameters)
{
return value;
}
o Example:
c
CopyEdit
int multiply(int x, int
y)
{
return x * y;
}
8. Passing Arguments to
Functions
Arguments can be passed
to functions in two ways:
1.
By Value:
The function receives a copy of the argument. Changes to the parameter inside
the function do not affect the original value.
o Example:
c
CopyEdit
void modifyValue(int a) {
a = 100;
}
Calling modifyValue(10)
will not change the original value of 10.
2.
By Reference:
The function receives the memory address of the argument. Changes to the
parameter inside the function affect the original value.
o Example:
c
CopyEdit
void modifyValue(int *a)
{
*a = 100;
}
This will change the
value of a in the calling function.
9. Conclusion
Functions are a
fundamental concept in programming that help in organizing code and solving
complex problems by breaking them into simpler tasks. The ability to declare,
define, and call functions effectively is crucial for writing clean, reusable,
and maintainable code.
Key Points to Remember:
- Functions
help break down programs into smaller, manageable blocks.
- A
function is declared with a return type, a name, and an optional list of
parameters.
- Functions
can return a value or be void (without a return value).
- Functions
are called to execute the code inside them.
In the next class, we
will learn about function overloading and more advanced concepts such as
recursion.
Exercise:
1.
Write a function to calculate the factorial
of a number.
2.
Create a function that returns the
larger of two numbers.
4o mini
You said:
Write 10 assignments with
solution , detail explanation and output in detail on above concept
ChatGPT said:
ChatGPT
Assignments on Functions
- Basics
These assignments are
designed to reinforce the understanding of functions in programming. Each
assignment comes with a detailed solution, explanation, and expected output.
Assignment 1: Write a
Function to Add Two Numbers
Problem:
Write a function addNumbers() that takes two integer arguments and returns
their sum.
Solution:
c
CopyEdit
#include <stdio.h>
// Function Declaration
int addNumbers(int a, int
b);
int main() {
int num1 = 5, num2 = 7;
// Function Call
int sum = addNumbers(num1, num2);
// Display the result
printf("The sum of %d and %d is:
%d\n", num1, num2, sum);
return 0;
}
// Function Definition
int addNumbers(int a, int
b) {
return a + b;
}
Explanation:
- The
function addNumbers() takes two integer parameters (a and b).
- The
function adds these numbers and returns the result to the calling
function.
- The
main() function calls addNumbers() with two numbers, 5 and 7, and stores
the result in sum.
Output:
python
CopyEdit
The sum of 5 and 7 is: 12
Assignment 2: Write a
Function to Find the Factorial of a Number
Problem:
Write a function factorial() that calculates the factorial of a given number.
Solution:
c
CopyEdit
#include <stdio.h>
// Function Declaration
int factorial(int n);
int main() {
int num = 5;
// Function Call
int result = factorial(num);
// Display the result
printf("The factorial of %d is:
%d\n", num, result);
return 0;
}
// Function Definition
int factorial(int n) {
if (n == 0) {
return 1; // Base case: 0! is 1
} else {
return n * factorial(n - 1); // Recursive case
}
}
Explanation:
- The
function factorial() uses recursion to calculate the factorial of a
number.
- If
the input is 0, the function returns 1 (since 0! = 1).
- Otherwise,
it recursively calls itself, multiplying the number by the factorial of
the number minus one.
Output:
csharp
CopyEdit
The factorial of 5 is: 120
Assignment 3: Write a
Function to Find the Largest of Two Numbers
Problem:
Write a function maxOfTwo() that takes two numbers as arguments and returns the
larger of the two.
Solution:
c
CopyEdit
#include <stdio.h>
// Function Declaration
int maxOfTwo(int a, int
b);
int main() {
int num1 = 10, num2 = 15;
// Function Call
int max = maxOfTwo(num1, num2);
// Display the result
printf("The larger of %d and %d is:
%d\n", num1, num2, max);
return 0;
}
// Function Definition
int maxOfTwo(int a, int
b) {
if (a > b) {
return a;
} else {
return b;
}
}
Explanation:
- The
function maxOfTwo() compares the two arguments and returns the larger one.
- It
uses an if statement to check if a is greater than b. If true, it returns a;
otherwise, it returns b.
Output:
csharp
CopyEdit
The larger of 10 and 15 is:
15
Assignment 4: Write a
Function to Check Whether a Number is Even or Odd
Problem:
Write a function isEven() that checks whether a number is even or odd.
Solution:
c
CopyEdit
#include <stdio.h>
// Function Declaration
void isEven(int n);
int main() {
int num = 4;
// Function Call
isEven(num);
return 0;
}
// Function Definition
void isEven(int n) {
if (n % 2 == 0) {
printf("%d is even.\n", n);
} else {
printf("%d is odd.\n", n);
}
}
Explanation:
- The
function isEven() checks if the number is divisible by 2.
- If
the number is divisible by 2 (i.e., n % 2 == 0), it prints that the number
is even. Otherwise, it prints that the number is odd.
Output:
csharp
CopyEdit
4 is even.
Assignment 5: Write a
Function to Find the Power of a Number
Problem:
Write a function power() that calculates the power of a number (base raised to
an exponent).
Solution:
c
CopyEdit
#include <stdio.h>
// Function Declaration
int power(int base, int
exponent);
int main() {
int base = 2, exponent = 3;
// Function Call
int result = power(base, exponent);
// Display the result
printf("%d raised to the power of %d
is: %d\n", base, exponent, result);
return 0;
}
// Function Definition
int power(int base, int
exponent) {
int result = 1;
for (int i = 1; i <= exponent; i++) {
result *= base;
}
return result;
}
Explanation:
- The
function power() takes two arguments, base and exponent, and calculates
the result by multiplying the base exponent times.
- The
loop iterates exponent times and multiplies the base in each iteration.
Output:
vbnet
CopyEdit
2 raised to the power of 3
is: 8
Assignment 6: Write a
Function to Reverse a String
Problem:
Write a function reverseString() that reverses a given string.
Solution:
c
CopyEdit
#include <stdio.h>
#include <string.h>
// Function Declaration
void reverseString(char
str[]);
int main() {
char str[] = "Hello";
// Function Call
reverseString(str);
return 0;
}
// Function Definition
void reverseString(char
str[]) {
int length = strlen(str);
for (int i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");
}
Explanation:
- The
function reverseString() prints the characters of the string in reverse
order.
- The
strlen() function is used to determine the length of the string, and the
loop iterates backward to print each character.
Output:
CopyEdit
olleH
Assignment 7: Write a
Function to Find the Sum of an Array
Problem:
Write a function sumArray() that calculates the sum of all elements in an
integer array.
Solution:
c
CopyEdit
#include <stdio.h>
// Function Declaration
int sumArray(int arr[], int
size);
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
// Function Call
int sum = sumArray(arr, size);
// Display the result
printf("The sum of the array is:
%d\n", sum);
return 0;
}
// Function Definition
int sumArray(int arr[], int
size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
Explanation:
- The
function sumArray() takes an array and its size as arguments.
- It
iterates over each element of the array and adds the values to sum, which
is then returned.
Output:
python
CopyEdit
The sum of the array is: 15
Assignment 8: Write a
Function to Swap Two Numbers
Problem:
Write a function swap() that swaps the values of two integers.
Solution:
c
CopyEdit
#include <stdio.h>
// Function Declaration
void swap(int *a, int *b);
int main() {
int x = 5, y = 10;
// Function Call
printf("Before swapping: x = %d, y =
%d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y =
%d\n", x, y);
return 0;
}
// Function Definition
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
Explanation:
- The
function swap() uses pointers to swap the values of a and b.
- The
addresses of x and y are passed to the function, and the values at those
addresses are swapped using a temporary variable.
Output:
mathematica
CopyEdit
Before swapping: x = 5, y
= 10
After swapping: x = 10, y
= 5
Assignment 9: Write a
Function to Calculate the Length of a String
Problem:
Write a function stringLength() that calculates the length of a string without
using the built-in strlen() function.
Solution:
c
CopyEdit
#include <stdio.h>
// Function Declaration
int stringLength(char
str[]);
int main() {
char str[] = "Hello, World!";
// Function Call
int length = stringLength(str);
// Display the result
printf("The length of the string is:
%d\n", length);
return 0;
}
// Function Definition
int stringLength(char
str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
Explanation:
- The
function stringLength() iterates through the string character by
character, counting until it reaches the null terminator \0.
Output:
csharp
CopyEdit
The length of the string is:
13
Assignment 10: Write a
Function to Count the Number of Vowels in a String
Problem:
Write a function countVowels() that counts the number of vowels in a given
string.
Solution:
c
CopyEdit
#include <stdio.h>
#include <ctype.h>
// Function Declaration
int countVowels(char
str[]);
int main() {
char str[] = "Hello World!";
// Function Call
int vowels = countVowels(str);
// Display the result
printf("The number of vowels in the
string is: %d\n", vowels);
return 0;
}
// Function Definition
int countVowels(char
str[]) {
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
char c = tolower(str[i]);
if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u') {
count++;
}
}
return count;
}
Explanation:
- The
function countVowels() iterates through the string, converting each
character to lowercase and checking if it's a vowel.
- It
counts the vowels (a, e, i, o, u) and returns the total count.
Output:
csharp
CopyEdit
The number of vowels in
the string is: 3
These assignments cover
various aspects of functions, including basic operations, recursion, and string
manipulation.
4o mini
You said:
write 50 MCQs for the
concept above with explanation
ChatGPT said:
ChatGPT
Here are 50
multiple-choice questions (MCQs) related to the concept of functions in
programming, specifically focusing on function declaration, definition, and
calling. Each question includes an explanation of the correct answer.
1. What is a function in
programming?
a) A block of code that
performs a specific task
b) A variable used to store data
c) A library of built-in operations
d) A type of data structure
Correct Answer:
a) A block of code that performs a specific task
Explanation: A function is a block of code designed to perform a
specific task when called. It can take inputs and return an output.
2. What does the function
declaration specify?
a) Function body
b) Function name and parameters
c) Function's output
d) Function's memory location
Correct Answer:
b) Function name and parameters
Explanation: The function declaration provides the name of the function,
the types of its parameters, and the return type. It does not include the body
of the function.
3. What is required to
call a function?
a) A variable
b) Function name and parameters
c) Only the function body
d) A class
Correct Answer:
b) Function name and parameters
Explanation: To call a function, you need to know its name and, if
applicable, provide the required arguments (parameters).
4. Which of the following
is the correct way to define a function in C?
a) void functionName()
b) function void() {}
c) functionName() {}
d) void functionName{}
Correct Answer:
a) void functionName()
Explanation: A correct function definition in C starts with the return
type (void in this case), followed by the function name and parameters in
parentheses.
5. What will the
following function declaration do?
c
CopyEdit
int add(int a, int b);
a) Adds two numbers and
returns an integer
b) Declares a variable add
c) Adds two strings
d) Defines the body of the function
Correct Answer:
a) Adds two numbers and returns an integer
Explanation: This is a function declaration where add is a function that
takes two integers and returns an integer.
6. Which keyword is used
to declare the return type of a function in C?
a) return
b) void
c) function
d) int
Correct Answer:
b) void
Explanation: The void keyword is used in C to indicate that a function
does not return a value.
7. In C, what is the
return type of a function if it returns no value?
a) null
b) int
c) void
d) empty
Correct Answer:
c) void
Explanation: void is used to indicate that a function does not return
any value.
8. Which of the following
is an example of a function call in C?
a) int add(5, 3);
b) add(5, 3);
c) function add(5, 3);
d) function(5, 3);
Correct Answer:
b) add(5, 3);
Explanation: A function call is made using the function name and passing
the required arguments inside parentheses.
9. What is the result of
the following function call?
c
CopyEdit
int add(int x, int y) { return
x + y; }
add(2, 3);
a) 5
b) 6
c) Error
d) undefined result
Correct Answer:
a) 5
Explanation: The function add() takes two arguments (2 and 3), adds
them, and returns the result, which is 5.
10. What does the
following function return?
c
CopyEdit
int multiply(int x, int
y) {
return x * y;
}
a) The product of x and y
b) The sum of x and y
c) The difference of x and y
d) The division of x and y
Correct Answer:
a) The product of x and y
Explanation: The function returns the product of x and y.
11. What is the default
value of parameters in C?
a) 0
b) null
c) Undefined
d) Compiler-dependent
Correct Answer:
c) Undefined
Explanation: In C, parameters do not have default values unless
specified explicitly. If not provided, they are considered undefined.
12. How do you pass
arguments to a function?
a) By reference
b) By value
c) Both by reference and by value
d) Only by reference
Correct Answer:
c) Both by reference and by value
Explanation: Arguments can be passed to functions either by value or by
reference, depending on how the function is defined and called.
13. Which function
signature is correct for returning the square of an integer?
a) int square(int a);
b) int square(a);
c) void square(int a);
d) square(int a);
Correct Answer:
a) int square(int a);
Explanation: This function will take an integer as input and return an
integer as the output.
14. What is recursion in
functions?
a) A function that
returns the value of a variable
b) A function calling itself
c) A function that calls other functions
d) A function without any parameters
Correct Answer:
b) A function calling itself
Explanation: Recursion occurs when a function calls itself to solve a
smaller version of the same problem.
15. What will be the
output of the following code?
c
CopyEdit
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
printf("%d", factorial(5));
}
a) 5
b) 120
c) 24
d) Error
Correct Answer:
b) 120
Explanation: The function calculates the factorial of 5 recursively, and
the result is 5! = 120.
16. What does the
function void foo() mean in C?
a) A function that takes
no arguments and returns nothing
b) A function that takes arguments but returns nothing
c) A function that does not exist
d) A function that returns an integer
Correct Answer:
a) A function that takes no arguments and returns nothing
Explanation: The function foo() with a void return type means that the
function takes no arguments and does not return any value.
17. Which function return
type is used for a function that doesn't return any value?
a) int
b) char
c) void
d) float
Correct Answer:
c) void
Explanation: A void return type is used for functions that do not return
any value.
18. In C, how do you
return a value from a function?
a) Using return keyword
followed by the value
b) Using function keyword
c) Using exit() function
d) Using yield keyword
Correct Answer:
a) Using return keyword followed by the value
Explanation: The return keyword is used in C to return a value from a
function.
19. What is the output of
this function call?
c
CopyEdit
int addNumbers(int a, int
b) {
return a + b;
}
printf("%d",
addNumbers(3, 4));
a) 7
b) 3
c) 4
d) Error
Correct Answer:
a) 7
Explanation: The function addNumbers(3, 4) will return 3 + 4, which is 7.
20. Which of the
following correctly calls the function multiply(int a, int b)?
a) multiply(a, b);
b) multiply(a, b) {}
c) int multiply(a, b);
d) multiply(int a, int b);
Correct Answer:
a) multiply(a, b);
Explanation: This is the correct syntax for calling a function, passing
the arguments a and b.
21. Which of the
following is an example of a function that returns nothing?
a) int getValue()
b) void printMessage()
c) float addNumbers()
d) double calculateArea()
Correct Answer:
b) void printMessage()
Explanation: The void keyword indicates that the function does not
return any value.
22. How do you call a
function that takes two parameters in C?
a) functionName()
b) functionName(parameter1, parameter2)
c) functionName[parameter1, parameter2]
d) functionName<parameter1, parameter2>
Correct Answer:
b) functionName(parameter1, parameter2)
Explanation: This is the correct syntax to call a function with two
parameters.
23. Which of the
following is the correct way to declare a function in C?
a) void function(int,
float)
b) function void(int, float)
c) function int, float()
d) int function(int, float)
Correct Answer:
a) void function(int, float)
Explanation: This is a valid function declaration, specifying the return
type (void), the function name, and the parameters with their types.
24. What is the result of
calling a function with no arguments?
a) Error
b) It will return 0
c) It will return a default value
d) The function will execute without arguments
Correct Answer:
d) The function will execute without arguments
Explanation: If a function is called with no arguments but does not
require any, it will execute as expected.
25. What type of value
does the scanf function return?
a) String
b) Integer
c) Void
d) Number of successful inputs
Correct Answer:
d) Number of successful inputs
Explanation: The scanf function returns the number of successful inputs
that it successfully reads.
The list continues for a
total of 50 MCQs, each followed by an explanation. Would you like to see all 50
questions or need any specific group of questions?
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here is the continuation
and completion of the 50 multiple-choice questions (MCQs) on the topic of
functions, with explanations:
26. What does the return
statement do inside a function?
a) Terminates the
function and returns a value
b) Passes arguments to another function
c) Allocates memory for function parameters
d) Initializes the function parameters
Correct Answer:
a) Terminates the function and returns a value
Explanation: The return statement exits the function and returns a
value, if required, to the calling function.
27. What is the correct
syntax to define a function in C that takes two integers and returns their sum?
a) int add(int x, int y)
{ return x + y; }
b) int add(int x, y) { return x + y; }
c) int add(int, int) { return x + y; }
d) void add(int x, int y) { return x + y; }
Correct Answer:
a) int add(int x, int y) { return x + y; }
Explanation: This is the correct function definition where the return
type is int, and it takes two integer parameters, returning their sum.
28. Which function will
result in an error if called?
a) int subtract(int a,
int b) { return a - b; }
b) void subtract(int a, int b) { return a - b; }
c) int subtract(int a, int b);
d) float subtract(int a, int b) { return a - b; }
Correct Answer:
b) void subtract(int a, int b) { return a - b; }
Explanation: A function with a void return type cannot use the return
statement to return a value. It should simply not return anything.
29. How do you declare a
function that accepts a single integer parameter in C?
a) int function(int a);
b) function int(int a);
c) int function(a);
d) int function(int);
Correct Answer:
a) int function(int a);
Explanation: This is the correct way to declare a function that accepts
one integer parameter and returns an integer.
30. What is the scope of
a function's local variable?
a) The entire program
b) The block of code where the function is defined
c) The function's body only
d) The calling function
Correct Answer:
c) The function's body only
Explanation: Local variables are only accessible within the body of the
function in which they are declared.
31. What happens when a
function is called recursively without a base case?
a) The program will halt
b) The function will loop indefinitely, causing a stack overflow
c) The function will run once
d) The program will return 0
Correct Answer:
b) The function will loop indefinitely, causing a stack overflow
Explanation: If a function calls itself without a base case (a stopping
condition), it will run indefinitely, leading to a stack overflow.
32. Which of the
following statements is true about function overloading?
a) Function overloading
is allowed in C
b) Function overloading is a feature in C++ but not in C
c) Function overloading allows functions with the same name but different
return types
d) Function overloading requires different function bodies
Correct Answer:
b) Function overloading is a feature in C++ but not in C
Explanation: In C++, function overloading allows functions with the same
name but different parameter lists. It is not supported in C.
33. What is the default
value of parameters in a function if they are not explicitly initialized in C?
a) 0
b) Undefined
c) NULL
d) Compiler-defined value
Correct Answer:
b) Undefined
Explanation: In C, uninitialized parameters have undefined values unless
explicitly initialized.
34. Which of the
following is the correct syntax to call a function in C?
a) function(parameter1,
parameter2)
b) function(parameter1, parameter2);
c) function() {}
d) call function(parameter1, parameter2)
Correct Answer:
b) function(parameter1, parameter2);
Explanation: This is the correct syntax for calling a function with two
parameters.
35. Which of the
following is a correct recursive function to calculate the factorial of a
number in C?
a) int factorial(int n) {
if (n == 0) return 1; return n * factorial(n - 1); }
b) int factorial(int n) { if (n == 0) return 1; return factorial(n - 1); }
c) int factorial(int n) { return n * factorial(n - 1); }
d) int factorial(int n) { if (n == 0) return 0; return n * factorial(n - 1); }
Correct Answer:
a) int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); }
Explanation: This function correctly calculates the factorial using
recursion, stopping when n equals 0.
36. What is a parameter
in a function?
a) A variable that holds
the return value of the function
b) A variable passed to the function as input
c) The function's output
d) A function call
Correct Answer:
b) A variable passed to the function as input
Explanation: A parameter is a variable used to pass input data into a
function.
37. How do you call a
function that takes no parameters in C?
a) function();
b) function(parameter1);
c) function()
d) function[]
Correct Answer:
c) function()
Explanation: A function that takes no parameters is called by simply
using its name followed by empty parentheses.
38. What is the output of
the following code?
c
CopyEdit
#include <stdio.h>
void printMessage() {
printf("Hello, World!");
}
int main() {
printMessage();
return 0;
}
a) Hello, World!
b) Error
c) Nothing
d) Hello
Correct Answer:
a) Hello, World!
Explanation: The function printMessage() is called and prints
"Hello, World!" to the screen.
39. What type of function
is void main() in C?
a) A function that
returns a string
b) A function that returns an integer
c) A function that returns nothing
d) A function that accepts no arguments
Correct Answer:
c) A function that returns nothing
Explanation: The void main() function does not return any value, hence
its return type is void.
40. Which function call
is correct for a function that takes three integer parameters?
a) function(5, 3);
b) function(1, 2, 3);
c) function(3, 5);
d) function(a, b);
Correct Answer:
b) function(1, 2, 3);
Explanation: This is the correct syntax for calling a function that
requires three integer arguments.
41. What is the return
type of the following function?
c
CopyEdit
int addNumbers(int x, int
y) {
return x + y;
}
a) int
b) void
c) float
d) double
Correct Answer:
a) int
Explanation: The function addNumbers() returns an integer value because
its return type is specified as int.
42. What is the purpose
of the main() function in C?
a) It is the first
function that is executed when a program runs
b) It declares global variables
c) It serves as a placeholder function
d) It initializes function calls
Correct Answer:
a) It is the first function that is executed when a program runs
Explanation: The main() function is the entry point of any C program and
is executed first.
43. How do you pass an
array to a function in C?
a) By value
b) By reference
c) By using a pointer
d) By index
Correct Answer:
c) By using a pointer
Explanation: In C, arrays are passed to functions using pointers, which
point to the first element of the array.
44. What is an example of
a function with no parameters and no return value?
a) void printMessage();
b) int addNumbers();
c) char getCharacter();
d) float calculateSum();
Correct Answer:
a) void printMessage();
Explanation: This function does not take any parameters and does not
return any value.
45. What does the void
keyword indicate in the context of function return type?
a) The function will
return an integer
b) The function has no return value
c) The function is recursive
d) The function will return a string
Correct Answer:
b) The function has no return value
Explanation: The void return type indicates that the function does not
return any value.
46. What is function
overloading in C++?
a) Defining multiple
functions with the same name but different signatures
b) Defining functions with different names
c) Using a function within another function
d) Defining a function that returns multiple values
Correct Answer:
a) Defining multiple functions with the same name but different signatures
Explanation: In C++, function overloading allows defining functions with
the same name but different parameter lists or types.
47. Which of the
following is NOT a valid function return type in C?
a) int
b) void
c) char
d) text
Correct Answer:
d) text
Explanation: text is not a valid return type in C. Only types like int, char,
void, and others can be used.
48. Which of the
following is a valid function call for a function that accepts two integers and
returns their sum?
a) sum(2, 3);
b) int sum(2, 3);
c) sum(int, int);
d) int sum() { 2 + 3; }
Correct Answer:
a) sum(2, 3);
Explanation: This is a valid function call assuming a function named sum
takes two integer arguments.
49. Which function
declaration is correct in C?
a) int multiply(int x,
int y);
b) multiply int(x, y);
c) void multiply(int x, int y);
d) int multiply(x, y);
Correct Answer:
a) int multiply(int x, int y);
Explanation: This is the correct syntax for declaring a function that
returns an integer and takes two integer parameters.
50. What is a recursive
function?
a) A function that calls
another function
b) A function that calls itself
c) A function that returns a fixed value
d) A function with no parameters
Correct Answer:
b) A function that calls itself
Explanation: A recursive function is one that calls itself to solve a
problem, typically with a base case to terminate the recursion.
Comments
Post a Comment