0% found this document useful (0 votes)
14 views125 pages

Unit I Functions

The document provides an overview of functions in C programming, explaining their purpose, structure, and types. It highlights the importance of functions for code readability, reusability, and debugging, and distinguishes between standard library functions and user-defined functions. Additionally, it covers the syntax for function declaration, calling, and definition, as well as the types of user-defined functions based on arguments and return values.

Uploaded by

skxgh01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views125 pages

Unit I Functions

The document provides an overview of functions in C programming, explaining their purpose, structure, and types. It highlights the importance of functions for code readability, reusability, and debugging, and distinguishes between standard library functions and user-defined functions. Additionally, it covers the syntax for function declaration, calling, and definition, as well as the types of user-defined functions based on arguments and return values.

Uploaded by

skxgh01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module-2

Unit-1: Functions

By
Mr. N Bharath Kumar
Assistant Professor
Department of EEE, VFSTR
Functions
• A function is a block of code that performs a particular task.

• There are many situations where we might need to write


the same line of code more than once in a program.

• This may lead to unnecessary repetition of code, bugs and


even becomes boring for the programmer.

• So, C language provides an approach in which we can


declare and define a group of statements once in the form
of a function and it can be called and used whenever
required.

N Bharath Kumar Department of EEE


Need of Functions in C
Functions are used because of the following reasons –
• To improve the readability of code.
• Improves the reusability of the code, the same function can be
used in any program rather than writing the same code from
scratch.
• Debugging of the code would be easier if you use functions, as
errors are easy to be traced.
• Reduces the size of the code, duplicate sets of statements are
replaced by function calls.

N Bharath Kumar Department of EEE


Classification of Functions

• Standard Library functions also called built-in


functions are those functions which are
already defined in the C library, for
Functions example, printf(), scanf(), etc. You just need
to include appropriate header files to use
these functions. These are already declared
Standard and defined in C libraries.
User-defined
Library
Functions
Functions • User-defined functions, on the other hand,
are those functions that are defined by the
user at the time of writing the program.
These functions are made for code
reusability and for saving time and space.

N Bharath Kumar Department of EEE


Structure of C Function

The basic structure of a C Function consists of 3


parts –
1. Function Declaration (Function Prototype)
2. Function Call
3. Function Definition

N Bharath Kumar Department of EEE


Structure of C Function

N Bharath Kumar Department of EEE


Structure of C Function
1. Function declaration (prototype)
Syntax:
Return_type function_name (parameters);

A function must be declared globally in a c


program to tell the compiler about the
return type, function name, and function
parameters.

N Bharath Kumar Department of EEE


Structure of C Function
1. Function declaration (prototype)
Syntax:
Return_type function_name (parameters);

• A function may return a value.


• The Return_type is the data type (int, char, float,
double) of the value, the function returns.
• In case your function doesn't return any value,
the return type would be void.

void is the return type here, so the function returns


nothing.

N Bharath Kumar Department of EEE


Structure of C Function
1. Function declaration (prototype)
Syntax:
Return_type function_name (parameters);

• This is the actual name of the function.


• The function name is any valid C identifier and
therefore must follow the same naming rules as
other variables in the C language.

my_func is the function name here.

N Bharath Kumar Department of EEE


Structure of C Function
1. Function declaration (prototype)
Syntax:
Return_type function_name (parameters) ;

• A parameter is like a placeholder.


• When a function is called, you pass a value to
the parameter. This value is referred to as an
actual parameter.
• The parameter list refers to the type, order, and
number of the parameters of a function.
Parameters are optional, i.e., a function may
contain no parameters.

Here, my_func() holds two parameters.


N Bharath Kumar Department of EEE
Structure of C Function
1. Function declaration (prototype)
Syntax:
Return_type function_name (parameters) ;

The function declaration must always be terminated


by a semi-colon.

N Bharath Kumar Department of EEE


Structure of C Function
2. Function call
Syntax:
function_name (arguments);
• A function can be called from anywhere in the
program.
• The argument list must not differ in function
calling and function declaration.
• We must pass the same number of parameters
as it is specified in the function declaration.
• The function call must always be terminated
by a semi-colon.

