SlideShare a Scribd company logo
1 of 6
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 1
DHANALAKSHMI COLLEGE OF ENGINEERING
Tambaram, Chennai
Department of Computer Science and Engineering
OCS752 INTRODUCTION TO C PROGRAMMING
Year / Sem : IV / VII
2 Marks Q & A
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 2
UNIT IV
FUNCTIONS
Introduction to Functions – Types: User-defined and built-in functions – Function prototype – Function
definition – Function call – Parameter passing: Pass by value – Pass by reference – Built-in functions
(string functions) – Recursive functions – Exercise programs: Calculate the total amount of power
consumed by ‘n’ devices (passing an array to a function) – Menu-driven program to count the numbers
which are divisible by 3, 5 and by both (passing an array to a function) – Replace the punctuations
from a given sentence by the space character (passing an array to a function)
PART – A
1. What is a function? (N/D – 14, N/D – 16, N/D – 19)
A function is a group of statements that together perform a task. The general form of a function
definition in C language:
return_type function_name( parameter list )
{
body of the function
}
2. Specify the advantages of function. (A/M – 16, A/M – 19)
Advantages of function
1) The program will be easier to understand, maintain and debug.
2) Reusable codes that can be used in other programs
3) A large program can be divided into smaller modules. Hence, a large project can be divided
among many programmers.
3. What does a function header and function body consist of?
A function definition consists of
1) Function header
2) Function body
The Function header consists of
1) Return Type
2) Function Name
3) Parameters
The Function body consists of
Declarations and statements necessary for performing the required task.
4. What is a recursive function?
A Function calls itself again and again, then that function is called Recursive function. This
technique is known as recursion.
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 3
Example:
void recursion()
{ recursion(); /* function calls itself */
}
int main()
{
recursion();
}
5. Differentiate pass by value from pass by reference. (A/M – 14, N/D – 17)
S. No. Pass by value Pass by reference
1 A copy of actual arguments is passed to
formal arguments of the called function.
The location (address) of actual arguments is
passed to formal arguments of the called
function.
2 Actual arguments will remain safe and
they cannot be modified accidentally.
Alteration to actual arguments is possible
within the called function.
6. List the advantages of recursion. (A/M – 18)
Advantages of recursion
1) Reduce unnecessary calling of function.
2) Through recursion one can solve problems in easy way, while its iterative solution is very big
and complex.
7. What are actual parameters and formal parameters? (A/M – 15)
Functions can take two kinds of parameters.
1) Actual parameter
2) Formal parameter
Actual parameters are parameters as they appear in function calls. Formal parameters are parameters
as they appear in function declarations.
8. Write a program to print the first 50 prime numbers recursively. (N/D – 15)
Program to print the first 50 prime numbers recursively
#include<stdio.h>
void main ( )
{
intn,i=3, j, c;
scanf("%d",&n);
for(i=3;i<=n;i++)
{
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 4
c=0;
{
for (j=1;j<=i;j++)
if(i%j==0)
c++;
}
if(c==2)
{
printf("%d",i);
}}}
Output:
50
357111317192329313739414347
9. What are the components of a function? (A/M – 17)
Components of a function
1) Function declaration
2) Function definition
3) Function call
10. What are the steps in writing a function in a program? (N/D – 19)
Steps in writing a function in a program
1) Function Declaration (Prototype declaration): Every user-defined function has to be declared
before the main ().
2) Function Callings: The user-defined functions can be called inside any function like main(),
user-defined function, etc.
3) Function Definition: Once a function is declared and defined, it can be called any number of
times, from any function.
11. Is it better to use a macro or a function? (N/D – 19)
Macros are more efficient than function because their corresponding code is inserted directly at the
point where the macro is called. Macros are generally small and cannot handle large, complex
coding constructs. Functions are used for large, complex coding constructs.
12. Is main() a library function in C?
The function main() is not a predefined or built in function. It is a user defined function with a
predefined function prototype (also called function signature). It is neither user defined nor a built in
library function.
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 5
13. Distinguish between exit ( ) and return in C.
S. No. exit() return
1) exit() is a system call (not a language
statement) that terminates the current
process.
return is an instruction of the language
that returns from a function call.
2) #include <stdio.h>
void f()
{
printf("Executing fn");
exit(0);
}
int main()
{
f();
printf("Back from fn");
}
#include <stdio.h>
void f()
{
printf("Executing fn");
return;
}
int main()
{
f();
printf("Back from fn");
}
14. How to generate random numbers in C?
rand() function is used in C to generate random numbers. If we generate a sequence of random
number with rand() function, it will create the same sequence again and again every time program
runs.
15. Classify the functions based on arguments and return values.
Depending on the arguments and return values, functions are classified into four types.
1) Function without arguments and return values.
2) Function with arguments but without return values.
3) Function without arguments but with return values.
4) Function with arguments and return values.
16. List the types of functions in C programming.
Depending on whether a function is defined by the user or already included in C compilers,
there are two types of functions in C programming
1) Standard library functions
2) User defined functions
17. List the advantages of functions. (A/M – 14)
Advantages of functions
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 6
1) Functions are self-contained block or sub program of one or more statements that performs a
specific task.
2) It increases the modularity, reusability of a program.
18. List the advantages of user-defined function.
Advantages of user defined functions
1) The program will be easier to understand, maintain and debug.
2) Reusable codes that can be used in other programs
3) A large program can be divided into smaller modules. A large project can be divided among
many programmers.
19. What are standard library functions?
The standard library functions are built-in functions in C programming to handle tasks such as
mathematical computations, I/O processing, string handling. These functions are defined in the
header file.
20. What is function prototyping? Why it is necessary? (A/M – 11)
Many built in functions can be used in C programs. The prototype of these functions is given in the
respective header files. With the help of a function prototype, the compiler can automatically
perform type checking on the definition of the function, which saves the time to delay the program.

