2nd-Computer-Chapter-13-Function-in-C-Notes
2nd-Computer-Chapter-13-Function-in-C-Notes
CHAPTER 13 (Functions in C)
Q 1. What is modular programming?
Ans. A programming technique in which a program consists of many independent parts is called modular
programming. These parts are called modules. These parts are also called function. Each module can
perform different tasks. Different modules are combined to make a complete program.
Q 2. What is a function?
Ans. In structured programming the program consists of more than more one part. Each part of program
is called a module or function. Function can be defined as " A named piece of code developed to perform
a specific task is called function".
Ans. Function is a piece of code designed to perform a specific task. These advantages are described
below:
• Easy programming
• Easy modification
• Easy debugging
• Reuse-ability
• Eliminates duplicate code
• Less programming time
Ans. The function that are provided as a part, C language are called built-in functions. These functions are
also called library function. A large number of built-in function are provided by C language. These functions
are stored in different header files. Examples are printf() scanf().
Ans. The functions that are written by the programmer to perform specific task are called user defined
functions. These functions are written according to the requirement of the program.
Ans. Function declaration is also called function prototype. It is a statement that provides basic
information to compiler about the structure of the function. Function declaration statement ends with
semicolon. Function can be declared before the main() function or inside the main() function.
Syntax:
Return_type function_name (paramters);
Example:
Int add(int a, int b);
Ans. Every function perform some specific task. The task is performed when the set of instructions
execute. Writing set of statements of a function is called function definition. Function definition is always
done outside main() function.
Syntax:
Return_type function_name (paramters);
Computer 11 Notes (By Last Hope Study)
Example:
Int add(int a, int b);
Q 8. What Is function header?
Ans. The first line of the function definition is called function header. It is similar to function prototype.
The only difference is that it is not terminated with semicolon. Its general syntax is as follow:
• Return_Type Function_Name(parameters)
Ans. The statement that is written to use a function is called function call. A function is called by using its
name. The required parameters are maintained after the name in braces at the end of the function call
statement. Semicolon is used at the end of statement in which function is called.
Example: add(3,5);
Ans. Keyword "return" is used to return a value from the body of called function to calling function. The
statement in which "return" keyword is used is called return statement. A function can return a single
value.
Syntax
return expression;
Q 11. What is return type?
Ans. The return type in function declaration indicates the type of value returned by a function e.g. int is
used as return type if the function returns integer value. If the function returns no value, the keyword
void is used as return type
Q 12. What are parameters?
Ans. Parameters are the values that are provided to a function when the function is called. Parameters
are given in the parentheses. If there are many parameters, these are separated by commas. If there is no
parameter, empty parentheses are used.
• Parameters in function call are called actual parameters/arguments
• Parameters in function declaration are called formal parameters
Q 13. What Is a local variable?
Ans. The variables declared inside curly braces { } (in main function, user defined function or conditional
statement etc) are called local variables. Local variable also called automatic variables.
Syntax:
auto data-type variables-name;
The use of auto is optional.
Ans. The variables that are declared outside the main() function or any other function are called global
variable. Global variables can be used by all functions in the program. All functions can share their value.
If value As global variable is changes in a function, that changes value is also available in other functions.
Q 15. What is meant by life time of a variable? / life time of local and global variable.
Ans. The time period for which a variable exists in the memory is called life time of a variable.
Computer 11 Notes (By Last Hope Study)
Lifetime of a local variable is limited, when control enters into the function and variable declaration
statement is executed, they are created in memory. When the control exits from the function these
variables are destroyed and their Life ends. When program starts execution, global variables are created
in memory. They remain in memory, till the termination of the program.
Q 16. What Is meant by scope of a variable? Scope of local and global variable.
Ans. The area where a variable can be accessed is known as scope of variable. Local variables have a
limited scope they can only be used in the function in which they are declared. Global variables can be
accessed in all modules of program. They are accessible in main() function as well as all other user defined
functions.
The simplest type of function is a function that accepts no argument and returns no value. The return type
of such function is void. The parameter list may be empty or the keyword void can be written in the
parenthesis. Example
Void hello()
{
Printf(“Hello world”);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
// Question 10: A program for basic arithmetic operations based on user input.
int Add(int a, int b) {
return a + b;
}
int main() {
int choice, num1, num2, result;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
scanf("%d", &choice);
switch (choice) {
case 1:
result = Add(num1, num2);
break;
case 2:
result = Subtract(num1, num2);
break;
case 3:
result = Multiply(num1, num2);
break;
case 4:
result = Divide(num1, num2);
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
return -1;
}
// Question 5: Write a function Is_Prime that has an input parameter and returns 1 if the
number is prime, otherwise returns 0.
#include <stdio.h>
int main() {
int num;
if (Is_Prime(num)) {
printf("%d is prime.\n");
} else {
printf("%d is not prime.\n");
}
}
// Question 4: Write a function named Draw_Asterisks that prints asterisks in a pattern.
#include <stdio.h>
void Draw_Asterisks(int n) {
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
}
int main() {
Draw_Asterisks(9);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
// Question 1: Write a program that constructs a rectangle using horizontal and vertical lines.
#include <stdio.h>
int main() {
int width, height;
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Rectangle:\n");
Draw_Horizontal(width);
Draw_Vertical(height - 2);
Draw_Horizontal(width);
}