N Bharath Kumar Department of EEE


Structure of C Function
2. Function call
Syntax:
function_name (arguments);

• Arguments are the values specified during the


function call, for which the formal parameters
are declared while defining the function.
• The argument list declares the type and number
of arguments that the function expects when it
is called.

N Bharath Kumar Department of EEE


Structure of C Function
3. Function definition
Syntax:
Return_type function_name (parameters)
{
code to be executed (body of the
function)
}
• It contains the actual statements which
are to be executed.
• It is the most important aspect to which
the control comes when the function is
called.
• The function definition should not be
terminated by a semi-colon.
N Bharath Kumar Department of EEE
Structure of C Function
3. Function definition
Syntax:
Return_type function_name (parameters)
{
code to be executed (body of the function)
}

Header of the function


• It consists of
• Return_type
• Function_name
• Formal parameters

N Bharath Kumar Department of EEE


Structure of C Function
3. Function definition
Syntax:
Return_type function_name (parameters)
{
code to be executed (body of the function)
}

Header of the function


• It consists of
• Return_type
• Function_name
• Formal parameters

N Bharath Kumar Department of EEE


Structure of C Function
3. Function definition
Syntax:
Return_type function_name (parameters)
{
code to be executed (body of the function)
}

Header of the function


• It consists of
• Return_type
• Function_name
• Formal parameters

N Bharath Kumar Department of EEE


Structure of C Function
3. Function definition
Syntax:
Return_type function_name (parameters)
{
code to be executed (body of the function)
}

Header of the function


• It consists of
• Return_type
• Function_name
• Formal parameters

N Bharath Kumar Department of EEE


Structure of C Function
3. Function definition
Syntax:
Return_type function_name (parameters)
{
code to be executed (body of the function)
}

Body of the function

N Bharath Kumar Department of EEE


Structure of C Function
3. Function definition
Body of the function
• The function body contains the
declarations and the statements
(algorithm) necessary for
performing the required task.

• The body is enclosed within curly braces { ... } and consists of three parts.
• Local variable declaration (if required).
• Function statements to perform the task inside the function.
• A return statement to return the result evaluated by the function.
(Note: If the return type is void, then no return statement is required.)

N Bharath Kumar Department of EEE


Structure of C Function
3. Function definition
Body of the function

• The body is enclosed within curly


braces { ... } and consists of three
parts.
• Local variable declaration
(if required)
• Here, avg is the local variable of
type float required to calculate
the average of the given numbers

N Bharath Kumar Department of EEE


Structure of C Function
3. Function definition
Body of the function

• The body is enclosed within curly


braces { ... } and consists of three
parts.
• Local variable declaration
(if required).
• Function statements to perform
the task inside the function.

• Here, the average of two numbers


is calculated using the formula
avg = (a + b) /2

N Bharath Kumar Department of EEE


Structure of C Function
3. Function definition
Body of the function

• The body is enclosed within curly


braces { ... } and consists of three
parts.
• Local variable declaration
(if required).
• Function statements to perform
the task inside the function.
• A return statement to return the
result evaluated by the function.
(Note: If the return type is void,
then no return statement is
required.)
N Bharath Kumar Department of EEE
Function using Return_type

N Bharath Kumar Department of EEE


Function using Return_type
• In the declaration, the return_type specified in
this example is int. So, the function my_func
returns an integer when it is called in main().

N Bharath Kumar Department of EEE


Function using Return_type
• In the declaration, the return_type specified in
this example is int. So, the function my_func
returns an integer when it is called in main().

declaration
• Since, the function my_func returns an integer
when it is called in main(), the value that is
returned by it must be stored in a variable.
definition
Here, that variable is declared as result and is
defined as shown in the program.

N Bharath Kumar Department of EEE


Function using Return_type
• In the declaration, the return_type specified in
this example is int. So, the function my_func
returns an integer when it is called in main().

• Since, the function my_func returns an integer


when it is called in main(), the value that is
returned by it must be stored in a variable.
Here, that variable is declared as result and is
defined as shown in the program.

N Bharath Kumar Department of EEE


