1. Recall
• Difference between while and do while?
• Difference between == and =
• Difference between break and continue?
• What is the difference between i++ and
++i
• What if i didn’t give break in switch?
• What is the difference between logical
operators and relational operators
4. Functions
• A function is a group of statements that
together perform a task.
return_type function_name ( parameter list )
{
body of the function
}
Eg: main()
{
Printf(“hello world”);
5. Functions
int equation(int num1, int num2)
{
int result;
result = num1*num1+2*num1*num2+num2*num2;
return result;
}
Return Type
Function name
Arguments
6. #include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ;
Printf(“%d”,c);
}
7. #include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
//Function body / function Definition
8. #include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
Should I use the same variable names?
Are they same of what I used in main() ?
9. #include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
Absolutely not !!!
Variable a in equation() is different from
varaible a in main(). There is no
connection between those two
10. #include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
What is this return???
11. #include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
Return will simply send value out of
function to whomsoever it was called
12. #include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
Can i write the whole function body after
main function??
13. #include<stdio.h>
int equation(int a, int b) ;
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Yes you can. But only additional thing you
need to do is
14. #include<stdio.h>
int equation(int a, int b) ;
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
You must declare it at the beginning
18. Strings in C
• There is no string data type in C
• But we can do the same with character
array in C
– Eg: char a[8] =“baabtra”//enables us to store
string with 6 characters
/0
A
R
T
B
A
A
B
A[7]
A[6]
a[5]
a[4]
a[3]
a[2]
a[1]
a[0]
20. Structures
• A structure is a collection of variables
under a single name
• These variables can be of different types,
and each has a name which is used to
select it from the structure.
struct struct_name
{
//Variable declarations;
}
21. Why structures?
• A structure is a convenient way of
grouping several pieces of related
information together.
• Eg: suppose there are several variables
called name,age,gender related to a
student . So we can bring all of them
under one name student using structures
• Eg: struct student
{
Char name[25];
Int age;
23. Pointers
• Pointers simply points to locations in
memory
• Each variables will be having a address in
the memory. So pointer is just another
variable which simply stores the address
of it
Main()
{
int a,b, *p,*q;
1002
1001
25
99
1004
1003
1002
1001
q
p
b
a
25. Character array as pointer
Character array is a pointer to the first location of
a group of locations
Eg : char a[10];
Here a stores memory address of a[0]
main()
{
char a[10]="baabtra";
printf("%d %d",&a[0],a); // will print the
same address
A
R
T
B
A
A
B
A[6]
a[5]
a[4]
a[3]
a[2]
a[1]
a[0]
1006
1005
1004
1003
1002
1001
1000