Assignment - 2
1 MARKS
1 What is Function?
Function is a sub part of a program use to perform
specific task and executed individually.
2 Write down Syntax of Function Definition.
Syntax of function definition is:
returntype functionName(parameterList)
{
//
function body;
}
3 Write down Syntax of Function Definition.
Syntax of a function declaration is:
returntype functionName(parameretList);
4 List out System Defined Functions.
System defined functions are defined in header
files like string.h, math.h, ctype.h etc.
Some system functions are as follow:
clrscr();
printf();
scanf();
getch();
pow();
sqrt();
5 List Out String Functions in C.
String functions are:
strcpy();
strcmp();
strcat();
strlen();
strlwr();
strupr();
strrev();
Please follow me for more practicals....
6 What is the use of abs() function in C.
The abs() function returns the absolute value of a
number.
7 List out Character input/output Functions.
Character input/output functions are:
getch();
getchar();
putch();
putchar();
gets();
puts();
clrscr();
8 What is nesting function?
Calling of one function inside another function
body is called nesting function.
9 What is return Statement in C?
A return statement terminates the execution of a
function and return to the calling function.
10 What is Recursion Function?
A function that calls itself is called recursive
function.
Please follow me for more practicals....
11 A Function called by itself is called Recursive
Function.
12 List out Different types of user defined
functions.
There are mainly 4 different types of user defined
functions.
1. A function with argument and return something.
2. A function with argument and return nothing.
3. A function with no argument and return nothing.
4. A function with no argument and return
something.
13 How many ways to pass parameters to calling
function? Write their names.
Two methods:
1. Call by value.
2. Call by reference.
14 What is the use of strcmp() function in C?
It is used to compare the first string with second
string, if both the string are same it returns 0.
15 What is the use of strcat() function in C?
This function is used concatenate or join two
strings.
Please follow me for more practicals....
16 The System defined functions are also called
as standard or pre-defined function or library function.
17 List out C Math functions.
C math functions are:
pow(): Power function.
sqrt(): Square root function.
ceil(): Round up to the nearest integer.
floor(): Round down to the nearest integer.
abs(): Absolute value function for integers (from
<stdlib.h>).
18 Write the categories to call the functions.
Categories to call the functions are:
1. A function with argument and return something.
2. A function with argument and return nothing.
3. A function with no argument and return nothing.
4. A function with no argument and return something.
19 Define following functions: tolower(),
toupper().
tolower(): It converts the entire string into
lowercase.
toupper(): It converts the entire string into
uppercase.
20 Which header file is used to define
mathematics functions in C?
Math.h header file is used to define mathematical
functions.
21 main() function is called user define function.
Please follow me for more practicals....
2 MARKS
1 Explain advantages of functions.
Advantages of functions are:
Implementing a modular programming.
Programs becomre more readable and understandable.
Implementation of the logic becomes easy.
Code reuseability.
Programs can be divided into smaller modulus.
2 Write Disadvantages of Recursion function.
Disadvantages of recursion function are:
It is slower than non-recursive function.
It consumes lots of memory space.
Hard to understand and implement.
Space and time complexity cannot be managed.
If it is not written properly, memory overflow may
be caused.
3 Write a C program to find factorial of a
given number using function.
#include <stdio.h>
#include <conio.h>
int factorial(int number)
{
if (number
== 0)
{
return 1;
}
return
(number * factorial(number - 1));
}
int main()
{
clrscr();
int
number;
printf("\n\nEnter the number = ");
scanf("%d", &number);
printf("The factorial of %d is = %d", number, factorial(number));
getch();
return
0;
}
Output:
Enter the number = 5
The factorial of 5 is = 120
Please follow me for more practicals....
4 Write down the syntax of nested function.
Explain it.
Syntax of nested function is:
functionOne()
{
//
body of functionOne.
functionTwo();
}
functionTwo() is called inside the functionOne()
it is called a nesting of functions.
5 Write a C program to swap to numbers using
call by value.
#include <stdio.h>
#include <conio.h>
void swap(int, int);
int main()
{
clrscr();
int x,
y;
printf("\n\nEnter
the value of x = ");
scanf("%d", &x);
printf("Enter the value of y = ");
scanf("%d", &y);
swap(x,
y);
printf("\nValue outside function: x = %d, y = %d", x, y);
getch();
return
0;
}
void swap(int x, int y)
{
int temp
= x;
x = y;
y =
temp;
printf("\nValue inside function: x = %d, y = %d", x, y);
}
Output:
Enter the value of x = 4
Enter the value of y = 5
Value inside function: x = 5, y = 4
Value outside function: x = 4, y = 5
6 Write down different types of functions.
Explain any one of them.
There are mainly two types of functions.
1. User define functions.
2. System define functions.
User define functions.
The functions that are created and implemted by
users is called userdefine funcitons.
System Define Functions.
The functions that are already created and
implemented in the system files (header files) are called system functions.
7 Explain ctype.h library functions with its
name and description.
Ctype.h is used for testing and mapping characters.
isalpha(int c): It checks character is alphabetic
character or not.
isdigit(int c): It checks character is digit or not.
isalnum(int c): It check character is alphanumeric or
not.
islower(int c): It checks character is in lower case.
isupper(int c): It checks character is in upper case.
Please follow me for more practicals....
8 Write difference between function definition
and function declaration.
·
The function definition provides the actual
logic of that function. Whereas the function declaration provides function
name, return type, and datatype of parameters to the compiler.
·
Function definition is also known as function
body. Whereas the function declaration is also known as function prototype.
·
Function definition can be written in any part
of the program. Whereas the function declaration must be written before the
main function.
9 How can you call the function explain it with
its syntax.
The
function call invokes the function defination to do a specific task.
When the
function called it executes the function body.
The function
call is perform inside the main() or another function.
Syntax:
functionName(parameterList);
10 Write a C program that performs string
functions.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char str[50];
printf("Enter the string =
");
gets(str);
printf("\n%d",
strlen(str));
printf("\n%s",
strlwr(str));
printf("\n%s",
strupr(str));
printf("\n%s",
strrev(str));
getch();
}
Output:
Enter the string = Mehdiali Kadiwala
17
mehdiali kadiwala
MEHDIALI KADIWALA
ALAWIDAK ILAIDHEM
Please follow me for more practicals....
11 How many types of console input/output
functions? Explain scanf() function in detailed.
Two types of console Input / Output Functions:
1. Formatted Input/Output function.
2. Unformatted Or Character Input/Output function.
scanf()
It is used to read input values using the standard
input device (keyboard).
Syntax:
scanf("Format Specifier", &var1,
&var2, ..., &varN);
Format Specifier represents the data type of
variables and "&" sign represents the address of the variables.
12 Define following terms: getch(), gets(),
putch(), puts().
getch()
It is used to read one character at a time from
the standard input device.
gets()
It is used to read a string of character including
white space.
putch()
It is used to display a character on the standard
output devices.
puts()
It is used to display a string of character on the
standard output devices.
13 Write a C program that performs function
with no arguments and a return value.
#include <stdio.h>
#include <conio.h>
int sumOfTwo();
void main()
{
clrscr();
int sum = sumOfTwo();
printf("\nThe sum = %d",
sum);
getch();
}
int sumOfTwo()
{
int num1, num2;
printf("Enter the 1st number =
");
scanf("%d", &num1);
printf("Enter the 2nd number =
");
scanf("%d", &num2);
return num1 + num2;
}
Output:
Enter the 1st number = 4
Enter the 2nd number = 5
The sum = 9
Please follow me for more practicals....
3-MARK
1 What is Function? How many types of functions
are there?
Function is used to divide a large program into
smaller sub programs such than the program become easy to understand and
easy to implement.
There are two different types of functions:
1. User-Define Function.
2. System-Define Function.
2 How many types of User Defined functions are
there? Explain any one of them.
There are mainly 4 different types of user defined
functions.
1. A function with argument and return something.
2. A function with argument and return nothing.
3. A function with no argument and return nothing.
4. A function with no argument and return
something.
Function with argument and return value:
It makes the function completely independent of
inputs and output and only the logic is defined inside the function body.
e.g.
int addTwoNum(int num1, int num2)
{
return
num1 + num2;
}
3 Write down advantages of Functions.
Advantages of functions are:
Implementing a modular programming.
Programs become more readable and understandable.
Implementation of the logic becomes easy.
Code reusability.
Programs can be divided into smaller modulus.
4 What is System Function? List out all the
system defined functions and explain any one.
The functions whose definition is defined by the
system is called system define functions.
- String.h Functions.
- Math.h Functions.
- Input / Output Functions.
String.h Functions:
Functions in string.h library are used to
manipulate strings.
strcpy(); strcmp(); strcat(); strlen(); strlwr(); strupr(); strrev();
are string functions.
Please follow me for more practicals....
5 List out String functions with its name and
its description.
strcpy(); - It copies source string to destination
string.
strcmp(); - Compare the first string with second
string.
strcat(); -
It is used to concatenate two strings.
strlen(); -
It is used to find the length of the string.
strlwr(); - It is used to convert string into
lowercase.
strupr(); - It is used to convert string into
uppercase.
strrev(); - It is used to reverse a string.
6 Write short note of any one category to
calling a function with example.
Call by Value:
The method to pass the parameters to the call by
value is to copy of actual parameter values to fomal parameters.
The changes made on formal parameters does not
affect the value of actual parameters.
It means after the execution control come back to
the calling function the actual parameters value remains same.
e.g.
swap(num1, num2);
void swap(int num1, int num2)
{
int
temp = num1;
num1
= num2;
num2
= temp;
}
7 Write down difference between call by value
and call by reference.
In call by value A copy of the actual parameter's
value is passed to the function. Whereas in call by reference the memory
address (reference) of the actual parameter is passed to the function.
In call by value any changes made to the parameter
inside the function do not affect the original value of the variable in the calling
function. Whereas in call by reference Any changes made to the parameter inside
the function directly affect the original value of the variable in the calling
function.
Please follow me for more practicals....
8 Explain Function with no argument and no
return value with example.
This function can either be used to display data
or they are completely dependent on user.
Here is the function which take two numbers from
user and display the greater number.
void findMax()
{
int
num1, num2;
printf("Enter
the 1st number = ");
scanf("%d",
&num1);
printf("Enter
the 2nd number = ");
scanf("%d",
&num2);
if
(num1 > num2)
{
printf("\n1st
is greater = %d", num1);
}
else
{
printf("\n2nd
number is greater = %d", num2);
}
}
Please follow me for more practicals....
5-MARK
1 Explain Function Invocation Mechanism.
there are two methods to pass parameters from
calling function to called function and they are as follows...
- Call by Value.
- Call by Reference.
Call by Value
In call by value parameter passing method, the
copy of actual parameter values is copied to formal parameters and these formal
parameters are used in called function. The changes made on the formal
parameters does not affect the values of actual parameters.
Call by Reference
In Call by Reference parameter passing method, the
memory location address of the actual parameters is copied to formal
parameters. This address is used to access the memory locations of the actual
parameters in called function. In this method of parameter passing, the formal
parameters must be pointer variables.
2 Explain Formatted input/output functions in
detailed.
scanf() Function :
scanf() function is used to
read/input values of variables using the standard input device such as
keyboard. It has the following the syntax:
scanf("format string",
&variable1,&variable2,&variable3,...&variablen);
where “format string” is the control string which
represents the format specification, the symbol & (ampersand) which
represents the memory address where the variable value is to be stored.
Printf()
printf() function is used to
print/display values of variables using the standard output device such as monitor.
It has the following syntax:
("format string",
variable1,variable2,variable3,...variablen);
Please follow me for more practicals....
3 Explain Call by reference with example.
In Call by Reference parameter
passing method, the memory location address of the actual parameters is copied
to formal parameters. This address is used to access the memory locations of
the actual parameters in called function. In this method of parameter passing,
the formal parameters must be pointer variables.
That means in call by reference
parameter passing method, the address of the actual parameters is passed to the
called function and is recieved by the formal parameters (pointers). Whenever
we use these formal parameters in called function, they directly access the
memory locations of actual parameters. So the changes made on the formal
parameters effects the values of actual parameters. For example consider the
following program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2 ;
void swap(int *,int *) ; // function
declaration
clrscr() ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 =
%d, num2 = %d", num1, num2) ;
swap(&num1, &num2) ; //
calling function
printf("\nAfter swap: num1 = %d,
num2 = %d", num1, num2);
getch() ;
}
void swap(int *a, int *b) // called
function
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
4 Explain Function with argument and
no return value with example.
We are using the same function as
example again and again, to demonstrate that to solve a problem there can be
many different ways.
This time, we have modified the above
example to make the function greatNum() take two int values as arguments, but
it will not be returning anything.
#include<stdio.h>
void greatNum(int a, int b);//
function declaration
int main()
{
int i, j;
printf("Enter 2 numbers that you
want to compare...");
scanf("%d%d",&i,&j);
greatNum(i, j);// function call
return0;
}
void greatNum(int x, int y)//
function definition
{
if(x > y){
printf("The greater number is:
%d", x);
}
else{
printf("The greater number is:
%d", y);
}
}
Please follow me for more practicals....
5 Explain nested function with example.
C language also allows nesting of
functions i.e to use/call one function inside another function's body. We must
be careful while using nested functions, because it may lead to infinite
nesting.
function1()
{
// function1 body here
function2();
// function1 body here
}
If function2() also has a call for
function1() inside it, then in that case, it will lead to an infinite nesting.
They will keep calling each other and the program will never terminate.
6 Explain Recursive Function with example.
The recursive functions should be used very
carefully because, when a function called by itself it enters into the infinite
loop. And when a function enters into the infinite loop, the function execution
never gets completed. We should define the condition to exit from the function
call so that the recursive function gets terminated.
When a function is called by itself, the first
call remains under execution till the last call gets invoked. Every time when a
function call is invoked, the function returns the execution control to the
previous function call.
Example Program
#include<stdio.h>
#include<conio.h>
int factorial( int ) ;
int main()
{
int fact, n ;
printf("Enter any positive integer: ") ;
scanf("%d", &n) ;
fact = factorial( n ) ;
printf("\nFactorial of %d is %d\n", n,
fact) ;
return 0;
}
int factorial( int n )
{
int temp ;
if( n == 0)
return 1 ;
else
temp = n * factorial( n-1 ) ; // recursive
function call
return temp ;
}
Please follow me for more practicals....
7 Explain C Math functions with example.
There are various methods in math.h header file.
The commonly used functions of math.h header file are given below.
1) ceil(number):
rounds up the given number. It returns the integer
value which is greater than or equal to given number.
2) floor(number):
rounds down the given number. It returns the
integer value which is less than or equal to given number.
3) sqrt(number):
returns the square root of given number.
4) pow(base, exponent): returns the power of given
number.
5) abs(number): returns the absolute value of
given number.
e.g.
#include<stdio.h>
#include <math.h>
int main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
return 0;
}
Output
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12