Function using Return_type
• In the declaration, the return_type specified in
this example is int. So, the function my_func
returns an integer when it is called in main().

• Since, the function my_func returns an integer


when it is called in main(), the value that is
returned by it must be stored in a variable.
Here, that variable is declared as result and is
defined as shown in the program.

• Since, the function my_func returns an integer,


a return statement to return the result
evaluated by the function has to be defined.

N Bharath Kumar Department of EEE


Function using Return_type

• Return statement also ends the function


execution, hence it must be the last statement
of any function.

N Bharath Kumar Department of EEE


Function using Return_type

• Return statement also ends the function


execution, hence it must be the last statement
of any function.

• If you write any statement after the return


statement, it won't be executed.

Note: A function may or may not return a result,


but if it does, we must use the return statement
to output the result.

N Bharath Kumar Department of EEE


Function using Return_type
• While declaring the function, we have
declared two parameters of type int.

N Bharath Kumar Department of EEE


Function using Return_type
• While declaring the function, we have
declared two parameters of type int.

• Therefore, while calling that function, we need


to pass two arguments, else we will get a
compilation error.

N Bharath Kumar Department of EEE


Function using Return_type
• While declaring the function, we have
declared two parameters of type int.

• Therefore, while calling that function, we need


to pass two arguments, else we will get a
compilation error.

• And the two arguments passed should be


received in the function definition, which
means that the function header in the function
definition should have the two parameters to
hold the argument values.

N Bharath Kumar Department of EEE


Types of User – defined Functions
So a function either can take some arguments, or nothing is taken. Similarly, a
function can return something, otherwise does not return anything. So we can
categorize them into four types.
Function with Function with
no arguments arguments
and a return and no return
value value

Function with Function with


no arguments arguments
and no return User- and a return
value value
defined
functions

N Bharath Kumar Department of EEE


Types of User – defined Functions
Function with no arguments and no return value:

• In the program, we have a function


named my_func which has no argument and
no return type (void is the return type - that
means, the function will not return
anything).

• Within the function my_func, we are


declaring two variables a, b, calculating sum
of these elements, and printing the sum.

Output:
Enter two integers: 3 6
3+6=9
N Bharath Kumar Department of EEE
Types of User – defined Functions
Function with no arguments and a return value:

• In the program, we have a function


named my_func which has no argument but
a return type (int is the return type - that
means, the function will return an integer).

• Within the function my_func, we are


declaring two variables a, b, calculating sum
of these elements, and returning the sum to
the main function where my_func is called.

Output:
Enter two integers: 3 6
Sum = 9
N Bharath Kumar Department of EEE
Types of User – defined Functions
Function with arguments and no return value:

• In the program, we have a function


named my_func which has two arguments of
type int but no return type (void is the return
type - that means, the function will not
return anything).

• Within the function my_func, we are


calculating the sum of the elements received
as arguments x, y, and print the resultant
sum (x+y).
Output:
Enter two integers: 3 6
3+6=9
N Bharath Kumar Department of EEE
Types of User – defined Functions
Function with arguments and a return value:

• In the program, we have a function


named my_func which has two arguments of
type int and a return type (int is the return
type - that means, the function will return an
integer).

• Within the function my_func, we are


calculating the sum of the elements received
as arguments x, y, and return the resultant
sum (x+y) to the main function where
my_func is called and the result is printed in
Output:
the main function. Enter two integers: 3 6
3+6=9
N Bharath Kumar Department of EEE
Actual Parameters – Formal Parameters

N Bharath Kumar Department of EEE


Actual Parameters – Formal Parameters

function call

Actual Parameters are the arguments that are


used in the function call.

N Bharath Kumar Department of EEE


Actual Parameters – Formal Parameters

function
definition
Formal Parameters are the arguments that are
used in the function definition.

N Bharath Kumar Department of EEE


Types of Function Calls
• Functions are called by their names.
• If the function does not have any arguments, then to call a function we can
directly use its name.
• But for functions with arguments, we can call a function in two different
ways, based on how we specify the arguments.

Call by
Function Value
Calls Call by
Reference

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:

• In this parameter passing method, values of actual


parameters are copied to the function’s formal
parameters.

• Different memory locations are allotted for both formal


and actual parameters.

• So any changes made in formal parameters (inside


functions) are not reflected in the actual parameters of
the calling function.

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value: Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
1. Call by Value:

N Bharath Kumar Department of EEE


Types of Function Calls
Address of Operator (&)

The "Address Of" Operator (&) is a unary operator,


which returns the address of a variable.

x Variable (x)
X = 10
10 Value of (x)
(&X) = 3108
3108 Address of (x)

Pointer is a special kind of variable which stores the


address of a variable.
ptr Pointer for (x)
X = 10 3108 Value of pointer
ptr = (&X) = 3108 3112 Address of pointer

N Bharath Kumar Department of EEE


Types of Function Calls
Address of Operator (&)
x Variable (x) ptr Pointer for (x)
10 Value of (x) 3108 Value of pointer
3108 Address of (x) 3112 Address of pointer

N Bharath Kumar Department of EEE


Types of Function Calls
Address of Operator (&)
x Variable (x) ptr Pointer for (x)
10 Value of (x) 3108 Value of pointer
3108 Address of (x) 3112 Address of pointer

N Bharath Kumar Department of EEE


Types of Function Calls
Dereferencing Operator (*)
• The dereference operator is also known as an indirection operator,
which is represented by (*).

• When the indirection operator (*) is used with the pointer variable, then
it is known as dereferencing a pointer.

• When we dereference a pointer, then the value of the variable pointed


by this pointer will be returned.

X = 10
ptr = (&X) = 3108
*ptr = *(&x) = *(3108) = 10

N Bharath Kumar Department of EEE


Types of Function Calls
Dereferencing Operator (*)
X = 10
ptr = (&X) = 3108
*ptr = *(&x) = *(3108) = 10

N Bharath Kumar Department of EEE


Types of Function Calls
Dereferencing Operator (*)
int x = 10;
int *ptr;
ptr = &x;

x= 10
Types of Function Calls
Dereferencing Operator (*)
int x = 10;
int *ptr;
ptr = &x;

x= 10
&x = 3108
Types of Function Calls
Dereferencing Operator (*)
int x = 10;
int *ptr;
ptr = &x;

ptr = &x
x= 10 3108
&x = 3108
Types of Function Calls
Dereferencing Operator (*)
int x = 10;
int *ptr;
ptr = &x; *ptr = *(&x) = x

ptr = &x
x= 10 3108
&x = 3108
Types of Function Calls
2. Call by Reference:

• In this method, the address of the variable is passed to


the function as a parameter.

• Same memory is used for both formal and actual


parameters since the only address is used by both
parameters.

• So any changes made in formal parameters (inside


functions) are reflected in the actual parameters of the
calling function.

N Bharath Kumar Department of EEE


Types of Function Calls
2. Call by Reference:

N Bharath Kumar Department of EEE


Types of Function Calls
2. Call by Reference:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
2. Call by Reference:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
2. Call by Reference:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
2. Call by Reference:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
2. Call by Reference: Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
2. Call by Reference:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
2. Call by Reference:
Memory Allocation

N Bharath Kumar Department of EEE


Types of Function Calls
2. Call by Reference:

N Bharath Kumar Department of EEE


Passing arrays as parameters to function
• Whenever we need to pass a list of elements as an argument to
any functions in C language, it is preferred to do so using an array.

Passing Arrays to function

Passing a single array element to a


function

Passing a complete One-dimensional array


to a function

Passing a Multi-dimensional array to a


function

N Bharath Kumar Department of EEE


1. Passing a single array element to a function

N Bharath Kumar Department of EEE


1. Passing a single array element to a function

Output:
Array Element Passed: 4
N Bharath Kumar Department of EEE
1. Passing a single array element to a function

N Bharath Kumar Department of EEE


1. Passing a single array element to a function

N Bharath Kumar Department of EEE


2. Passing a complete One-dimensional array to a function

N Bharath Kumar Department of EEE


2. Passing a complete One-dimensional array to a function

N Bharath Kumar Department of EEE


2. Passing a complete One-dimensional array to a function

N Bharath Kumar Department of EEE


