0% found this document useful (0 votes)
30 views5 pages

Programming - Languages - Lecture-17 & 18 - Fall-2022

The document discusses functions in programming languages. It provides an example of a function declaration, definition, and call to add two integers and return the sum. It explains the components of a function definition. It also provides an exercise to write a program that calculates the surface area and volume of a sphere using separate functions.

Uploaded by

Ahmed Bilwani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Programming - Languages - Lecture-17 & 18 - Fall-2022

The document discusses functions in programming languages. It provides an example of a function declaration, definition, and call to add two integers and return the sum. It explains the components of a function definition. It also provides an exercise to write a program that calculates the surface area and volume of a sphere using separate functions.

Uploaded by

Ahmed Bilwani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

12/23/2022

Lecture-17&18
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-5

Functions

1
12/23/2022

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);
}

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;
}

2
12/23/2022

Types declaration of functions


how they will work ??
// definition
// examples of Declarations abc()
abc(); {……
……
mno(int, int, float); }
float rst( );
// definition
float xyz ( int, int, float); mno(int a, int b , float c)
{ …..
…..
}
// definition
// definition

Exercise
• Write a program which takes radius of a
sphere as input and prints its surface area and
volume using functions
• Make separate functions for area and volume.

3
12/23/2022

// add header files here


float cent(float); 9
float faren(float); Write a program which takes f  c   32
void main(void)
{
temperature and its units input and 5
float temp, c, f, k; prints it in centigrade, Fahrenheit 5
char ch;
clrscr(); and Kelvin c   f  32
printf(“enter temperature input”; 9
scanf(“%f”,&temp);
printf(“\n enter its unit as input, c for centigrade, f for farenheit and k for kelvin”); k  c  273
ch=getche();
if ( ch==‘c’)
{ f=faren(temp);
k=temp+273;
c=temp;
}
else if (ch==‘f’)
float cent (float x)
{ c=cent (temp); {
k=c+273; float y;
f=temp;
}
y=(x-32)*(5/9);
else if (ch==‘k’) return(y);
{ c=temp-273; }
f=faren(c);
k=temp;
float faren (float x)
else {
printf(“invalid temperature unit”);
exit();
float y;
} y=(c*9/5)+32;
printf(“\n the temperature in centigrade is %f c”, c); return(y);
printf(“\n the temperature in Fahrenheit is %f f”, f);
printf(“\n the temperature in kelvin is %f k”, k);
}
getch();
} 7

Exercise
Write a program which takes temperature and its
units input and prints it in centigrade, Fahrenheit
and Kelvin (use switch case with functions)

9
f  c   32
5
5
c   f  32
9
k  c  273

4
12/23/2022

Exercise
• Write a program to evaluate sin(x) series upto
6 terms, for a value of x
x3 x5 x7
sin( x)  x     ...
3! 5! 7!

THE END

10

You might also like