C Programming Functions and Pointers Tutorial
Table of Contents
Learn C Programming Functions and Pointers MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
C language is built up of a number of functions. A function is a group of statements or block of statements that servers a specific purpose.
There are two types of functions in a C language.
- Library Functions
- User Defined Functions
1. Library Functions
As the name suggests, Library functions are predefined functions written for specific purposes like printing, scanning input and more. To use library functions, you have to include the header files with the file name extension DOT-H (.h) in your C program using a Preprocessor Directive.
2. User Defined Functions
User defined functions are the functions defined or written by you in your C program. You can define any number of functions in a C program.
Syntax of a Function
RETURN-TYPE FUNCTION_NAME(PARAMERTER1, PARAMETER2,..) { STATEMENTS; // FUNCTION LOGIC return SOME_VALUE; }
Notes about Writing and Working of a Function in C
A function name should follow the same guidelines of choosing a Variable name
function name can not start with a number. It can start with an Underscore ( _ ) symbol.
A function name can contain Alphabets, numbers and Special character like Underscore.
Same function name should be repeated again in the same C program.
Popular ‘return’ keyword is used most with functions.
Default Return type of a function is a Garbage Integer value if not specified explicitly.
A function can return only one value at a time.
You can put any number of RETURN statements in a c program.
You need not write a RETURN statement. It is not suggested.
main() is also a function which is invoked or called or started automatically on execution.
Any executable C program must contain main() function.
Other programming languages call functions as methods.
Arguments are nothing but Parameters a function accepts.
and Arguments received in a function are called FORMAL ARGUMENTS.
Arguments passed to a function are called ACTUAL ARGUMENTS.
Variables defined in a C program are called Local Variables. Local variables die after execution of the function.
Static Storage Class Variables in a function Retain their value among multiple calls.
Functions accept all types of data i.e int, long, float, double, char and pointers.
Arguments are values a function accepts from a calling function. Accepting arguments is not mandatory.
If you call a function SOMEFUNCTION() from main() function, then the main function is called CALLING FUNCTION and SOMEFUNCTION() is called CALLED FUNCTION.
There is no limit on the number of different functions being defined in a C program.
A function may be called any number of times.
Nesting of functions or defining a function inside another function is not allowed.
Passing or arguments are processed from Right to Left in C Language.
A function can call itself. This process is called Recursion.
C Programming Functions and Pointers
Example 1: Area of Rectangle Function
int area(int,int); int main() { int x=5, y=6; int z = area(x,y); int p = area(2,6); printf("%d, %d", z, p); return 9; } int area(int a, int b) { return (a*b); } //output //30, 12
In the above program, we passes arguments x and y to the function area(). Function area() returned calculated area which is assigned to the variable Z. You can directly call a function with constants without using any variables like area(2,6).
C Programming Functions and Pointers
Example 2: Using Library functions
#include<math.h> int main() { float x=1; float a = sin(x); printf("%f", a); return 9; }
We have to INCLUDE the Header file of the Library we want to use. Different functions are included in different libraries. As we are dealing with Trigonometric functions, we have included MATH.H file.
Recursion or Recursive Functions
A function which calls itself is called a Recursive Function and the process is called Recursion.
- To create a recursive function, exit condition must be specified without which it becomes never ending recursion and the program crashes.
- Recursion is slow compared to normal WHILE, FOR and DO WHILE loops.
- Recursion logic by one developer is difficult to understand for other developers.
- Usually, IF or ELSE conditions are used to END recursion.
Example 1:
int main() { int a = 4335; int no_of_digits = func(a); printf("NO=%d", no_of_digits); return 9; } int func(int p) { int k; if(p<10) return 1; else k = 1 + func(p/10); return k; } //output //NO=4 //Analysis //Step1: 1 + func(433) //Step2: 1 + (1 + func(43)) //Step3: 1 + (1 + (1 + func(4))) //Step4: 1 + (1 + (1 + 1) )
Above recursive function takes the input number a P. Using recursion, it counts number of digits present in the given number.
Pointers in Functions
When passing variables and variable addresses from one function to another, pointers are also used.
There are two ways of calling a function with arguments.
- Call By Value
- Call By Reference
1. Call By Value / Pass by Reference
If you pass only variables or constants to a function, it is called Call By Value. Because you do not use any Pointers or Addresses. Advantage of Call By Value is that the original values of passing variables do not change when you make changes in the called function.
Example
void show(int); int main() { int a=10; show(a); //CALL BY VALUE printf("%d", a); } void show(int x) { x = x + 1; //Here it is not a. printf("%d,", x); return; } //output: //11,10 //Value of a is not changed.
2. Call By Reference / Pass by Reference
Call By Reference passes addresses of variables to another function. So whatever changes you make to the variables in the CALLED function reflect in the CALLING function. It has side effects.
To pass addresses by call by reference, two operators are used: ADDRESS OF (&) and VALUE AT ADDRESS (*).
C Programming Functions and Pointers
Example
void show(int*); int main() { int a=10; show(&a); //CALL BY Reference printf("%d", a); } void show(int *x) { *x = *x + 1; //Here it is A only. printf("%d,", *x); return; } //output: //11,11 //Value of a is CHANGED.
&a is the address of the Variable ‘a’. *x is the value at the address passed. So you are changing the original copy of data in memory location passed. This is called CALL BY REFERENCE.
[WpProQuiz 32]
C Programming Functions and Pointers MCQ
1) What is the limit for number of functions in a C Program.?
A) 16
B) 31
C) 32
D) None of the above
Answer [=] D
Explanation:
Yes. There is no limit on the number of functions in a C Program.
2) Every C Program should contain which function.?
A) printf()
B) show()
C) scanf()
D) main()
Answer [=] D
Explanation:
main() is a compulsory function with or without returning anything.
void main(){}
int main(){return 0;}
3) What is the minimum number of functions to be present in a C Program.?
A) 1
B) 2
C) 3
D) 4
Answer [=] A
Explanation:
At least there will be one function which is main() function.
4) What is the output of C Program with functions.?
static void show(); int main() { printf("ROCKET "); show(); return 0; } static void show() { printf("STATIC"); }
A) ROCKET
B) ROCKET STATIC
C) STATIC ROCKET
D) Compiler error
Answer [=] B
Explanation:
Yes. A function can be static. Remember that a static variable or function has only FILE scope. You can use this function outside of the defined file even with extern static void show() prototype declaration.
5) What is the maximum number of statements that can present in a C function.?
A) 64
B) 128
C) 256
D) None of the above
Answer [=] D
Explanation:
There is no limit on the number of statements that can present in a C Function.
6) What characters are allowed in a C function name identifier.?
A) Alphabets, Numbers, %, $, _
B) Alphabets, Numbers, Underscore ( _ )
C) Alphabets, Numbers, dollar $
D) Alphabets, Numbers, %
Answer [=] B
Explanation:
Remember that a C function name can not start with a number but it can contain contain numbers after 1st character is either an Underscore ( _ ) or an Alphabet.
7) What is the output of C Program with functions and pointers.?
int main() { int b=25; //b memory location=1234; int *p = b; printf("%d %d", b, p); return 0; }
A) 25 1234
B) 25 0
C) 25 25
D) Compiler error
Answer [=] C
Explanation:
Integer Pointer *p copied the value of b to a new memory location. Its is same as int p= b;. Only p=&b points to the same memory of b.
8) What is the output of C Program with functions and pointers.?
int main() { int b=25; //b memory location=1234; int *p; p=&b; printf("%d %d %d", &b, p); return 0; }
A) 25 25
B) 1234 1234
C) 25 1234
D) 1234 25
Answer [=] B
Explanation:
Integer pointer is declared by int *p. p=&b makes pointer P to point the address of b. So &b and p hold only address.
9) What do you call STAR * and Ampersand & in a c program context.?
int a=10, *p; p = &a; printf("%d %d", a, *p);
A) * = ADDRESS OF operator, & = VALUE AT operator
B) * = ADDRESS OF operator, & = ADDRESS OF operator
C) * = VALUE AT operator, & = ADDRESS OF operator
D) * = VALUE AT operator, & = VALUE AT operator
Answer [=] C
Explanation:
&b gives ADDRESS OF b. *p gives VALUE AT memory location of b.
10) What is the output of C Program with functions.?
#include int sum(int,int); int main() { int a=5, b=10, mysum; mysum = sum(a,b); printf("SUM=%d ", mysum); printf("SUM=%d", sum(10,20)); return 0; } int sum(int i, int j) { return (i+j); }
A) SUM=15 SUM=30
B) SUM=30 SUM=15
C) SUM=15 SUM=15
D) SUM=30 SUM=30
Answer [=] A
Explanation:
You can call a function sum(10,20) inside another function printf directly.
11) Arguments passed to a function in C language are called ___ arguments.
A) Formal arguments
B) Actual Arguments
C) Definite Arguments
D) Ideal Arguments
Answer [=] B
12) Arguments received by a function in C language are called ___ arguments.
A) Definite arguments
B) Formal arguments
C) Actual arguments
D) Ideal arguments
Answer [=] B
13) Choose a corrects statement about C language function arguments.
A) Number of arguments should be same when sending and receiving
B) Type of each argument should match exactly
C) Order of each argument should be same
D) All the above
Answer [=] D
Explanation:
20.0 is float. But char j is Character type. So, it is a mismatch. Here exactly two arguments are passed and two arguments are received.
int main() { sum(10, 20.0); return 0; } int sum(int i, char j) { return 10; }
14) What is the output of C program with functions.?
int main() { printf("funny=%d" , funny()); return 0; } funny() { }
A) funny=
B) funny=1
C) funny=0
D) Compiler error
Answer [=] C
Explanation:
Any function that doesn’t have a return 0; explicitly specified at the end will have one by default.
15) What is the output of C Program with functions.?
void funny2(); int main() { printf("funny2=%d" , funny2()); return 0; } void funny2() { }
A) funny2=
B) funny2=0
C) funny2=1
D) Compiler error
Answer [=] D
Explanation:
void function does not return anything.
16) Choose a correct statement with C Functions.
A) A function can call any other function any number of times
B) You can write any function in any order in a multi function C File.
C) You can refer to or call any function using a Pointer also.
D) All the above
Answer [=] D
17) Choose a non Library C function below.
A) printf()
B) scanf()
C) fprintf()
D) printf2()
Answer [=] D
Explanation:
fprintf is used along with files. There is no library function PRINTF2().
18) What is the default return value of a C function if not specified explicitly.?
A) -1
B) 0
C) 1
D) None of the above
Answer [=] C
Explanation:
int funny() { //return 0; is added by compiler. }
19) What do you call this C Function calling itself.?
int funny2() { funny2(num); }
A) Indefinite Function
B) Definite Function
C) Cursive Function
D) Recursive Function
Answer [=] D
Explanation:
This recursive function hangs the CPU as there is no come out condition with IF.
20) What is the output of C Program with functions.?
int bunny(int,int); int main() { int a, b; a = bunny(5, 10); b = bunny(10, 5); printf("%d %d", a, b); return 0; } int bunny(int i, int j) { return (i, j); }
A) 5 10
B) 10 5
C) 5 5
D) Compiler error
Answer [=] B
Explanation:
Do not try to put the same return (a,b). It is a bad practice to return 2 values at a time. You can return only one value. Value right side value return j works every time.