2. Passing a complete One-dimensional array to a function

N Bharath Kumar Department of EEE


2. Passing a complete One-dimensional array to a function

N Bharath Kumar Department of EEE


2. Passing a complete One-dimensional array to a function

N Bharath Kumar Department of EEE


3. Passing a Multi-dimensional array to a function

N Bharath Kumar Department of EEE


3. Passing a Multi-dimensional array to a function

N Bharath Kumar Department of EEE


3. Passing a Multi-dimensional array to a function

N Bharath Kumar Department of EEE


3. Passing a Multi-dimensional array to a function

N Bharath Kumar Department of EEE


Nesting of Functions
• C language 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
}
N Bharath Kumar Department of EEE
Recursion
• Recursion is a special way of nesting functions, where a function calls itself
inside it.

• We must have certain conditions in the function to break out of the


recursion, otherwise recursion will occur infinite times.

function1( )
{
// function1 body here
function1();
// function1 body here
}

N Bharath Kumar Department of EEE


Sum of first n natural numbers using Recursion
#include<stdio.h>
int sum(int n);
void main()
{
int number, result;
printf("Enter an integer:”);
scanf("%d", &number);
result = sum(number);
printf(“Sum is %d “, result);
}
int sum(int n)
{
if(n != 0)
return n + sum(n-1);
else
return n;
}
N Bharath Kumar Department of EEE
Sum of first n natural numbers using Recursion
#include<stdio.h>

int factorial(int n);


void main()
{
int num, result;
printf("Enter a number:");
scanf("%d", &num);
result = factorial(num);
printf(“Factorial = %d”, result);
}

int factorial(int n)
{
if(n > 1)
return n*factorial(n-1);
else
return 1;
}
N Bharath Kumar Department of EEE
Library Functions in C

 Library functions are built-in functions that are grouped


together and placed in a common location called a library.
 Each function performs a specific operation. We can use
this library function to get the pre-defined output.
 All C standard library functions are declared by using many
header files.
 These library functions are created at the time of designing
the compilers.
 We include the header files in our C program by
using #include<filename.h>.
 Whenever the program is run and executed, the related
files are included in the C program.

N Bharath Kumar Department of EEE


Header File Functions
Header Files

stdio.h − It is a standard i/o header file in which Input/output


functions are declared
math.h − All functions related to mathematics are in this
header file.
ctype.h − This file contains functions that are useful for testing
and mapping characters.

string.h − All string related functions are in this header file.

time.h − This file contains time and clock related functions.

N Bharath Kumar Department of EEE


Built functions in stdio.h
<stdio.h>
printf()
This function is used to print the all char, int, float,
string etc., values onto the output screen.
scanf()
This function is used to read data from keyboard.
putchar()
It writes a character to screen.
getchar()
It reads character from keyboard.
puts()
It writes line to o/p screen.
gets()
It reads line from keyboard.
N Bharath Kumar Department of EEE
Built functions in math.h

N Bharath Kumar Department of EEE


Built functions in math.h

N Bharath Kumar Department of EEE


Program on Built functions in math.h

#include<stdio.h>
#include<math.h> Output:
void main() 3.162278
{ 54.598150
printf("%f\n",sqrt(10.0)); 1.386294
printf("%f\n",exp(4.0));
printf("%f\n",log(4.0)); 2.000000
printf("%f\n",log10(100.0)); 5.200000
printf("%f\n",fabs(-5.2)); 5.000000
printf("%f\n",ceil(4.5)); -5.000000
printf("%f\n",floor(-4.5)); 2.000000
printf("%f\n",pow(4.0,.5));
printf("%f\n",fmod(4.5,2.0)); 0.500000
printf("%f\n",sin(0.0)); 0.000000
printf("%f\n",cos(0.0)); 1.000000
printf("%f\n",tan(0.0)); 0.000000
}

N Bharath Kumar Department of EEE


Built functions in ctype.h
[Link] Function Description Return Values

Returns 0 if the passed argument is a non –


1 This function identifies the alphanumeric character
isalnum()
alphanumeric characters Returns non-zero value if the passed
argument is an alphanumeric character

Returns 0 if the passed argument is not an


This function identifies the
alphabet
2 isalpha() alphabets from other
Returns non-zero value if the passed
characters
argument is an alphabet

Returns 0 if the passed argument is not a


This function identifies number
3 isdigit()
numbers in character. Returns nonzero value if the passed
argument is a number

N Bharath Kumar Department of EEE


Built functions in ctype.h
[Link] Function Description Return Values
Returns 0 if the passed argument is not
This function identifies the a lowercase alphabet
4 islower()
lowercase alphabets. Returns nonzero value if the passed
argument is a lowercase alphabet

Returns 0 if the passed argument is not


This function identifies the an uppercase alphabet
5 isupper()
uppercase alphabets. Returns nonzero value if the passed
argument is an uppercase alphabet

6 This function converts uppercase Returns lowercase alphabet of the


tolower()
alphabet to lowercase alphabet. corresponding uppercase alphabet

This function convert lowercase Returns Uppercase alphabet of the


7 toupper()
alphabet to uppercase alphabet. corresponding lowercase alphabet

N Bharath Kumar Department of EEE


MCQs on Functions
Which keyword is used to give back the value

A. static
B. void
C. return
D. const

Solution : C

N Bharath Kumar Department of EEE


MCQs on Functions
Every C Program should contain which function.?

A) printf()
B) show()
C) scanf()
D) main()

