Docsity

Understanding C Functions: Predefined, User-Defined, and Call by Value or Reference, Study notes of Computer Engineering and Programming

Toggle sidebar
bg1
UNIT 3
FUNCTION:-
WHAT IS C FUNCTION?
A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by “{ }” which
performs specific operation in a C program. Actually, Collection of these functions creates a C program.
USES OF C FUNCTIONS:
•C functions are used to avoid rewriting same logic/code again and again in a program.
•There is no limit in calling C functions to make use of same functionality wherever required.
•We can call functions any number of times in a program and from any place in a program.
•A large C program can easily be tracked when it is divided into functions.
•The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve
understandability of very large C program
•Types of Functions
FUNCTION
Predefined Function
Call by
reference
User Defined
Function
Call By
value
Buit in
Function
OR
Predefined
Function
1. No
Argument
and no
Return
value
printf()
stdio.h
scanf()
getch();
conio.h
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Understanding C Functions: Predefined, User-Defined, and Call by Value or Reference and more Study notes Computer Engineering and Programming in PDF only on Docsity!

UNIT 3

FUNCTION:-

WHAT IS C FUNCTION?

A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by “{ }” which performs specific operation in a C program. Actually, Collection of these functions creates a C program.

USES OF C FUNCTIONS:

  • C functions are used to avoid rewriting same logic/code again and again in a program.
  • There is no limit in calling C functions to make use of same functionality wherever required.
  • We can call functions any number of times in a program and from any place in a program.
  • A large C program can easily be tracked when it is divided into functions.
  • The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C program

• Types of Functions

FUNCTION

Predefined Function

Call by reference

User Defined Function

Call By value Function^ Buit in PredeORfined Function

  1. No Argument and no Return value

printf() stdio.h scanf() getch(); conio.h

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 There are three types of functions in C programming:

  1. Standard library functions or Built in function or Predefined Function
  2. User defined functions or Call by value
  3. Call by Reference
  4. Standard library functions or Buit in function or Predefined Function The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc.These functions are defined in the header file. When you include the header file, these functions are available for use. For example:

The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in "stdio.h" header file.There are other numerous library functions defined under "stdio.h" , such as scanf() , fprintf() , getchar() etc. Once you include "stdio.h" in your program, all these functions are available for use.

  1. User defined functions As mentioned earlier, C allow programmers to define functions. Such functions created by the user are called user-defined functions.Depending upon the complexity and requirement of the program, you can create as many user-defined functions as you want.

C FUNCTION DECLARATION, FUNCTION CALL AND FUNCTION DEFINITION:

Advantages of user-defined 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.
  4. Here is a example to add two integers. To perform this task, a user-defined function add Numbers() is defined.

Function Definition in C Programming:

#include<stdio.h> #include<conio.h> int sum(int n1,int n2) { return(n1+n2); }

int main() { int num1=11,num2=22; int result;

result = sum(num1,num2); printf("\nResult : %d",result); return(0); } Explanation of Live Example :

Function Nane Sum Return Type Integer Calling Function Main

Programmes:-Write a programme to display add two numbers using Function.

With Argument & with Return Value No Argument & with Return value

With Argument & No Return value No Argument & No Return Value

#include <stdio.h> #include<conio.h> int add (int, int ); //Function prototype main() { Int ans,b,c; clrscr(); printf(“enter no.”); scanf(“%d%d”,&b,&c); ans=add(int a,int b);/ /Function Call printf(“%d”,ans);

#include <stdio.h> #include<conio.h> int add (void ); //Function prototype main() { Int ans,b,c; clrscr(); ans=add();/ /Function Call printf(“%d”,ans); getch(); }

#include <stdio.h> #include<conio.h> vod add (int ,int); //Function prototype main() { Int b,c; clrscr(); printf(“enter no”); scanf(“%d%d”,&a,&b); add(int a, int b); //Function Call getch(); }

#include <stdio.h> #include<conio.h> void add (void); //Function prototype

main() { clrscr(); add();/ /Function Call getch(); } void add(void ) // Function definition {

getch(); } int add(int x,int y ) / / Function definition { Int c; c=x+y; return c; } Output:-enter no. 2 3 5

int add(void) // Function definition { Int c,x,y; Printf(“enter no.”); Scanf(“%d%d”,&x,&y); c=x+y; return c; } Output:-enter no. 2 3 5

int add(int x,int y) // Function definition {

Int c; c=x+y; printf(“%d”,c); } Output:-enter no. 2 3 5

Int a,b,c; Printf(“enter no.”); Scanf(“%d%d”&a,&b); c=a+b; printf(“%d”,c); } Output:-enter no. 2 3 5

Passing Argument to Function :

  1. In C Programming we have different ways of parameter passing schemes such as Call by Value and Call by Reference.
  2. Function is good programming style in which we can write reusable code that can be called whenever require.
  3. Whenever we call a function then sequence of executable statements gets executed. We can pass some of the information to the function for processing called argument.

Two Ways of Passing Argument to Function in C Language :

return (0); } Output :

Number 1 : 50 Number 2 : 70 Explanation : Call by Value

  1. While Passing Parameters using call by value , xerox copy of original parameter is created and passed to the called function.
  2. Any update made inside method will not affect the original value of variable in calling function.
  3. In the above example num1 and num2 are the original values and xerox copy of these values is passed to the function and these values are copied into number1,number2 variable of sum function respectively.
  4. As their scope is limited to only function so they cannot alter the values inside main function.

B .Call by Reference/Pointer/Address : #include<stdio.h> void interchange( int *num1, int *num2) { int temp; temp = *num1; *num1 = *num2; *num2 = temp;

Summary of Call By Value and Call By Reference :

Point Call by Value Call by Reference

Copy Duplicate Copy of Original Parameter is Passed Actual Copy of Original Parameter is Passed

Modification No effect on Original Parameter after modifying parameter in function

Original Parameter gets affected if value of parameter changed inside function