IDOL Institute of Distance and Online Learning ENHANCE YOUR QUALIFICATION, ADVANCE YOUR CAREER.
BCA 2 All right are reserved with CU-IDOL Computer Programming Course Code: BCA 612 Semester: First SLM Unit: e-Lesson No.: 6 4 www.cuidol.in Unit- 6 (BCA112)
Functions and 33 Macros OBJECTIVES INTRODUCTION • Explain the concepts of functions In this session we are going to learn about • Describe different types of functions • The concepts of functions • Discuss recursive function • Different types of functions and • Explain the concepts of pre-processor • Recursive function • Describe pre-processor directives • The concepts of pre-processor • elaborate the concepts of predefined macros • Pre-processor directives • discuss the concepts of parameterized macros • The concepts of predefined macros • The concepts of parameterized macros www.cuidol.in Unit- 6 (BCAA611122)) INASlTl ITriUgThEt OarFeDrIeSsTeArNvCeEd AwNitDh OCNUL-IIDNOE LLEARNING
TOPICS TO BE COVERED 43 • Concepts of Function INTRODUCTION • User Defined Functions • Functions Based on Arguments it takes and Return statement • Function Calling • Recursive Function • Concepts of pre-processor • Pre-processor directives • Predefined macros • Parameterized macros www.cuidol.in Unit- 6 (BCA112) IANlSlTIrTigUhTtEaOreF rDeIsSTeArvNeCdEwAiNthDCOUN-LIDINOELLEARNING
Concepts of Function 5 • A function is a block of statements that performs a specific task. • We use functions due to the following reasons – To improve the readability of code. Improves the reusability of the code, 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 set of statements are replaced by function calls. • There are two types of functions – System defined functions - printf(), scanf(), puts(), gets() etc User defined functions - msg(), factorial(), average() etc www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
User Defined Functions 6 • The functions that we create in a program are known as user defined functions. • Function declaration (function prototype) Syntax– return_type function_name (argument list) Example- int sum(int, int); • Function Definition Syntax- // Function header return_type function_name (argument list) // Body of the function { Set of statements; } Example– int sum(int a, int b) { int s; s = a + b; return (s); } www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
User Defined Functions 7 • User defined functions can be defined before or after the main() function. www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Functions Based on Arguments it 8 takes and Return statement • Based on argument it takes and Return statement Functions can be categorize as – Function with no argument and no return value Function with arguments but no return value Function with no arguments but returns a value Function with arguments and return value www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Functions Based on Arguments 9 it takes and Return statement • Function with no argument and no return value #include <stdio.h> Output – void usa(); void main() I am in main { I am in USA printf(\"I am in main\"); usa(); } void usa() { printf(\"\\n I am in USA\"); } www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Functions Based on Arguments it takes 10 and Return statement • Function with arguments but no return value Output – #include <stdio.h> Hello void sum(int, int); Sum = 30 void main() { int a = 10, b = 20; printf(“Hello\"); sum(a,b); } void sum(int x, int y) { printf(\"\\n Sum = %d”, x+y); } www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Functions Based on Arguments it takes 11 and Return statement • Function with no arguments but returns a value Output – Sum = 35 #include <stdio.h> int sum(); All right are reserved with CU-IDOL void main() { int add; add = sum(); printf(“\\n Sum = %”, add); } int sum() { int a =10, b = 25; return(a+b); } www.cuidol.in Unit- 6 (BCA112)
Functions Based on Arguments it takes 12 and Return Statement • Function with arguments and return value Output – Sum = 106 #include <stdio.h> int sum(int, int); void main() { int a = 25, b = 81, add; add = sum(a,b); printf(“\\n Sum = %”, add); } int sum(int x, int y) { return(x+y); } www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Function Calling 13 • Function calling means how a function can be called from another function. • Functions can be called by following two ways – Call by value Call by reference • Actual arguments: The parameters that appear in function call. • Formal arguments: The parameters that appear in function definition. • Call by value – Changes made to the formal argument will not be reflected to actual arguments. • Call by reference - Changes made to the formal argument will be reflected to actual arguments. www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Function Calling 14 • Call by value Output – #include <stdio.h> Before swapping: a= 10, b= 20 void swap( int a, int b ) In swap after swapping: a= 20, b= 10 { In main after swapping: a= 10, b= 20 int temp; temp = a; a = b; b = temp; printf(\"\\n In swap after swapping: a= %d, b= %d\", a,b); } void main( ) { int a = 10, b = 20 ; printf(\"Before swapping: a= %d, b= %d\", a,b); swap(a,b); printf(\"\\n In main after swapping: a= %d, b= %d\", a,b); } www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Function Calling 15 • Call by reference Output – • Pointer is a variable that holds the address of another variable. Before swapping: a= 10, b= 20 In swap after swapping: a= 20, b= 10 #include <stdio.h> In main after swapping: a= 20, b= 10 void swap( int *a, int *b ) { int temp; temp = *a; *a = *b; *b = temp; printf(\"\\n In swap after swapping: a= %d, b= %d\", *a,*b); } void main( ) { int a = 10, b = 20 ; printf(\"Before swapping: a= %d, b= %d\", a, b); swap(&a, &b); printf(\"\\n In main after swapping: a= %d, b= %d\", a, b); } www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Recursive Function 16 • A function which calls itself is known as recursive function. • It can be used as an alternative to loops. • www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Recursive Function 17 • Example – www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Concepts of Pre-processor 18 • A C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre- processing before the actual compilation. • All preprocessor commands begin with a hash symbol (#). • It performs preprocessing of the High Level Language (HLL). • •Preprocessing is the first step of the language processing system. • Language processing system translates the Fig.: Preprocessing Steps of the High Level Language (HLL) high level language to machine level language or absolute machine code. www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Pre-processor Directives 19 • List of Preprocessor Directives www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Pre-processor Directives 20 Macro: #define Example: • The syntax to use define macro is as follows: #include<stdio.h> #define PI 3.14 #define Identifier_name value void main() { • Where Identifier_name represents a symbolic float radius=1.2; name, generally written in uppercase letters. float area_of_circle = PI * radius * radius; float circumference_of_circle = 2 * PI * radius; • Pre-processor statements not end with a printf(“\\n Details of circle”); semicolon. printf(“\\n Area = %f”, area_of_circle); printf(“\\n Circumference = %f”, circumference_of_circle); } www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Pre-processor Directives 21 File Inclusion: #include #include <filename> vs #include \"filename\" • The file preprocessor directive causes one file to be included in another. #include <filename> • The preprocessor searches directories pre-designated • The preprocessor command for file by the compiler. inclusion looks like this: • This method is used to include standard library header files. #include <file_name> #include \"filename\" • The #include directive can be used in two • The preprocessor searches in the same directory as the ways as discussed below: file containing the directive. • If this fails, then it starts behaving like the #include #include “filename” <filename> form. #include <filename> • This method is usually used to include your own header #include “c:\\employee.c” files. #include <math.h> www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Pre-processor Directives 22 Conditional Compilation • Conditional compilation as the name implies that the code is compiled if certain condition(s) hold true. Example - #include <stdio.h> #define xyz 10 void main() { #ifdef xyz printf(\"hi\\n\"); // this is compiled as x is defined #else printf(\"bye\\n\"); // this isn't compiled #endif } www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Pre-processor Directives 23 Undef Directive Example - • Undefines a symbol specified by <identifier> which was #include<stdio.h> previously defined with a #define directive. #define MARKS 40 #undef Directive is used to undefine any Macro #ifdef MARKS #undef MARKS Symbol. #define MARKS 88 #undef can undefine only “User Defined” Macro’s. #else #define MARKS 100 #undef is used where we have to redefine any #endif Macro Identifier. int main() www.cuidol.in Unit- 6 (BCA112) { printf(\"%d“,MARKS); return 0; } Output: 88 All right are reserved with CU-IDOL
Predefined Macros 24 • A predefined macro is a macro that is already understood by the Example: C preprocessor without the program needing to define it. #include <stdio.h> void main() { • Here are some predefined macros in C programming: printf(“File :%s\\n”, __FILE__ ); printf(“Date :%s\\n”, __DATE__ ); printf(“Time :%s\\n”, __TIME__ ); printf(“Line :%d\\n”, __LINE__ ); printf(“ANSI :%d\\n”, __STDC__ ); } When the above code in a file test.c is compiled and executed, it produces the following Output – File :test.c Date :Jun 2 2012 Time :03:36:24 Line :8 ANSI :1 www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Parameterized Macros • Parameterized macros can be used to create templates for 25 frequently used parts of code. • Frequently, they serve as simple functions. Example: • Syntax of a parameterized macro is as follows: #include <stdio.h> #define QUBE (N) (N*N*N) #define identifier(parameterlist) replacement-list •There should be no spaces between the identifier and the int main() parameter list. There should be no spaces in the parameter list. { • Example: printf(\"%d\\n\",QUBE(10)); #define IS_EVEN(m) ((m)%2 == 0) return 0; #define max(a,b) (((a)>(b))?(a):(b)) } • The macro makes program execution faster because the macro is substituted in line and no function calling overhead is Output: required. 1000 www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Multiple Choice Questions 26 1. What is the default return type if it is not specified in function definition? a) void b) char c) float d) int 2. Recursion is similar to which of the following? a) Switch Case b) Loop c) If-else d) if elif else Answers: 1. d) 2.b) www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Multiple Choice Questions 27 3. What is the output of a C program with functions.? void show(); void main() { show(); printf(\"RAINBOW \"); } void show() { printf(\"BRIGHT \"); } a) RAINBOW BRIGHT b) BRIGHT c) BRIGHT RAINBOW d) Compiler error Answer: c) www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Multiple Choice Questions 4. Processor Directive in C language starts with ___ 28 a) $ b) # c) & d) @ 5. What is the output of C program.? #define LOGIC(a,b) (a= =b) void main() { if(LOGIC(5,5)) { printf(\"SAME \"); } } a) SAME b) LOGIC(5,5) c) 5==5 d) Compiler error Answers: 4.b) 5.a) www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
SUMMARY 29 Let us recapitulate the important concepts discussed in this session: • A function is a block of statements that performs a specific task. It reduces the size of the code, duplicate set of statements are replaced by function calls. •There are two types of functions – • System defined functions- Functions created by system and available in C library. • User defined functions - Functions created by users •Based on argument it takes and Return statement Functions can be categorize as – • Function with no argument and no return value • Function with arguments but no return value • Function with no arguments but returns a value • Function with arguments and return value •Function calling means how a function can be called from another function. •Functions can be called by following two ways – Call by value, Call by reference. •A function which calls itself is known as recursive function. www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
SUMMARY 30 •A C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre- processing before the actual compilation. •All preprocessor commands begin with a hash symbol (#). •Pre-processor statements not end with a semicolon. •Macros are used to define some identifier for a value substitution. •Conditional compilation as the name implies that the code is compiled if certain condition(s) hold true. •Undefines a symbol specified by <identifier> which was previously defined with a #define directive. •A predefined macro is a macro that is already understood by the C preprocessor without the program needing to define it. •Parameterized macros can be used to create templates for frequently used parts of code. Frequently, they serve as simple functions. www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
FREQUENTLY ASKED QUESTIONS 31 Q1. Define function. How it is beneficial? Ans. A function is a block of statements that performs a specific task. For further details please refer to the slide no. 6 and subject SLM unit 6. Q2. Differentiate between call by value and call by reference. Ans: Call by value – Changes made to the formal argument will not be reflected to actual arguments. Call by reference - Changes made to the formal argument will be reflected to actual arguments. For further details please refer to the subject SLM unit 6. Q3. Explain different types of functions based on argument it takes and value returned. Ans: Based on argument it takes and Return statement Functions can be categorize as – Function with no argument and no return value Function with arguments but no return value Function with no arguments but returns a value Function with arguments and return value For further details please refer to the subject SLM unit 6. www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
FREQUENTLY ASKED QUESTION 32 Q4: Describe recursive function. Ans: A function which calls itself is known as recursive function. For further details please refer to the subject SLM unit 6. Q5. Define preprocessor. Describe different types of preprocessor. Ans. A C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre- processing before the actual compilation. For further details please refer to the subject SLM unit 6. Q6. Illustrate parameterized macros with suitable example. Ans. Parameterized macros can be used to create templates for frequently used parts of code. Frequently, they serve as simple functions. For further details please refer to the subject SLM unit 6. www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
REFERENCES 33 • CU-IDOL’s “Computer Programming” SLM • Balaguruswamy (2017).Programming in ANSI C. New Delhi: McGraw-Hill. • Kanetkar, Y. (2014), Programming in C ANSI standard.New Delhi: BPB Publications. • Gottfried (2005).Programming with C. New York: Tata McGraw Hill. • Harrow K., Jones J. (1996).Problem Solving with C. London: Pearson Education. • Jeri R., Hanly,KoffmanE.P. (2000).Problem Solving and Program Design in C. 3rd Ed. Boston: Addison Wesley. www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
34 THANK YOU www.cuidol.in Unit- 6 (BCA112) All right are reserved with CU-IDOL
Search
Read the Text Version
- 1 - 34
Pages: