Test 1
Test 1
4.1. Functions:
Function definition
Function call
Function declaration
Definition of functions
Function name
Function type
List of parameters
Local variable declarations
Function statements and
A return statement.
All the six elements are grouped into two parts, namely,
Function header(first three elements)
Function body(second three elements)
Syntax
function_typefunction_name(parameter list)
{
local variable declarations;
executable statement1;
executable statement2;
-----------------
return(expression);
}
where,
The parameter list declares the variables that will receive the data sent
by the calling program.
They serve as input data to the function to carry out the specified task.
Since they represent actual input values, they are often referred to as
formal parameters.
The parameters are also known a arguments.
Function Body
floatmul(float x, float y)
{
float result;
result=x*y;
return(result);
}
FunctionCalls
Example
main()
{
int y;
y = mul (10,5); /*function call */
printf(“%d/n”,y);
}
A function may or may not sent back any value to the calling function.
If I does, it is done through the return statement.
return; - The “plain” return does not return any value; it acts much as
the closing
brace of the function.
Example
If (error)
return;
Example
intmul (int x, int y)
{
int p;
P=x*y;
return(p);
}
mul(x,y)
main()
int p;
The function is executed and the value is returned and assigned to p
A function which returns a value can be used in expressions like any
other variables
Example
printf("%dn", mul(p,q));
y = mul(p,q) / (p+q);
if (mul(m,n)>total)
printf("large");
}
4.1.5. Category of functions
When a function has no arguments, it does not receive any data from
the calling function.
Similarly, when it does not return a value, the calling function does not
receive any data from the called function.
There is no data transfer between the calling function and the called
function.
Example
#include <stdio.h>
#include <conio.h>
main()
clrscr();
getch();
Output
The function with arguments will receive data from the calling function.
The actual arguments and formal arguments should match in number,
data type and order.
The values of actual arguments are assigned to the formal arguments
on a one-to-one basis, starting with the first argument.
When the function call is made, only the copy of the actual arguments
is passed into the called function.
So their values cannot be altered by the function.
When a function does not return a value, the calling function does not
receive any data from the called function.
Therefore it is a one-way data communication.
Example
#include <stdio.h>
#include <conio.h>
main()
{void print(int);
clrscr();
print(5);
getch();
}
Output
The value is 5
The function with arguments will receive data from the calling function.
The values of actual arguments are assigned to the formal arguments
on a one-to-one basis, starting with the first argument.
When the function call is made, only the copy of the actual arguments
is passed into the called function.
So their values cannot be altered by the function.
When a function returns a value, the calling function will receive a data
from the called function.
The nature of data communication between the calling function and
the called function is two-way communication.
Example
#include<stdio.h>
#include <math.h>
main(){
int square(int);
inta,b;
clrscr();
printf(“\nEnter the numbers\n”);
scanf(“%d”,&a);
b=square(a);
printf(“Square is %d”,b);
getch();
}
int square(int x) {
int c;
Output
Enter the numbers
100
Square is 10
When a function has no arguments, it does not receive any data from
the calling function.
When a function returns a value, the calling function will receive a data
from the called function.
The nature of communication between the calling function and the
called function is one-way communication.
Example
#include <stdio.h>
#include <conio.h>
main()
int s;
int add();
clrscr();
s=add();
printf(“Sum=%d\n”,s);
getch();
int add()
inta,b;
Output
Sum=5
#include <stdio.h>
int main(void)
printf("Main");
int fun()
Output:
4.1.7. Recursion
Example
#include <stdio.h>
#include <conio.h>
main()
{
int n;
Output
One-Dimensional Arrays
o The function must be called by passing only the name of the array.
o In the function definition, the formal parameter must be an array
type; the size of the array does not need to be specified.
o The function prototype must show that the argument is an array.
Example
#include <stdio.h>
#include <conio.h>
main()
{
float largest(float a[], int n);
float value[4]={2.5,-4.75,1.2,3.67};
clrscr();
printf(“The largest value is = %f\n”,largest(value,4));
getch();
}
float largest(float a[], int n)
{
int i;
float max;
max=a[0];
for(i=1;i<n;i++)
if(max<a[i])
max=a[i];
return(max);
}
Output
The largest value is = 4.000000
Two-Dimensional Arrays
#include <stdio.h>
#include <conio.h>
#define m 3
#define n 4
main()
{
void display(int a[][n]);
int a[m][n]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
clrscr();
display(a);
}
void display(int a[][n])
{
inti,j;
printf(“Contents of the array\n”);
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
printf(“\t%d”,a[i][j]);
printf(“\n”);
}
getch();
}
Output
1 2 3 4
5 6 7 8
9 10 11 12
#include <stdio.h>
#include <conio.h>
main()
{
char t[20];
intn,j;
void left(char t[],int n);
clrscr();
printf(“\n Enter the string “);
gets(t);
printf(“\nEnter the no. of characters “);
scanf(“%d”,&n);
left(t,n);
getch();
}
void left(char t[],int n)
{
int i;
printf(“%s”,t);
for(i=0;i<n,i++)
printf(“\n%c”,t[i]);
}
Output
Enter the string SOWDESWARI COLLEGE
SOWDESWARI COLLEGE
#include <stdio.h>
int main()
{ int a = 10;
int b = 20;
swap(a,b);
int temp;
Output
#include <stdio.h>
void swap(int *, int *);
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);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
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);
} }
Output
External variables
o Variables that are both alive and active throughout the entire
program are known as external variables
o They are also known as global variables
o Global variables can be accessed by any function in the program
o External variables are declared outside a function
o In case a local variables and a global variable have the same name,
the local variable will have precedence over the global one in the
function where it is declared
o Once a variable has been declared as global, any function can use it
and change its value
o Then subsequent functions can reference only that new value
o Global variable is that it is visible only from the point of declaration
to the end of the program
o Global variables are initialized to zero by default
o The main cannot access the variable that has been declared after
the main function
o This problem can be solved by declaring the variable with the
storage class “extern”
Static variables
o The value of static variables persists until the end of the program
o A variable can be declared static using the keyword “static”
o A static variable may be an internal or external type, depending on
the place of declaration
o Internal static variables
Declared inside a function
The scope extend up to end of the function in which they are
defined
Similar to auto variables, except that they remain
inexistence (alive) throughout the remainder of the program
Static variable is initialized only once, when the program is
compiled.
It is never initialized again
o External static variables
Declared outside of all functions and is available to all the
functions in that program
The difference between a static external variable and a
simple external variable is that – the static external variable
is available only within the file where it is defined while the
simple external variable can be accessed by other files
Register variables
o Register access is much faster than a memory access
o We can make a variable to be placed in one of the machine register
instead of memory using “register”
o Most compiler allow int or char variables to be placed in the register
o ANSI standard does not restrict to any particular data type
o C will automatically convert register variables into non-register
variables once the limit is reached
Example
Each character in the array occupies one byte of memory and the last
character is always “\0”. The null character will be automatically added by
the compiler.
Syntax
charstring_name[size];
Examples
char city[10];
The above statement defines the array as a five element array. We can
also declare the size munch larger than the string size in the initialization.
For example, the below statement is also permitted.
scanf(“%s”,address);
#include<stdio.h>
#include<conio.h>
main()
Output
Hello POOJA!
The “scanf()” statement with “%s” or “%ws” can read only strings
without whitespaces. That is, they cannot be used for reading a text
containing more than one word.
However, C supports a format specification known as the edit set
conversion code “%[..]” that can be used to read a line containing a
variety of characters, including whitespaces.
Example program
#include<stdio.h>
#include<conio.h>
main()
{ char name[25]
clrscr();
scanf(“%[^\n]s”,name);
Output
charch;
ch=getchar();
charstring_name[20];
gets(string_name);
Example program
#include<stdio.h>
#include<conio.h>
main()
{ charmy_name[30];
clrscr();
gets(my_name);
SARUKESH
My name is SARUKESH
When the field width is less than the length of the string, the entire
string is printed.
The integer value on the right side of the decimal point specifies the
number of characters to be printed.
When the number of characters to be printed in specified as zero,
nothing is printed.
The minus sign in the specification causes the string to be printed left-
justified.
The specification % .ns prints the first n characters of the string.
Example program
#include<stdio.h>
#include<conio.h>
main()
char name[25];
clrscr();
scanf(“%s”,name);
Output
REENA
Name is REENA
Syntax
char ch=”A”
putchar(ch);
Syntax
puts(str);