Solution : D

N Bharath Kumar Department of EEE


MCQs on Functions
What is the minimum number of functions to be
present in a C Program.?

A) 1
B) 2
C) 3
D) 4
Solution : A

N Bharath Kumar Department of EEE


MCQs on Functions
What is the limit for number of functions in a C
Program.?

A) 16
B) 31
C) 32
D) Any Number
Solution : D

N Bharath Kumar Department of EEE


MCQs on Functions
What is the maximum number of statements that
can present in a C function.?

A) 64
B) 128
C) 256
D) Any Number
Solution : D

N Bharath Kumar Department of EEE


MCQs on Functions
Arguments passed to a function in C language are
called ___ arguments.

A) Formal arguments
B) Actual Arguments
C) Definite Arguments
D) Ideal Arguments
Solution : B

N Bharath Kumar Department of EEE


MCQs on Functions
Arguments received by a function in C language are
called ___ arguments.

A) Formal arguments
B) Actual Arguments
C) Definite Arguments
D) Ideal Arguments
Solution : A

N Bharath Kumar Department of EEE


MCQs on Functions
What do you call this C Function calling itself?
int funny2()
{
funny2();
}

A) Indefinite Function
B) Definite Function
C) Cursive Function
D) Recursive Function

Solution : D
N Bharath Kumar Department of EEE
MCQs on Functions
Point out the illegal function declaration

A. int two_bhk(int, int [ ]);


B. int bhk_1(int a);
C. int 1_bhk(int);
D. int two_bhk(int [ ])

Solution : C, D

N Bharath Kumar Department of EEE


MCQs on Functions
#include <stdio.h>
void foo(); If we run this code so what
void main() will be the output?
{ A. No Output
int x = 0; B. 1
x = foo(); C. 0
printf("%d", x); D. Runtime error
}
void foo()
{ Solution : D
return 1;
}

N Bharath Kumar Department of EEE


MCQs on Functions

If we run this code so what


#include <stdio.h> will be the output?
int main()
{
printf("Links"); A. Prints Links 1 time
main(); B. Prints Links 34567 times
return 0; C. Compile time error
} D. Prints Links Infinite times

Solution : D

N Bharath Kumar Department of EEE


MCQs on Functions

#include <stdio.h> If we run this code so what


int main() will be the output?
{
void show()
A. Tutorial Links
{
B. Run Time Error
printf("Tutorial Links");
C. Compile time error
}
D. None of the above
show();
return 0; Solution : A
}

N Bharath Kumar Department of EEE


MCQs on Functions
#include <stdio.h>
void meg(int k) If we run this code so what
{ will be the output?
printf("hi"); A. Compile time error
}
B. hi
void meg(double k)
{
C. hello
printf("hello"); D. hi hello
}
void main( )
Solution : A
{
meg(3);
}

