You are on page 1of 7

12/19/2022

Lecture-15&16
Corse Title: Programming Language
Course Code: EL-255
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2022
Offered to: S.E. (Electronic Engineering)

Department of Electronic Engineering


NED University of Engineering and Technology Karachi, Pakistan

Chapter-4

Conditions

1
12/19/2022

Switch Case
How switch case works
main()
{
int id ;
printf(“enter course id of a students from 1 – 3”);
scanf(“%d”,&id);
switch(id)
{
case 1:
printf("c programming language");
break;
case 2:
printf("c++ programming language");
break;
case 3:
printf("web technology");
break;
default :
printf("no student found");
break;
}
} // end of main 3

main()
{
char ch;
program: take int ca=0,ce=0,ci=0,co=0,cu=0; // initialize all counters
some text as printf(“ type some text and press enter”);
input and do
{
counts each ch=getche();
vowel switch(ch)
separately and {
case ‘a’: ca++; break;
prints their case ‘e’: ce++; break;
count. ……….
……….
case ‘u’: cu++; break;
default :
} // end of switch
} // end of do-while
while (ch !=‘\r’);
printf(“ \n number of a=%d, \t e=%d, …., \t u=%d”, ca, ce, ci,co,cu);
getch();
} // end of main
4

2
12/19/2022

Practice program
Write a program to make a simple calculator to
perform following basic functions
• Addition
• Subtraction
• Multiplication
• division

Chapter-5

Functions

3
12/19/2022

Functions in C
• A function in is a block of code that performs a
specific task. It has a name and it is reusable.

Types of functions
1. Built-in functions/ Library Functions/ Pre-Defined functions.
2. User defined functions.

User-Defined Functions
• we write our own functions to perform
specific tasks.
• For this, we break code into discrete sections
(functions) that each perform a single,
understandable task.
• we can call our function throughout our
program in the same way that we call C library
functions.

4
12/19/2022

Steps to implement a function


1. Function Declaration
2. Function Definition
3. Function Call

Function Declaration / Prototype


• Prototype declaration is necessary in order to provide
information to the compiler about function, about
return type, parameter list, function name etc.
• it is always done above main program
• 1. Function Declaration is also called as Function
Prototype.
• 2. It Provide following information to the Compiler:

i. Name of the Function


ii. Return Type [Optional]
iii. Argument List / Parameter List

10

5
12/19/2022

Syntax: Prototype Declaration


return_type function_name ( type arg1, type arg2...... );
Examples Prototype Declaration
int sum (int, int);
int square (int);
void display (void); display ();
int getvalue (void);

11

Example
#include <stdio.h>
int add(int,int); Function Declaration
main()
{
int a=4,b=6,c; Function Call
c = add(a,b);
printf(“value of c = %d”,c);
}
int add( int x, int y)
{ Function Definition
int sum;
sum=x+y;
return(sum);
}

12

6
12/19/2022

Function Definition:
• Function definition contains the actual code of
execution task. In function definition we write
code whatever we want to perform by using
function.
return-type function-name (parameters)
{
declarations
statements
return value;
}

13

THE END

14

You might also like