0% found this document useful (0 votes)
20 views23 pages

CSC 183 Chap-10

The document provides an overview of functions in C programming, detailing their syntax, types, and how to define and invoke them. It explains the concepts of parameters, return values, local and global variables, and the importance of functions for code reusability and readability. Additionally, it covers examples of function prototypes, definitions, and the use of recursion.

Uploaded by

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

CSC 183 Chap-10

The document provides an overview of functions in C programming, detailing their syntax, types, and how to define and invoke them. It explains the concepts of parameters, return values, local and global variables, and the importance of functions for code reusability and readability. Additionally, it covers examples of function prototypes, definitions, and the use of recursion.

Uploaded by

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

Functions

Example
 int max(int a, int b); int max(int a, int b){
return a>b?a:b;
void main(){ }
int x;
x = max(5,8);
x = max(x,7);
}
 Void main()
 {
 Int a, b=8,c=4;//global variable
 c=a+b;
 Printf(“%d”,c)

//user defined function


int max(int a, int b)//local variable
{
return a>b?a:b;
}

 }
Void main()
{
char dept[5] = “CSE”; //global variable to declare a string
//functions
int csc183_A() {}
int csc183_B() {}
int csc183_C() {}
int csc183_D() {}

int csc183_E(int numofstudent) {


Numofstudent = 30;
}

int csc183_F(int numofstd)


{
numofstd=17;//local
}
}
int run() int run(int a, int b)
{ {
printf(“Today the weather is return(a+b);
very unpleasant”); }
return 0;
}
 A function receives zero or more parameters, performs a
specific task, and returns zero or one value.
 A function is invoked by its name and parameters.
– No two functions have the same name in your C program.
– The communication between the function and invoker is
through the parameters and the return value.
 A function is independent:
– It is “completely” self-contained.
– It can be called at any places of your code and can be ported
to another program.
 Functions make programs reusable and readable.
Void main()
{
int a,b;//access
int func1(int a, int b){printf(a)}
int func2(){……….}
}
Syntax
 Function Prototype:
return_type function_name (type1 name1, type2 name2,
..., typen namen);

 Function Definition:
return_type function_name (type1 name1, type2 name2,
...,typen namen)
{
....statements...
}

The parameters
Some Examples
 Function Prototype Examples
double squared (double number);
void print_report (int);
int get _menu_choice (void);
 Function Definition Examples void parameter list means
double squared (double number) it takes no parameters
{
return (number * number);
}

void print_report (int report_number)


{
if (report_nmber == 1)
printf(“Printer Report 1”);
else
printf(“Not printing Report 1”); return type void means
} it returns nothing
#include <stdio.h>

int addNumbers(int a, int b);


Example
// function prototype

int main()
{
int n1,n2,sum;

printf("Enters two numbers: ");


scanf("%d %d",&n1,&n2);

sum = addNumbers(n1, n2); // function call

printf("sum = %d",sum);

return 0;
}

int addNumbers(int a,int b) // function definition


{
int result;
Passing Arguments
 Arguments are passed as in Java and Pascal
 Function call:
func1 (a, b, c);
 Function header
int func1 (int x, int y, int z)
– Each argument can be any valid C expression that has a value:
– For example:
x = func1(x+1,func1(2,3,4),5);
 Parameters x y z are initialized by the value of a b c
 Type conversions may occur if types do not match.
Parameters are Passed by Value
 All parameters are #include<stdio.h>
passed by value!! int twice(int x)
{
– This means they are
x=x+x;
basically local variables
return x;
initialized to the values
}
that the function is called
int main()
with.
{
– They can be modified as
int x=10,y;
you wish but these
y=twice(x);
modifications will not be
printf("%d,%d\n",x,y);
seen in the calling routine!
}
Returning a Value
 To return a value from a C function you must explicitly
return it with a return statement.
 Syntax:

return <expression>;

– The expression can be any valid C expression that resolves to


the type defined in the function header.
– Type conversion may occur if type does not match.
– Multiple return statements can be used within a single function
(eg: inside an “if-then-else” statement…)
Local Variables
 Local Variables

int func1 (int y)


{
int a, b = 10;
float rate;
double cost = 12.55;
.......
}

 Those variables declared “within” the function are considered “local


variables”.
 They can only be used inside the function they were declared in, and
not elsewhere.
A Simple Example
#include <stdio.h>
int x=1; /* global variable - bad! */
void demo(void);
void main() {
int y=2; /* local variable to main */
printf ("\nBefore calling demo(), x = %d and y = %d.",x,y);
demo();
printf ("\nAfter calling demo(), x = %d and y = %d.\n",x,y);
}
void demo () {
int x = 88, y =99; /* local variables to demo */
printf ("\nWithin demo(), x = %d and y = %d.",x,y);
}
 Function with no arguments and no return value

Types
Function with no arguments and a return value
 Function with arguments and no return value
 Function with arguments and a return value.
#include <stdio.h>

void checkPrimeNumber();

int main()
{
checkPrimeNumber(); // no argument is passed to prime()
return 0;
}

// return type of the function is void becuase no value is returned from the function
void checkPrimeNumber()
{
int n, i, flag=0;

printf("Enter a positive integer: ");


scanf("%d",&n);

for(i=2; i <= n/2; ++i)


{
if(n%i == 0)
{
flag = 1;
}
}
if (flag == 1)
#include <stdio.h>

Function with no arguments and a return value


int getInteger();

int main()
{
int n, i, flag = 0;

// no argument is passed to the function


// the value returned from the function is assigned to n
n = getInteger();

for(i=2; i<=n/2; ++i)


{
if(n%i==0){
flag = 1;
break;
}
}

if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);

return 0;
}

// getInteger() function returns integer entered by the user


int getInteger()
{
int n;

printf("Enter a positive integer: ");


Function with arguments and no return value
#include <stdio.h>
void checkPrimeAndDisplay(int n);

int main()
{
int n;

printf("Enter a positive integer: ");


scanf("%d",&n);

// n is passed to the function


checkPrimeAndDisplay(n);

return 0;
}

// void indicates that no value is returned from the function


void checkPrimeAndDisplay(int n)
{
int i, flag = 0;

for(i=2; i <= n/2; ++i)


{
if(n%i == 0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not a prime number.",n);
else
int main()
{ Function with arguments and a return value.
int n, flag;

printf("Enter a positive integer: ");


scanf("%d",&n);

// n is passed to the checkPrimeNumber() function


// the value returned from the function is assigned to flag variable
flag = checkPrimeNumber(n);

if(flag==1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);

return 0;
}

// integer is returned from the function


int checkPrimeNumber(int n)
{
/* Integer value is returned from function checkPrimeNumber() */
int i;

for(i=2; i <= n/2; ++i)


{
if(n%i == 0)
return 1;
}
Recursion
unsigned int factorial(unsigned int unsigned int factorial (unsigned
a); Recursion - An Example int a) {
if (a==1)
void main () {
return 1;
unsigned int f,x;
printf("Enter value between 1 & else {
8: "); a *= factorial(a-1);
scanf("%d", &x);
return a;
if (x > 8 || x < 1)
printf (”Illegal input!\n"); }
else { }
f = factorial(x);
printf ("%u factorial equals %u\
n", x,f);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy