Professional Documents
Culture Documents
➢I N T R O D U C T I O NTO FUNCTIONS
➢T Y P E S O F F U N C T I O N S
➢E L E M E N T S O F U S E R D E F I N E D F U N C T I O N S
➢T Y P E S O N T H E B A S I S O F A R G U M E N T S A N D
RETURN VALUES
➢M E T H O D S O F C A L L I N G A F U N C T I O N
Introduction to Function
Modular Programming
Length of source program can be reduced
Easy to locate and isolate faulty function
Functions can be used by other program’s
Types of Functions
Function Prototype
Function Call
Function arguments and parameters
Function Definitions
Function prototype
int main()
{
int a,b;
printf("enter num \n");
scanf("%d%d",&a,&b);
sum(a,b);
getch();
return 0;
Return statement
int main()
{
Function with argument and no return
#include<stdio.h>
#include<conio.h>
void sum(int x,int y)
{
int main()
{
int a,b;
printf("enter num \n");
scanf("%d%d",&a,&b);
sum(a,b);
getch();
return 0;
}
Function with no argument and return
#include<stdio.h>
#include<conio.h>
int sum()
{
int a,b;
printf("enter num \n");
scanf("%d%d",&a,&b);
return a+b;
int main()
{
int x;
x=sum();
printf("value %d",x);
getch();
return 0;
}
Function with argument and return
#include<stdio.h>
#include<conio.h>
int sum(int a, int b)
{
return a+b;
int main()
{
int x,y,z;
printf("enter num \n");
scanf("%d%d",&x,&y);
z=sum(x,y);
printf("value %d",z);
getch();
return 0;
Methods of calling function
Call by value
Call by reference
Call by value
#include<stdio.h>
#include<conio.h>
void swap(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}
void main()
{
int a,b;
printf("enter num \n");
scanf("%d%d",&a,&b);
printf(" num befor call a=%d and b=%d\n",a,b);
swap(a,b);
printf(" num after call a=%d and b=%d \n",a,b);
getch();
}
Call by Reference
#include<stdio.h>
#include<conio.h>
void swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
void main()
{
int a,b;
printf("enter num \n");
scanf("%d%d",&a,&b);
printf(" num befor call a=%d and b=%d\n",a,b);
swap(&a,&b);
printf(" num after call a=%d and b=%d \n",a,b);
getch();
}
Recursion, Debugging
SauleAnay
Fall 2016, NU, CSCI151
You should know already
• Functions
• Function parameters and arguments
• How to call function
• Global vs local variables
Recursion
• is a method where the solution to a problem
depends on solutions to smaller instances of
the same problem.
example: 2^11 = 2^10 * 2 = 1024 * 2
• one of the central ideas of computer science.
How do you empty a vasecontaining five
flowers?
recursion iteration
Examples of recursive solution
1.Factorial:
Base case:0! = 1
Recursive case: for all n > 0, n! = n(n −1)!
2.fibbonachi:
Base case:Fn(1) = 1, Fn(2) = 1
Recursive case: for all n > 2, Fn(n) =Fn(n-
1)+Fn(n-2)
3.X^n (x to the powern)
Base case: if n=0,x^n=1
Recursive case: for all n>0,x^n=x*x^(n-1)
Example with
Factorial
Factorial recursive
1.
2. function
#include <stdio.h>
#include<conio.h>
3. int factorial(int n)
4. {
5. //base case
6. if(n==0)
7. return 1;
8. //recursive case
9. if (n>0)
10. return n*factorial(n-1);
11. //default case if n<0
Stack is. used for static memory allocation and Heap for dynamic memory allocation, both
stored in the computer's RAM .
Variables allocated on the stack are stored directly to the memory and access to this
memory is very fast, and it's allocation is dealt with when the program is compiled. When
a function or a method calls another function which in turns calls another function etc.,
the execution of all those functions remains suspended until the very last function
returns its value. The stack is always reserved in a LIFO order, the most recently reserved
block is always the next block to be freed. This makes it really simple to keep track of the
stack, freeing a block from the stack is nothing more than adjusting one pointer.
Variables allocated on the heap have their memory allocated at run time and accessing
this memory is a bit slower, but the heap size is only limited by the size of virtual memory
. Element of the heap have no dependencies with each other and can always be accessed
randomly at any time. You can allocate a block at any time and free it at any time. This
makes it much more complex to keep track of which parts of the heap are allocated or
free at any given time.
Representation of
stack and heap
Type of Memory Allocation
There are two type of memoryallocation.
1) Static memory allocation.
2) Dynamic memory allocation.
Different Between Static and dynamic memoryallocation.
• Static memory allocation is allocated the memoryat
the compile time. Dynamic at the runtime of
execution of program.
• In static memory can’t be increase whileexecution of
the program. But in the dynamic memory can
increase while executing the program.
• Static used in array and dynamic used in linkedlist.
Memory space required caman be specified at the
timeof execution.
.
malloc()
The declaration of malloc ( ) functionis
Syntax:-
Void*malloc(size_t size)
Example:- int *nums =(int*)malloc(5*sizeof(int));
This is similar to int *nums=newint[5];
➢Deallocation of using free[] function-
free(nums);
➢This is special function that are used to assign valueto
variable.
➢ New return exact datatype, while malloc() return void
pointer.
➢To use malloc() you must#include<stdlib.h>
malloc() …continue….
charpt
charpt = malloc(10);
10 bytes or chars
charpt
ptr =(int*)malloc(5*sizeof(int));
/ / allocate memory for 6 more integers i.e a total of 11.
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
int main()
{
int i, num;
float *data;
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &num);
// Allocates the memory for 'num' elements.
data = (float*) calloc(num, sizeof(float));
if(data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
printf("\n");
// Stores the number entered by the user.
for(i = 0; i < num; ++i)
{
printf("Enter Number %d: ", i + 1);
scanf("%f", data + i);
}
// Loop to store largest number at address data
for(i = 1; i < num; ++i)
{
// Change < to > if you want to find the smallest number
if(*data < *(data + i))
*data = *(data + i);
}
printf("Largest element = %.2f", *data);
getch();
return 0;
}
task