C Programming Functions and Pointers Tutorial
Table of Contents
Learn Functions and Pointers in C Tutorial and 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.
Types of functions in C
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; }
Functions and Pointers in C
Notes about Writing and Working of a Function in C
- A function name should follow the same guidelines of choosing a Variable name
- A 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.
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).
Functions and Pointers in C examples
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.
Functions and Pointers
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
Functions and pointers are both used to pass variables and variable addresses from one function to another.
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.
C Programming Functions and Pointers
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 33]
Functions and Pointers in C MCQ
1) What are the data type of variables that can be returned by a C Function.?
A) int, float, double, char
B) struct, enum
C) Pointers to variables, arrays, functions, struct variables, enum variables etc
D) All the above
Answer [=] D
2) What is the output of a C Program with functions and pointers.?
void texas(int *,int *); int main() { int a=11, b=22; printf("Before=%d %d, ", a, b); texas(&a, &b); printf("After=%d %d", a, b); return 0; } void texas(int *i, int *j) { *i = 55; *j = 65; }
A) Before=11 22, After=11 22
B) Before=11 22, After=55 65
C) Before=11 22, After=0 0
D) Compiler error
Answer [=] B
Explanation:
Through the call by reference functions &a, &b, a and b are passed. Variables I and j thus gather the passed addresses. As a result, pointer-based modifications are kept even after the function has finished running.
3) What is the output of C Program with functions.?
int main() { int a = 66; printf("%d %d %d,\n", a, ++a, a++); a = 66; printf("%d %d %d,\n", ++a, a++, a); a = 66; printf("%d %d %d", ++a, a, a++); return 0; }
A)
68 68 66, 68 66 66, 68 68 66
B)
68 68 66, 66 66 68, 68 68 66
C)
68 68 66, 68 66 68, 68 68 66
D)
68 68 66, 68 66 68, 68 68 68
Answer [=] C
Explanation:
Order of processing arguments is right to left. First keep 3 placeholder buckets. Right to left check for higher priority of variables like ++a or a++ etc. Always process higher priority variables first. At last, fill the value of low priority variable a.
4) What is the output of a C program.?
int main() { int a = 1; printf("%d %d %d,\n", a, ++a, a++); a = 1; printf("%d %d %d,\n", ++a, a++, a); a = 1; printf("%d %d %d", ++a, a, a++); return 0; }
A)
3 3 1, 3 3 3, 3 3 1
B)
3 1 1, 3 1 3, 3 3 1
C)
3 3 1, 3 1 3, 3 1 1
D)
3 3 1, 3 1 3, 3 3 1
Answer [=] D
5) What is the output of a C Program.?
void show(int,int,int); int main() { int a = 1; show(++a, a++, a); return 0; } void show(int i, int j, int k) { printf("%d %d %d,\n", i, j, k); }
A)
1 1 3,
B)
3 1 3,
C)
3 1 1,
D)
3 3 3,
Answer [=] B
Explanation:
1) ++a, 2) a++ 3) a. Second and third arguments have higher priority than third parameter A. Second is therefore handled. Then, processing begins. Finally, the third-place finisher receives the actual value of at that time.
6) What is the output of C Program with pointers.?
int main() { int a = 4; int *p; p=&a; while(*p > 0) { printf("%d ", *p); (*p)--; } return 0; }
A) 0 0 0 0
B) 4 4 4 4
C) 4 3 2 1
D) Compiler error
Answer [=] C
Explanation:
Notice the decrement operation on variable “a” using (*p)–. If there are no parantheses, the variable is not decremented, but the memory location pointed to is decremented and has a garbage value.
7) What is the output of C Program with functions.?
void show(); int show(); int main() { printf("ANT\n"); return 0; } void show() { printf("Integer") ; } int show() { printf("Void"); }
A) ANT
B) Integer
C) Void
D) Compiler error
Answer [=] D
Explanation:
You can not declare same function with different return types. Here both void show() and int show() can not be written.
8) What is the output of C Program with functions.?
void show(int); void show(float); int main() { printf("ANT\n"); return 0; } void show(int a) { printf("Integer") ; } void show(float b) { printf("Void"); }
A) Integer Void
B) ANT Integer Void
C) ANT
D) Compiler error
Answer [=] D
Explanation:
You can not re declare and redefine the a function with same name and different arguments. Function name can not be duplicate in any combination.
9) What is the output of C Program with pointers.?
int main() { int a=10; int *p, **q; p=&a; q=&p; printf("%d ", a); *p=15; printf("%d ", a); **q=20; printf("%d ", a); return 0; }
A) 10 10 10
B) 10 0 0
C) 10 15 20
D) Compiler error
Answer [=] C
Explanation:
Here p is pointer to an integer. q is a pointer to pointer. That is why you should use two STARs **.
10) What is the output of C Program with pointers.?
int main() { int a=20; int *p, *q; p=&a; q=p; printf("%d ", a); *p=30; printf("%d ", a); *q=40; printf("%d ", a); return 0; }
A) 20 0 0
B) 20 20 20
C) 20 30 40
D) Compiler error
Answer [=] C
Explanation:
P is already a pointer to an integer a. Q copied P. So Q is also a pointer to the same integer a.
11) What is the output of C Program with pointers.?
int main() { int a=20; //a memory location=1234 printf("%d %d %d", a, &a, *(&a)); return 0; }
A) 20 20 20
B) 20 1234 1234
C) 20 1234 20
D) 20 20 20
Answer [=] C
Explanation:
The immediate use of the VALUEAT operator * and the ADDRESSOF operator & results in the same result as the variable itself. So *(&a) == a.
12) What is the output of C Program with functions.?
int main() { int a=20; printf("CINEMA "); return 1; printf("DINOSAUR"); return 1; }
A) CINEMA DINOSAUR
B) CINEMA
C) DINOSAUR
D) Compiler error
Answer [=] B
Explanation:
There are two return statements in main() function. Only first return is executed. All the statements or lines of code below first return is not reachable and hence not executed.
13) What is the output of C Program with recursive function.?
int sum(int); int main() { int b; b = sum(4); printf("%d", b); } int sum(int x) { int k=1; if(x<=1) return 1; k = x + sum(x-1); return k; }
A) 10
B) 11
C) 12
D) 15
Answer [=] A
Explanation:
4 + 3 + 2 + 1 = 10.
14) What is the output of C Program with Recursive Function.?
int mul(int); int main() { int b; b = mul(3); printf("%d", b); } int mul(int x) { if(x<=1) return 1; return (x * mul(x-1)); }
A) 2
B) 3
C) 6
D) 1
Answer [=] C
Explanation:
3*2*1 = 6.
15) A recursive function can be replaced with __ in c language.
A) for loop
B) while loop
C) do while loop
D) All the above
Answer [=] D
16) A recursive function is faster than __ loop.
A) for
B) while
C) do while
D) None of the above
Answer [=] D
Explanation:
Yes. It takes time to recur. Multiple times, variables are kept and removed from STACK memory.
17) A recursive function without If and Else conditions will always lead to.?
A) Finite loop
B) Infinite loop
C) Incorrect result
D) Correct result
Answer [=] B
Explanation:
Yes. To come out of recursion, you must use IF or ELSE blocks.
18) What is the C keyword that must be used to achieve expected result using Recursion.?
A) printf
B) scanf
C) void
D) return
Answer [=] D
Explanation:
Recursion is entirely based on RETURN value; statement.
19) How many functions are required to create a recursive functionality.?
A) One
B) Two
C) More than two
D) None of the above
Answer [=] A
Explanation:
To achieve recursion, only one function is needed. The same function is invoked repeatedly. There must be at least one main() function in order to use the results of all recursive calls.
20) Choose a correct statement about Recursive Function in C language.
A) Each recursion creates new variables at different memory locations
B) There is no limit on the number of Recursive calls
C) The use of pointers with recursion is also possible, but challenging.
D) All the above
Answer [=] D