7. Add two numbers
main()
{
int a,b;
int c;
printf(“Enter the value of a”);
scanf(“%d”,&a);
printf(“Enter the value of b”);
scanf(“%d”,&b);
c=a+b;
printf(“Answer is:%d”,c);
}
8. Add two numbers using functions
main()
{ add(x,y)
int a,b; {
int c; z=x+y;
printf(“Enter the value of a”); printf(“%d”,z);
scanf(“%d”,&a); }
printf(“Enter the value of b”);
scanf(“%d”,&b);
add(a,b);
}
9. Arguments/Parameters
• Arguments are used mostly in functions.
• it can be any input parameter which a
function can use to do it's work.
• Example: sin(x) sin is a function
x is it's argument.
11. Add two numbers using functions
main() formal arguments
{ add(x,y)
int a,b; {
int c; z=x+y;
printf(“Enter the value of a”); printf(“%d”,z);
scanf(“%d”,&a); }
printf(“Enter the value of b”);
scanf(“%d”,&b);
add(a,b); //function call
} actual arguments
16. Types of functions
1. No arguments and no return type
2. No arguments and return type
3. With arguments and no return type
4. With arguments and return type
17. Passing arguments
• The arguments passed to function can be of two
types.
1. Values passed – Call by value
2. Address passed – Call by reference
18. Call by value
• main()
{
int x=50, y=70;
add(x,y);
}
add(int x1,int y1)
{
int z1;
z1=x1+y1;
printf(“%d”,z1);
}
19. Call by reference
• main()
{
int x=50, y=70;
add(&x,&y);
}
add(int *x1,int *y1)
{
int z1;
z1=x1+y1;
printf(“%d”,z1);
}
20. Call by reference
• Address is passed using symbol ‘&’
value is accessed using symbol ‘*’
x=50 &x=2000 *x=50
y=70 &y=2008 *y=70