More Related Content

What's hot

Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++Vishesh Jha
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
Subroutine & string in 8086 Microprocessor
Subroutine & string in 8086 MicroprocessorSubroutine & string in 8086 Microprocessor
Subroutine & string in 8086 MicroprocessorMustafa AL-Timemmie
 
Computer organization-and-architecture-questions-and-answers
Computer organization-and-architecture-questions-and-answersComputer organization-and-architecture-questions-and-answers
Computer organization-and-architecture-questions-and-answersappasami
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesSreedhar Chowdam
 
Central processing unit and stack organization r013
Central processing unit and stack organization   r013Central processing unit and stack organization   r013
Central processing unit and stack organization r013arunachalamr16
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphismkiran Patel
 
8085 logical instruction
8085 logical instruction8085 logical instruction
8085 logical instructionprashant1271
 
Functions in c++
Functions in c++Functions in c++
Functions in c++Maaz Hasan
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
memory reference instruction
memory reference instructionmemory reference instruction
memory reference instructionDeepikaT13
 
Basic concepts in Verilog HDL
Basic concepts in Verilog HDLBasic concepts in Verilog HDL
Basic concepts in Verilog HDLanand hd
 
Computer Organization : CPU, Memory and I/O organization
Computer Organization : CPU, Memory and I/O organizationComputer Organization : CPU, Memory and I/O organization
Computer Organization : CPU, Memory and I/O organizationAmrutaMehata
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILEDipta Saha
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsAakash deep Singhal
 

What's hot (20)

C fundamental
C fundamentalC fundamental
C fundamental
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Subroutine & string in 8086 Microprocessor
Subroutine & string in 8086 MicroprocessorSubroutine & string in 8086 Microprocessor
Subroutine & string in 8086 Microprocessor
 
Function in C
Function in CFunction in C
Function in C
 
Computer organization-and-architecture-questions-and-answers
Computer organization-and-architecture-questions-and-answersComputer organization-and-architecture-questions-and-answers
Computer organization-and-architecture-questions-and-answers
 
