[go: up one dir, main page]

0% found this document useful (0 votes)
13 views19 pages

functions in C.pptx

The document explains the concept of functions in C programming, detailing their definition, declaration, and calling methods. It covers the structure of a function, including return type, function name, parameters, and body, as well as the difference between call by value and call by reference. Additionally, it demonstrates examples of function usage, including functions that return values, handle arrays, and utilize pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views19 pages

functions in C.pptx

The document explains the concept of functions in C programming, detailing their definition, declaration, and calling methods. It covers the structure of a function, including return type, function name, parameters, and body, as well as the difference between call by value and call by reference. Additionally, it demonstrates examples of function usage, including functions that return values, handle arrays, and utilize pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

FUNCTIONS

A function is a block of code that has a name


and it has a property that it is reusable i.e. it
can be executed from as many different
points in a C Program as required.

A function is a group of statements that


together perform a task. Every C program has
at least one function, which is main(), and all
the most trivial programs can define
additional functions.
Defining a Function:
The general form of a function definition in C programming
language is as follows:
return_type function_name( parameter list )
{ body of the function }
A function definition in C programming language consists of a
function header and a function body. Here are all the parts of a
function:
Return Type: A function may return a value. The return_type is
the data type of the value the function returns. Some functions
perform the desired operations without returning a value. In this
case, the return_type is the keyword void.
Function Name: This is the actual name of the function. The
function name and the parameter list together constitute the
function signature.
Parameters: The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional;
that is, a function may contain no parameters.
Function Body: The function body contains a collection of
statements that define what the function does.
#include <stdio.h>
/* function declaration/prototype */
int max(int num1, int num2);
int main () {
int a = 100; int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* function definition:
returning the max between two numbers */
int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else result = num2;
return result; }
OUTPUT:

Max value is : 200


Function Declarations/Prototype:
A function declaration tells the compiler about a
function name and how to call the function. The
actual body of the function can be defined
separately.
A function declaration has the following parts:
return_type function_name( parameter list );
For the above defined function max(), following is
the function declaration:
int max(int num1, int num2);
Parameter names are not important in function
declaration only their type is required, so
following is also valid declaration:
int max(int, int);
Calling a Function:

Function_name(parameter list);

When a program calls a function, program control


is transferred to the called function. A called
function performs defined task and when its
return statement is executed or when its
function-ending closing brace is reached, it
returns program control back to the main
program.
To call a function, you simply need to pass the
required parameters along with function name,
and if function returns a value, then you can
store returned value.
#include<stdio.h>
void add();//function prototype
int main(){
add();
return 0;
Function add() with void return type
} and empty parameter
void add(){
int a,b,c;
printf("\nEnter the values");
scanf("%d %d", &a, &b);
c=a+b;
printf("\nThe sum of two numbers is %d", c);
}
#include<stdio.h>
void add(int,int);//function prototype
int main(){
int a,b;
printf("\nEnter the values"); Function add() with void return type
scanf("%d %d", &a, &b); and two parameters
add(a,b);
return 0;
}
void add(int a, int b){
int c;
c=a+b;
printf("\nThe sum of two numbers is %d", c);
}
#include<stdio.h>
#include<conio.h>
int add(int,int);//function prototype
int main(){
int a,b;
Function add() with int return type
printf("\nEnter the values");
and two parameters
scanf("%d %d", &a, &b);
printf("\nThe sum of two numbers is %d",add(a,b));
return 0;
}
int add(int a, int b){
int c;
c=a+b;
return c;
}
Function that computes average of 3 nos.

#include<stdio.h>
float add(int,int,int);//function prototype
int main(){
int a,b,c;
printf("\nEnter the values");
scanf("%d%d%d", &a, &b,&c);
printf("\nThe average of three numbers is %f",add(a,b,c));
return 0;
}
float add(int a, int b, int c){

return (a+b+c)/3.0;
}
Actual parameters are parameters as they appear in function calls.
Formal parameters are parameters as they appear in function
definitions.

While calling a function, there are two ways that arguments can be
passed to a function:
Call by value: This method copies the actual value of an argument
into the formal parameter of the function. In this case, changes
made to the formal parameter inside the function have no effect on
the argument.
Call by reference: In this method, the called function accesses and
works with the original values using their references. Any changes
that occur take place in the original values and are reflected to the
calling function.
Call by value:
# include <stdio.h>
void change(int, int);
int main ()
{
int a,b;
a=90;b=89; a=900 b=9000
change(a,b); The values are
printf("\nThe values are \n"); a=90 b=89
printf("a=%d b=%d",a,b);
return 0;
}
void change(int a, int b){
a=900;
b=9000;
printf("a=%d b=%d\n",a,b);
}
Call by reference: (reference
variable)
# include <stdio.h>
void change(int&, int&);
int main ()
{
int a,b;
a=90;b=89; a=900 b=9000
change(a,b); The values are
printf("\nThe values are \n"); a=900 b=9000
printf("a=%d b=%d",a,b);
return 0;
}
void change(int &a, int &b){
a=900;
b=9000;
printf("a=%d b=%d\n",a,b);
}
Call by reference: (pointer
variable)
# include <stdio.h>
void change(int*, int*);
int main ()
{
int a,b;
a=90;b=89; 900 9000
change(&a,&b); The values are
printf("\nThe values are \n"); a=900 b=9000
printf("a=%d b=%d",a,b);
return 0;
}
void change(int *a, int *b){
*a=900;
*b=9000;
printf(“%d %d\n",*a,*b);
}
A reference variable is an alias, that is, another name for an
already existing variable. Once a reference is initialized with a
variable, either the variable name or the reference name may be
used to refer to the variable.

# include <stdio.h>

int main () x=99


{ a=99
clrscr();
int a=90;
int &x=a; //x is a reference variable for ‘a’
a=a+9;
printf("x=%d \n", x);
printf("a=%d ", a);
}
Function returning a pointer
value:
# include <stdio.h>
int* bigger(int, int);
int main ()
{ Enter two numbers:
int a,b,*c;
56
printf("Enter two numbers:\n");
scanf("%d %d", &a,&b);
90
printf("The nos. are "); The nos. are a=56 b=90
printf("a=%d b=%d",a,b); The bigger number is 90
c=bigger(a,b);
printf("\nThe bigger number is %d", *c);
return 0;
}
int* bigger(int x, int y){
if(x>y)
return &x;
else
return &y;
}
A pointer is a variable which contains the
address in memory of another variable.
# include <stdio.h>
int main ()
{
int a=90;
int *x; //x is a pointer variable x=90
x=&a; a=90
printf("x=%d \n", *x);
printf("a=%d ", *(&a));
return;
}
Array as parameters to
function:
#include<stdio.h>
void multiply(int list[],int n);
int main(){ Enter the number of terms of the
int list[100];
int i,n;
list
printf("Enter the number of terms of the 5
list\n); Enter the list
scanf("%d",&n); 5
printf("Enter the list\n")
for(i=0;i<n;i++) 6
scanf("%d",&list[i]); 7
multiply(list,n); 8
printf("The modified list is\n");
for(i=0;i<n;i++)
9
printf("%d\n",list[i]); The modified list is
return 0; 50
} 60
void multiply(int list[],int n)
{ 70
int j; 80
for(j=0;j<n;j++) 90
list[j]=list[j]*10;
}

You might also like