Google search

Basic and advanced computer skills like Excel with macros, How to speed up your PC, C, CPP, Java programming, HTML, JavaScript, PHP, Wordpress, all web tools, Android tutorials, MySQL Tutorials, WAMP server installation. etc.

Functions and Pointers


A function is a unit or module within a program that can be called as and when necessary and as many times as we wish within a program to solve a particular task. 

When program becomes complex, it is difficult handle or debug, so the complex program can be split into small parts or modules and each module can perform different task or function. These small parts of a complex program are called functions.

Functions can be re-used as many times in the same program or can be used in other programs

 Benefits of using functions:-

  • Reduce the effort taken to program
  • Enable placing of common block of statements separately instead of repetitively within a program, thus providing re-usability
  • Help to reduce errors and enable faster debugging
  • Enable categorizing code into libraries which can be used for large projects


Types of Functions:-
1. Built-in functions: C provides a large number of built-in functions, which are defined in header files (.h files).

2. User defined function: User defined functions are those functions which are written by the user according to his/her need within a program 


/* function with arguments and with return values */
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,res;

/* function declaration */
add();
sub();
mul();
div();

clrscr();
/* input output operations */

printf("Enter two numbers:");
scanf("%d%d", &a,&b);

res=add(a,b);
printf("sum=%d\n", res);

res=sub(a,b);
printf("sub=%d\n", res);

res=mul(a,b);
printf("multiplication=%d\n", res);

res=div(a,b);
printf("division=%d\n", res);

getch();
}

/* functions calling */

int add(int x, int y)
{
int t=x+y;
return(t);
}

int sub(int x, int y)
{
int t=x-y;
return(t);
}

int mul(int x, int y)
{
int t=x*y;
return(t);
}

int div(int x, int y)
{
int t=x/y;
return(t);
}

Function to display Total marks.
#include<stdio.h>
#include<conio.h>
main()
{
int s1,s2,s3,tot;
float avg;

clrscr();

printf("Enter 3 subjects marks: ");
scanf("%d%d%d", &s1, &s2, &s3);

tot=total(s1,s2,s3);
avg=average(s1,s2,s3);

printf("Total marks : %d \n", tot);
printf("Average marks : %f\n", avg);
getch();
}

int total(int x, int y, int z)
{
int t=x+y+z;
return(t);
}
int average(int x, int y, int z)
{
float t=(float)(x+y+z)/3.0;
return(t);
}


/* function with no arguments and no return values */
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
display();
getch();
}
display()
{
printf("inside the sub-function.....");
}

/* function with argements but no return values */
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();

printf("Enter two numbers:");
scanf("%d%d", &a, &b);

large(a,b);
getch();
}

int large(int x, int y)
{
if(x>y)
{
printf("a is large");
}
else
{
printf(" b is large");
}
}

C Program to demonstrate built-in math function 

#include<conio.h>
#include <stdio.h>
#include <math.h>
main()
{
double result, x = 1.0;
clrscr();
result = sin(x);
printf("The sin() of %lf is %lf\n", x, result);
return 0;
}

C Program to demonstrate built-in string function 

#include<conio.h>
#include <stdio.h>
#include <string.h>
main()
{
char str[20];
int len;
clrscr();
printf("Enter string :");
scanf("%s", str);

printf("\n\nINPUT string : %s\n", str);
printf("lower case string : %s \n", strlwr(str));
printf("Upper case string : %s \n", strupr(str));

len=strlen(str);
printf("Length of the string : %d", len);
getch();
}

Pointers:- Pointer is a variable that holdsa memory address of another variable.
A pointer provides a way of accessing a variable value without referring to the variable directly. 

Use of Pointers:-
  1. To return more than one value from a function.
  2. To pass arrays more conveniently from one. function to another.
  3. To manipulate arrays easily by moving pointers to them.
  4. To allocate memory and access it.
Simple C Program to demonstrate pointers

#include<stdio.h>
#include<conio.h>
main()
{
int x,y;
int *ptr1, *ptr2;
clrscr();
x=2, y=3;
ptr1=&x;
ptr2=&y;
printf("\n Pointers \n");
printf("the value of x : %d\n", x);
printf("the address of x : %u\n", ptr1);
printf("the value of y : %d\n", y);
printf("the address of y : %u\n", ptr2);
getch();
}

No comments:

Post a Comment