functions in C.pptx
functions in C.pptx
Function_name(parameter list);
#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>