Functions in C Programming PDF

Summary

This document introduces functions in C programming. It discusses different types of functions, including those with arguments and return values. It also compares monolithic and modular programming paradigms, highlighting the advantages and disadvantages of each.

Full Transcript

DGI Greater Noida Topic: Functions- Introduction, types of functions, functions with array, passing parameters to functions, Call by value, call by reference FUNCTION MONOLITHIC VS MODULAR PROGRAMMING: 1. Monolithic Programming indi...

DGI Greater Noida Topic: Functions- Introduction, types of functions, functions with array, passing parameters to functions, Call by value, call by reference FUNCTION MONOLITHIC VS MODULAR PROGRAMMING: 1. Monolithic Programming indicates the program which contains a single function for the large program. 2. Modular programming help the programmer to divide the whole program into different modules and each module is separately developed and tested. Then the linker will link all these modules to form the complete program. 3. On the other hand monolithic programming will not divide the program and it is a single thread of execution. When the program size increases it leads inconvenience and difficult to maintain. Disadvantages of monolithic programming: 1. Difficult to check error on large programs. 2. Difficult to maintain. 3. Code can be specific to a particular problem. i.e. it cannot be reused. Advantage of modular programming: 1. Modular program are easier to code and debug. 2. Reduces the programming size. 3. Code can be reused in other programs. 4. Problem can be isolated to specific module so easier to find the error and correct it. FUNCTION: A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. Function Declaration OR Function Prototype: 1. It is also known as function prototype. 2. It inform the computer about the three things a) Name of the function b) Number and type of arguments received by the function. c) Type of value return by the function Syntax: return type function name (type1 arg1 , type2 arg2); OR return type function name (type1 type2); 3. Calling function need information about called function.If called function is place before calling function then the declaration is not needed. Function Definition: 1. It consists of code description and code of a function. It consists of two parts a) Function header b) Function coding Function definition tells what the I/O functions are and what is going to do. Syntax: return type function name (type1 arg1 , type2 arg2) {local variable; statements ; return (expression); } Downloaded from : uptukhabar.net DGI Greater Noida 2. Function definition can be placed anywhere in the program but generally placed after the main function. 3. Local variable declared inside the function is local to that function. It cannot be used anywhere in the program and its existence is only within the function. 4. Function definition cannot be nested. 5. Return type denote the type of value that function will return and return type is optional if omitted it is assumed to be integer by default. USER DEFINE FUNCTIONS VS STANDARD FUNCTION: User Define Function: A function that is declare, calling and define by the user is called user define function. Every user define function has three parts as: 1. Prototype or Declaration 2. Calling 3. Definition FUNCTION CATAGORIES There are four main categories of the functions these are as follows: 1. Function with no arguments and no return values. 2. Function with no arguments and a return value. 3. Function with arguments and no return values. 4. Function with arguments and return values. Function with no arguments and no return values: syntax: void funct (void); main ( ) { funct ( ); } void funct ( void ); { } Function with no arguments and a return value: This type of functions has no arguments but a return value example: int msg (void) ; int main ( ) { int s = msg ( ); printf( ―summation = %d‖ , s); } int msg ( void ) { int a, b, sum ; sum = a+b ; return (sum) ; } Function with arguments and no return values: Example: void msg ( int , int ); int main ( ) Downloaded from : uptukhabar.net DGI Greater Noida { int a,b; a= 2; b=3; msg( a, b); } void msg ( int a , int b) { int s ; sum = a+b; printf (―sum = %d‖ , s ) ; } Function with arguments and return value: Here calling function of arguments that passed to the called function and called function return value to calling function. example: int msg ( int , int ) ; int main ( ) { int a, b; a= 2; b=3; int s = msg (a, b); printf (―sum = %d‖ , s ) ; } int msg( int a , int b) { int sum ; sum =a+b ; return (sum); } ACTUAL ARGUMENTS AND FORMAL ARGUMENTS Actual Arguments: 1. Arguments which are mentioned in the function in the function call are known as calling function. 2. These are the values which are actual arguments called to the function. Formal Arguments: 1. Arguments which are mentioned in function definition are called dummy or formal argument. 2. These arguments are used to just hold the value that is sent by calling function. 3. Formal arguments are like other local variables of the function which are created when function call starts and destroyed when end function. Basic difference between formal and local argument are: a) Formal arguments are declared within the ( ) where as local variables are declared at beginning. b) Formal arguments are automatically initialized when a value of actual argument is passed. c) Where other local variables are assigned variable through the statement inside the function body. Downloaded from : uptukhabar.net DGI Greater Noida PARAMETER PASSING TECHNIQUES: Call by Value and Call by Reference in C On the basis of arguments there are two types of function are available in C language, they are;  With argument  Without argument If a function takes any arguments, it must declare variables that accept the values as a arguments. These variables are called the formal parameters of the function. There are two ways to pass value or data to function in C language which is given below;  call by value  call by reference Call by value In call by value, original value can not be changed or modified. In call by value, when you passed value to the function it is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only but it not change the value of variable inside the caller method such as main(). Downloaded from : uptukhabar.net DGI Greater Noida Call by value #include #include void swap(int a, int b) { int temp; temp=a; a=b; b=temp; } void main() { int a=100, b=200; clrscr(); swap(a, b); // passing value to function printf("\nValue of a: %d",a); printf("\nValue of b: %d",b); getch(); } Output Value of a: 200 Value of b: 100 Call by reference In call by reference, original value is changed or modified because we pass reference (address). Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, any value changed inside the function, is reflected inside as well as outside the function. Example Call by reference #include #include void swap(int *a, int *b) { int temp; temp= *a; *a=*b; Lecture Notes||Programming for Problem Solving (KCS-101/KCS-201 58 Downloaded from : uptukhabar.net DGI Greater Noida } void main() { int a=100, b=200; clrscr(); swap(&a, &b); // passing value to function printf("\nValue of a: %d",a); printf("\nValue of b: %d",b); getch(); } Output Value of a: 200 Value of b: 100 Difference between call by value and call by reference. call by value call by reference This method copy original value into function as a This method copy address of arguments into arguments. function as a arguments. Changes made to the parameter affect the argument. Changes made to the parameter inside the function Because address is used to access the actual have no effect on the argument. argument. Actual and formal arguments will be created in Actual and formal arguments will be created in same different memory location memory location Lecture Notes||Programming for Problem Solving (KCS-101/KCS-201)|| 59 Downloaded from : uptukhabar.net DGI Greater Noida Topic: Recursion When Function is call within same function is called Recursion. The function which calls same function is called recursive function. In other word when a function calls itself then that function is called Recursive function. Recursive function are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series, etc. Advantage of Recursion  Function calling related information will be maintained by recursion.  Stack evaluation will be take place by using recursion.  In fix prefix, post-fix notation will be evaluated by using recursion. Disadvantage of Recursion  It is a very slow process due to stack overlapping.  Recursive programs can create stack overflow.  Recursive functions can create as loops. Find the Factorial of any number using recursion Example #include #include void main() { int fact(int); int i,f,num; clrscr(); printf("Enter any number: "); scanf("%d",&num); f=fact(num); printf("Factorial: %d",f); getch(); } int fact(int n) { if(a

Use Quizgecko on...
Browser
Browser