8085 instruction set
8085 instruction set8085 instruction set
8085 instruction set
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
Central processing unit and stack organization r013
Central processing unit and stack organization   r013Central processing unit and stack organization   r013
Central processing unit and stack organization r013
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphism
 
8085 logical instruction
8085 logical instruction8085 logical instruction
8085 logical instruction
 
design of accumlator
design of accumlatordesign of accumlator
design of accumlator
 
8085 alp programs
8085 alp programs8085 alp programs
8085 alp programs
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
memory reference instruction
memory reference instructionmemory reference instruction
memory reference instruction
 
Basic concepts in Verilog HDL
Basic concepts in Verilog HDLBasic concepts in Verilog HDL
Basic concepts in Verilog HDL
 
Computer Organization : CPU, Memory and I/O organization
Computer Organization : CPU, Memory and I/O organizationComputer Organization : CPU, Memory and I/O organization
Computer Organization : CPU, Memory and I/O organization
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 

Similar to Ocs752 unit 4

U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptManivannan837728
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
Functions part1
Functions part1Functions part1
Functions part1yndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directivesVikash Dhal
 
Functional Paradigm.pptx
Functional Paradigm.pptxFunctional Paradigm.pptx
Functional Paradigm.pptxFurretMaster
 
functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxMehakBhatia38
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesUmesh Nikam
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10Jadavsejal
 
Lecture 3.2.4 C pointer to Structure.pptx
Lecture 3.2.4 C pointer to Structure.pptxLecture 3.2.4 C pointer to Structure.pptx
Lecture 3.2.4 C pointer to Structure.pptxravi2692kumar
 

Similar to Ocs752 unit 4 (20)

U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
C structure
C structureC structure
C structure
 
Functions part1
Functions part1Functions part1
Functions part1
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
3 Function & Storage Class.pptx
3 Function & Storage Class.pptx3 Function & Storage Class.pptx
3 Function & Storage Class.pptx
 
Functional Paradigm.pptx
Functional Paradigm.pptxFunctional Paradigm.pptx
Functional Paradigm.pptx
 
functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptx
 
FUNCTIONS.pptx
FUNCTIONS.pptxFUNCTIONS.pptx
FUNCTIONS.pptx
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Function
FunctionFunction
Function
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
 
Lecture 3.2.4 C pointer to Structure.pptx
Lecture 3.2.4 C pointer to Structure.pptxLecture 3.2.4 C pointer to Structure.pptx
Lecture 3.2.4 C pointer to Structure.pptx
 

Recently uploaded

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

Ocs752 unit 4

  • 1. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 1 DHANALAKSHMI COLLEGE OF ENGINEERING Tambaram, Chennai Department of Computer Science and Engineering OCS752 INTRODUCTION TO C PROGRAMMING Year / Sem : IV / VII 2 Marks Q & A
  • 2. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 2 UNIT IV FUNCTIONS Introduction to Functions – Types: User-defined and built-in functions – Function prototype – Function definition – Function call – Parameter passing: Pass by value – Pass by reference – Built-in functions (string functions) – Recursive functions – Exercise programs: Calculate the total amount of power consumed by ‘n’ devices (passing an array to a function) – Menu-driven program to count the numbers which are divisible by 3, 5 and by both (passing an array to a function) – Replace the punctuations from a given sentence by the space character (passing an array to a function) PART – A 1. What is a function? (N/D – 14, N/D – 16, N/D – 19) A function is a group of statements that together perform a task. The general form of a function definition in C language: return_type function_name( parameter list ) { body of the function } 2. Specify the advantages of function. (A/M – 16, A/M – 19) Advantages of function 1) The program will be easier to understand, maintain and debug. 2) Reusable codes that can be used in other programs 3) A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers. 3. What does a function header and function body consist of? A function definition consists of 1) Function header 2) Function body The Function header consists of 1) Return Type 2) Function Name 3) Parameters The Function body consists of Declarations and statements necessary for performing the required task. 4. What is a recursive function? A Function calls itself again and again, then that function is called Recursive function. This technique is known as recursion.
  • 3. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 3 Example: void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); } 5. Differentiate pass by value from pass by reference. (A/M – 14, N/D – 17) S. No. Pass by value Pass by reference 1 A copy of actual arguments is passed to formal arguments of the called function. The location (address) of actual arguments is passed to formal arguments of the called function. 2 Actual arguments will remain safe and they cannot be modified accidentally. Alteration to actual arguments is possible within the called function. 6. List the advantages of recursion. (A/M – 18) Advantages of recursion 1) Reduce unnecessary calling of function. 2) Through recursion one can solve problems in easy way, while its iterative solution is very big and complex. 7. What are actual parameters and formal parameters? (A/M – 15) Functions can take two kinds of parameters. 1) Actual parameter 2) Formal parameter Actual parameters are parameters as they appear in function calls. Formal parameters are parameters as they appear in function declarations. 8. Write a program to print the first 50 prime numbers recursively. (N/D – 15) Program to print the first 50 prime numbers recursively #include<stdio.h> void main ( ) { intn,i=3, j, c; scanf("%d",&n); for(i=3;i<=n;i++) {
  • 4. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 4 c=0; { for (j=1;j<=i;j++) if(i%j==0) c++; } if(c==2) { printf("%d",i); }}} Output: 50 357111317192329313739414347 9. What are the components of a function? (A/M – 17) Components of a function 1) Function declaration 2) Function definition 3) Function call 10. What are the steps in writing a function in a program? (N/D – 19) Steps in writing a function in a program 1) Function Declaration (Prototype declaration): Every user-defined function has to be declared before the main (). 2) Function Callings: The user-defined functions can be called inside any function like main(), user-defined function, etc. 3) Function Definition: Once a function is declared and defined, it can be called any number of times, from any function. 11. Is it better to use a macro or a function? (N/D – 19) Macros are more efficient than function because their corresponding code is inserted directly at the point where the macro is called. Macros are generally small and cannot handle large, complex coding constructs. Functions are used for large, complex coding constructs. 12. Is main() a library function in C? The function main() is not a predefined or built in function. It is a user defined function with a predefined function prototype (also called function signature). It is neither user defined nor a built in library function.
  • 5. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 5 13. Distinguish between exit ( ) and return in C. S. No. exit() return 1) exit() is a system call (not a language statement) that terminates the current process. return is an instruction of the language that returns from a function call. 2) #include <stdio.h> void f() { printf("Executing fn"); exit(0); } int main() { f(); printf("Back from fn"); } #include <stdio.h> void f() { printf("Executing fn"); return; } int main() { f(); printf("Back from fn"); } 14. How to generate random numbers in C? rand() function is used in C to generate random numbers. If we generate a sequence of random number with rand() function, it will create the same sequence again and again every time program runs. 15. Classify the functions based on arguments and return values. Depending on the arguments and return values, functions are classified into four types. 1) Function without arguments and return values. 2) Function with arguments but without return values. 3) Function without arguments but with return values. 4) Function with arguments and return values. 16. List the types of functions in C programming. Depending on whether a function is defined by the user or already included in C compilers, there are two types of functions in C programming 1) Standard library functions 2) User defined functions 17. List the advantages of functions. (A/M – 14) Advantages of functions
  • 6. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 6 1) Functions are self-contained block or sub program of one or more statements that performs a specific task. 2) It increases the modularity, reusability of a program. 18. List the advantages of user-defined function. Advantages of user defined functions 1) The program will be easier to understand, maintain and debug. 2) Reusable codes that can be used in other programs 3) A large program can be divided into smaller modules. A large project can be divided among many programmers. 19. What are standard library functions? The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling. These functions are defined in the header file. 20. What is function prototyping? Why it is necessary? (A/M – 11) Many built in functions can be used in C programs. The prototype of these functions is given in the respective header files. With the help of a function prototype, the compiler can automatically perform type checking on the definition of the function, which saves the time to delay the program.