2. // Addition of two integers using
//Function
#include<stdio.h>
int addnumbers(int x, int y);
int main()
{
int num1, num2, add_result;
printf("Enter first numbern");
scanf("%d", &num1);
printf("Enter second numbern");
scanf("%d", &num2);
add_result = addnumbers(num1,
num2);
printf("Addition result is
%dn",add_result);
getchar(); return 0;
}
int addnumbers(int x, int y)
{
int z;
z = x +y;
return z;
}
Dr. Yousaf, PIEAS
3. // Addition of two integers using
//Function
#include<stdio.h>
int addnumbers(int x, int y);
int main()
{
int num1, num2, add_result;
printf("Enter first numbern");
scanf("%d", &num1);
printf("Enter second numbern");
scanf("%d", &num2);
add_result = addnumbers(num1,
num2);
printf("Addition result is
%dn",add_result);
printf(“%d”, z) // Error
getchar(); return 0; }
int addnumbers(int x, int y)
{
int z;
z = x +y;
printf(“%d”, num1) // Error
return z;
}
Dr. Yousaf, PIEAS
Any variable declared in a function is
not visible inside main and vice versa.
4. Dr. Yousaf, PIEAS
Example of Function
// To determine the even or odd using
// a function
#include<stdio.h>
void EvenOrOdd(int num);
int main()
{
int num;
printf("Enter a number to know
even or oddn");
printf("Enter -100 to exitn");
while (1)
{
scanf("%d", &num);
if (num == -100)
break;
EvenOrOdd(num);
}
printf("OK");
getchar(); return 0; }
void EvenOrOdd(int num)
{
if(num%2==0)
printf("Evenn");
else
printf("Oddn");
}
5. Dr. Yousaf, PIEAS
Example of Function
#include<stdio.h>
void printLine(void)
{
printf("n*****************n");
}
void main ()
{
int num;
printLine();
printf("Enter a Number :");
scanf("%d",&num);
if(num%2==0)
printf("Even Number");
else
printf("Odd Number");
printLine();
getchar();
}
Output
*****************
Enter a Number :13
Odd Number
*****************
6. Returning a value using if else statement
A single value is returned, however, multiple
return statements can be used within a single
function using “if-then-else” statement.
Dr. Yousaf, PIEAS
7. Dr. Yousaf, PIEAS
// Returning a value and if else
#include<stdio.h>
int myfunc(int x, int y);
int main()
{
int num1, num2, smaller;
printf("Enter first numbern");
scanf("%d", &num1);
printf("Enter second numbern");
scanf("%d", &num2);
smaller = myfunc(num1, num2);
printf("%d is smallern",smaller);
getchar();
getchar();
return 0;
}
int myfunc(int x, int y)
{
if(x < y )
return x;
else
return y;
}
8. Home/Lab Assignments
Write a function that returns the greater of the two numbers i.e.
make a function that takes two integer arguments and returns the
greater of the two. Write program to use this function.
(Try Yourself)
Write a function should accept two arguments i.e. length and
width of the rectangle and should return the area of the
rectangle, the prototype of the function is as under:
float areaRect(float, float);
(Try Yourself)
Develop a function that accepts two values as length and width of
a shape and returns 0 or 1. A 1 will be returned if the shape is a
square otherwise a 0 will be returned.
(Try Yourself).
Dr. Yousaf, PIEAS
9. Home/Lab Assignments
A normal dice has 6 sides numbered from 1 to 6, when the dice is
rolled the number on top side of the dice is considered to be the
result of rolling the dice. Dices are used in many games etc. Your
task is to write a function int rollDice(), the function should return
an integer value ranging from 1 to 6, to make function return a
random number each time use the library function rand() and
srand() (. Write a program that allow the user to call this function
multiple times , display the value returned by the function.
(Try Yourself)
Dr. Yousaf, PIEAS
10. After studying the functions,
once again we discuss the
Anatomy of a C-Program
Dr. Yousaf, PIEAS
12. #include <stdio.h>
int main( )
{
int x, y, z;
x = 5;
y = 7;
z = x + y;
printf(“%d", z);
getchar();
return 0;
}
//Anatomy of a C Program
Lines that begin with a # in column 1 are
called preprocessor directives (commands).
• When we write our programs, including
libraries of functions help us that we do not
have to write the same code over and over.
• Example: the #include <stdio.h> directive
causes the preprocessor to include a copy
of the standard input/output header file
stdio.h at this point in the code.
• Some of the functions are very complex
and long. Not having to write them
ourselves make it easier and faster to write
programs.
Dr. Yousaf, PIEAS
13. Preprocessor Directives
#include <stdio.h>
#include <math.h>
#include <string.h>
• Lines that begin with a # in column 1 are called
preprocessor directives (commands).
• To access the functions which are stored in the library, it is
necessary to tell the compiler, about the file to be
accessed.
• These files contain information about some library
functions used in the program.
Dr. Yousaf, PIEAS
14. Preprocessor Directives
• # include Directive provides instructions to the
compiler to link (link section) function from the
system library.
• The #include directives “paste” the contents of
the files, e.g., stdio.h, math.h and string.h
into your source code, at the very place where the
directives appear.
• Syntax:-
#include<stdio.h>
stdio.h is header file.
Dr. Yousaf, PIEAS
15. Preprocessor Directives
• stdio stands for “standard I/O”, stdlib stands for
“standard library”, and string.h includes useful string
manipulation functions.
• #include<stdio.h> header file is included because it
contains information about the printf ( ) function that is
used in this program.
• This header file was included because it contains
information about the printf ( ) function that is used in
this program.
• printf function saves you from the complexity of writing
your own function of how to display text on the
computer screen.
• Hence you are more productive with the actual program
rather than worrying about such issues.
Dr. Yousaf, PIEAS
16. The printf() function
printf(“The values of x = %dn", x);
• printf() is a library function declared in
<stdio.h>
• Syntax: printf( FormatString, Expr,
Expr...)
– FormatString: String of text to print
– Exprs: Values to print
– FormatString has placeholders to show
where to put the values (note: #placeholders
should match #Exprs)
Dr. Yousaf, PIEAS
17. The printf() function
– Placeholders:
%d (print as integer),
%f (print as floating-point)
%s (print as string)
%c (print as char)
– n indicates a newline character
Make sure you pick
the right one!
Dr. Yousaf, PIEAS
18. #include <stdio.h>
int main( )
{
int x, y, z;
x = 5;
y = 7;
z = x + y;
printf(“%d", z);
getchar();
return 0;
}
//Anatomy of a C Program
Lines that begin with a # in column 1 are
called preprocessor directives (commands).
Main function
int main ( )
{
statement(s)
return 0;
}
Dr. Yousaf, PIEAS
19. The main() function
• Every ‘C’ program must have one main() function
section.
• main() is always the first function called in a
program execution.
• Every program must have a function called main.
This is where program execution begins.
Dr. Yousaf, PIEAS
20. The main() function
int main(void)
{ …
}
• The parentheses following the reserved word “main”
indicate that it is a function.
• void indicates that the function takes no arguments
• The reserved word “int” indicates that main() returns
an integer value.
Dr. Yousaf, PIEAS
21. Main() function section
“main” function basically serves as
the entry point of the core
program.
It contains two parts
1) Declaration part:
It declares all variables
used in the executable
part.
2) Executable part:
It has atleast one
statement.
#include <stdio.h>
#include "stdafx.h“
int main( )
{
int x, y, z;
x = 5;
y = 7;
z = x + y;
printf(“%d", z);
getchar();
return 0;
}
22. The Function Body
• A left brace { -- begins the body of
every function. A corresponding right
brace -- } -- ends the function body.
• The style is to place these braces on
separate lines in column 1 and to
indent the entire function body 3 to 5
spaces.
Dr. Yousaf, PIEAS
#include <stdio.h>
#include "stdafx.h“
int main( )
{
int x, y, z;
x = 5;
y = 7;
z = x + y;
printf(“%d", z);
getchar();
return 0;
}
23. return 0 ;
• Because function main() returns an integer value,
there must be a statement that indicates what this
value is.
• The statement
return 0 ;
indicates that main() returns a value of zero to the
operating system.
• A value of 0 indicates that the program execution
terminated successfully.
Dr. Yousaf, PIEAS
24. Hello World
#include <stdio.h>
/* My C program which prints Hello World */
int main ()
{
printf ("Hello World!n");
return 0;
}
Preprocessor directive
Library command
main() means “start here”
Comments are good
Return 0 from main means our program
finished without errorsBrackets
define code blocks
Dr. Yousaf, PIEAS
25. Comment Delimiters
• We can use // for a single line comment.
• We can use /* */ for single line as well as multiline
comments.
• These are called comment delimiters
Dr. Yousaf, PIEAS