N Bharath Kumar Department of EEE


MCQs on Functions
How many values can a C Function return at a time?

A) Only One Value


B) Maximum of two values
C) Maximum of three values
D) Maximum of 8 values

Solution : A
Explanation: Using a return x; statement, you can return only
one value.
N Bharath Kumar Department of EEE
MCQs on Functions

#include <stdio.h> If we run this code so what


void show(); will be the output?
void main()
{ A) RAINBOW COLOURS
show(); B) COLOURS RAINBOW
printf("RAINBOW "); C) COLOURS
} D) Compiler error
void show()
{
Solution : B
printf("COLOURS ");
}

N Bharath Kumar Department of EEE


MCQs on Functions
int show();
If we run this code so what
void main()
will be the output?
{
int a;
A) PISTA COUNT=
printf("PISTA COUNT="); B) PISTA COUNT=0
a=show(); C) PISTA COUNT=10
printf("%d", a); D) Compiler error
}
int show()
{ Solution : C
return 10;
}
N Bharath Kumar Department of EEE
MCQs on Functions
int show();
void main() If we run this code so what
{ will be the output?
int a;
printf("PISTA COUNT="); A) PISTA COUNT=
a=show(); B) PISTA COUNT=10
printf("%d", a); C) PISTA COUNT=15
} D) Compiler error
int show()
{
return 15; Solution : C
return 10;
}

N Bharath Kumar Department of EEE


MCQs on Functions

int show(); If we run this code so what


void main() will be the output?
{
int a;
a=show();
A) 15.5
printf("%d", a); B) 15
} C) 0
int show() D) Compiler error
{
return 15.5; Solution : B
return 10;
}

N Bharath Kumar Department of EEE


MCQs on Functions
float show(); If we run this code so what
void main()
will be the output?
{
int a;
a=show(); A) 15.5
printf("%d", a); B) 15
} C) 0
float show() D) Compiler error
{
return 15.5; Solution : A
return 10;
}

N Bharath Kumar Department of EEE


MCQs on Functions

int myshow(int); If we run this code so what


void main() will be the output?
{
myshow(5); A) Received 5, Received 10,
myshow(10); B) Received 10, Received 5,
} C) Received 0, Received 0,
int myshow(int b) D) Compiler error
{
printf("Received %d, ", b); Solution : A
}

N Bharath Kumar Department of EEE


MCQs on Functions

int myshow(int); If we run this code so what


void main() will be the output?
{
myshow(5); A) Received 5, Received 10,
myshow(10); B) Received 10, Received 5,
} C) Received 0, Received 0,
int myshow(int b) D) Compiler error
{
printf("Received %d, ", b); Solution : A
}

N Bharath Kumar Department of EEE


MCQs on Functions

int myshow(int *); If we run this code what will


void main() be the output?
{
int a=10; A) Received RANDOMNumber,
myshow(&a); B) Received 10
C) Received 10,
} D) Compiler error
int myshow(int *k)
{
printf("Received %d, ", *k); Solution : C
}

N Bharath Kumar Department of EEE


MCQs on Functions
void myshow(int *);
void main() If we run this code what will
{ be the output?
int a=10;
printf("%d ", a); A) 10 10
myshow(&a); B) 20 20
printf("%d", a); C) 10 20
} D) Compiler error
void myshow(int *k)
{ Solution : C
*k=20;
}

N Bharath Kumar Department of EEE


MCQs on Functions
void myshow(int *);
void main() If we run this code what will
{ be the output?
int a=10;
printf("%d ", a); A) 10 10
myshow(&a); B) 20 20
printf("%d", a); C) 10 20
} D) Compiler error
void myshow(int k)
{ Solution : A
k=20;
}

N Bharath Kumar Department of EEE


MCQs on Functions
int bunny(int,int);
int main() If we run this code so what
{ will be the output?
int a, b;
a = bunny(5, 10); A) 5 10
b = bunny(10, 5); B) 10 5
printf("%d %d", a, b); C) 5 5
return 0; D) 15 15
}
int bunny(int i, int j) Solution : B
{
return (i, j);
}

N Bharath Kumar Department of EEE

You might also like