Subject Name: Computer Programming Fundamentals Unit No: 04
UNIT: 4
User-defined Functions
❖ Introduction to Functions:
Definition of Function: Function is a group of statement to perform a specific task.
Types of functions:
1. Library function
2. User-defined Function
❖ Advantages of using Functions:
1. By using functions, we can avoid rewriting same logic/code again and again in a program.
2. We can call C functions any number of times in a program and from any place in a program.
3. We can track a large C program easily when it is divided into multiple functions.
❖ Types of Functions: Built-in and user defined Functions
Library function(Built-in):
• The function which is develop by system itself is known as Library function .
• Example: printf(),scanf(),sqrt(),ceil() etc …
• Library function is all ready exist in system.
User-defined Function:
• The function which is develop by user itself is known as User-defined function.
• Example: main()
• User-defined function is created by the user at the time of writing of program.
Difference between Library function and User-defined Function.
No. Library function User-defined Function
The function which is develop by The function which is develop by
1 system itself is known as Library user itself is known as User-defined
function. function.
Example: printf(),scanf(),sqrt() etc … Example: main(),add()
2.
There no elements in Library function. User-defined function has three
3.
elements.
Prepared By: Department of Computer Engineering Page 1
Subject Name: Computer Programming Fundamentals Unit No: 04
❖ Declaring, Defining and calling user defined Functions
(1) Function declaration
Syntax:
ReturnType FunctionName (Parameter List);
• Function Type is a return type or datatype of a function.
• Function name is the name of the user defined function.
Example:
int sum (int, int);
int sum( int a, int b);
(2) Function call
Syntax:
Identifier = Function name(Argument value);
Where identifier is the name of variable
Example:
a = sum(10,5);
(3) Function definition
Syntax:
ReturnType Function Name(Parameter List)
{
Local variable declaration
Function statement
Return Statement
}
Example:
int sum(int x, int y)
{
int total;
total = x + y;
return total;
}
❖ Categories of user-defined Functions
A function may or may not accept any argument. It may or may not return any value. Based on these facts,
There are four different categories of function calls.
• function without arguments and without return value
• function without arguments and with return value
• function with arguments and without return value
Prepared By: Department of Computer Engineering Page 2
Subject Name: Computer Programming Fundamentals Unit No: 04
• function with arguments and with return value
Example for Function without argument and return value
#include<stdio.h>
void sum();
void main()
{
printf("\n sum of two numbers:");
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
Example for Function without argument and with return value
#include<stdio.h>
int sum();
void main()
{
int result;
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Prepared By: Department of Computer Engineering Page 3
Subject Name: Computer Programming Fundamentals Unit No: 04
Example for Function with argument and without return value
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
Example for Function with argument and with return value
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}
❖ Call by Value and call by Reference
Call By Value with example:
• When a function is called using the value of variables, then it is known as call by value.
Prepared By: Department of Computer Engineering Page 4
Subject Name: Computer Programming Fundamentals Unit No: 04
• The value of variables, which are to be passed, will be copied from the variables of the calling functions
to the variables of the called functions.
• All the process done on the duplicate variables rather then actual variables.
Example:
#include <stdio.h>
void swap(int , int); // function prototype
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b); // function call
printf("After swapping values in main a = %d, b = %d\n",a,b); //actual parameters a=10, b=20
}
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a=20, b=10
}
Call By Reference with example:
• When a function is called using the address of variables, then it is known as call by reference.
• Instead of passing the value of variables from calling function to the called function, addresses of the
variables are passed.
• All the process done on the actual variables.
Example:
#include <stdio.h>
void swap(int *, int *); // function prototype
void main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
Prepared By: Department of Computer Engineering Page 5
Subject Name: Computer Programming Fundamentals Unit No: 04
swap(&a,&b); // function call
printf("After swapping values in main a = %d, b = %d\n",a,b); // actual parameters a = 20, b = 10
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters a=20, b=10
}
❖ Recursion
• Function call itself is called Recursion.
• Recursive call to a function may be conditional or unconditional.
• In conditional recursive call, function is terminated when condition is false.
• In unconditional recursive, function is called infinitely.
• It is used to find the factorial of a given number.
• Example :
void main()
{
printf(“Recursion”);
main();
}
• Example :
void main()
{
function1();
}
function1()
{
function1();
Prepared By: Department of Computer Engineering Page 6
Subject Name: Computer Programming Fundamentals Unit No: 04
Advantages of Recursion:
• The recursion is very flexible in data structure like stacks.
• Using recursion, the length of the program can be reduced.
Disadvantages of Recursion:
• It requires extra storage space.
• The recursion function is not efficient in execution speed and time.
Proper termination is required; otherwise it leads to infinite loop.
Scope, Visibility and Lifetime of a Variable in C
• Scope determines the region in a C program where a variable is available to use.
• Visibility of a variable is related to the accessibility of a variable in a particular scope of the program
and Lifetime of a variable is for how much time a variable remains in the system’s memory.
Block Scope:
• Variables declared inside a block {} have block scope.
• These variables can only be accessed within the block they are declared in.
• For example, variables declared within a function or inside a loop have block scope
void function()
{
int a = 5; // Block scope within function
{
int b = 10; // Block scope within nested block
}
// b is not accessible here
}
Function Scope:
• Labels (used with goto statements) have function scope.
• They can be used anywhere in the function where they are declared.
File Scope:
• Variables declared outside of any function have file scope.
• They can be accessed from the point of declaration to the end of the file.
• These are also known as global variables.
Prepared By: Department of Computer Engineering Page 7
Subject Name: Computer Programming Fundamentals Unit No: 04
Visibility
1. Global Variables:
o Declared outside of functions.
o Visible throughout the entire file and accessible from any function within that file.
o Use the extern keyword to access them from other files.
2. Static Variables:
o Declared with the static keyword.
o If declared outside a function, they are visible only within that file (internal linkage).
o If declared inside a function, they retain their value between function calls but are not visible
outside the function.
3. Local Variables:
o Declared within a function or a block.
o Visible only within that function or block.
o Not accessible outside the function or block.
Lifetime
1. Automatic (Local) Variables:
o Declared inside a function or block.
o Created when the block or function is entered and destroyed upon exit.
o They have automatic storage duration.
2. Static Variables:
o Have a lifetime from the program's start to its end.
o Retain their value between function calls (for static local variables)
3. Global Variables:
o Declared outside of any function.
o Have a lifetime from program start to program termination.
o Exist for the entire duration of the program.
Prepared By: Department of Computer Engineering Page 8