0% found this document useful (0 votes)
8 views20 pages

Unit Iv

This document provides an overview of functions in programming, including their definition, declaration, types, and parameters. It explains how to create, call, and return values from functions, along with examples in C programming. Additionally, it highlights the importance of functions for code reusability and organization.

Uploaded by

ccpatil.24
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)
8 views20 pages

Unit Iv

This document provides an overview of functions in programming, including their definition, declaration, types, and parameters. It explains how to create, call, and return values from functions, along with examples in C programming. Additionally, it highlights the importance of functions for code reusability and organization.

Uploaded by

ccpatil.24
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/ 20

Unit IV

Function
Dr Prashant B Wakhare
Mob NO-7020880915
Mail Id: [email protected]
https://www.linkedin.com/in/pbwakhare/
Syllabus
• Function-Definition of a Function
• Declaration of a Function
• Function Prototypes
• Types of variables
• types of functions
• call by value.
Function-Needs
• Functions are used to perform certain actions,
and they are important for reusing code:
Define the code once, and use it many times.
• Functions break long programs up into smaller
components.
• Functions can be shared and used by other
programmers.
Predefined Functions
• For example, main() is a function, which is
used to execute code, and printf() is a
function; used to output/print text to the
screen
Create a own Function
To create your own function, specify the name
of the function, followed by parentheses () and
curly brackets {}:
Syntax:-
void myFunction()
{
// code to be executed
}
Call a Function
• To call a function, write the function's name
followed by two parentheses () and a
semicolon ;
• In the next example, myFunction() is used to
print a text (the action), when it is called:
Example
// Create a function
void myFunction()
{
printf(“AISSMS IOIT!");
}
int main() {
myFunction(); // call the function
return 0;
}
A function can be called multiple times
void myFunction()
{
printf(“AISSMS IOIT!\n”);
}
int main()
{
myFunction();
myFunction();
myFunction();
return 0;
}
C Function Parameters
Parameters and Arguments
• Information can be passed to functions as a
parameter. Parameters act as variables inside
the function.
• Parameters are specified after the function
name, inside the parentheses. You can add as
many parameters as you want, just separate
them with a commas.
Syntax
returnType functionName(parameter1, paramet
er2, parameter3)
{
// code to be executed
}
Example
void myFunction(char name[])
{
printf("Hello %s\n", name);
}
int main()
{
myFunction(“AISSMS");
myFunction(“IOIT");
myFunction(“COLLEGE");
return 0;
}
Multiple Parameters
void myFunction(char name[], int age)
{
printf("Hello %s. You are %d years old.\n", name, age);
}
int main()
{
myFunction(“RAM", 3);
myFunction(“RAJ", 14);
myFunction("Anju", 30);
return 0;
}
Pass Arrays as Function Parameters
void myFunction(int myNumbers[5])
{
for (int i = 0; i < 5; i++)
{
printf("%d\n", myNumbers[i]);
}
}
int main()
{
int myNumbers[5] = {10, 20, 30, 40, 50};
myFunction(myNumbers);
return 0;
}
Return Values
• The void keyword, used in the previous
examples, indicates that the function should
not return a value.
• If you want the function to return a value, you
can use a data type (such as int or float, etc.)
instead of void, and use the return keyword
inside the function
Examples
int myFunction(int x)
{
return 5 + x;
}

int main()
{
printf("Result is: %d", myFunction(3));
return 0;
}
Examples
int myFunction(int x, int y)
{
return x + y;
}

int main()
{
printf("Result is: %d", myFunction(5, 3));
return 0;
}
C Function Declaration and Definition
A function consist of two parts:
• Declaration: the function's name, return type,
and parameters (if any)
• Definition: the body of the function (code to
be executed)
void myFunction()
{ //declaration
// the body of the function (definition)
}
C Function Declaration and Definition
• You will often see C programs that have
function declaration above main(), and
function definition below main().
• This will make the code better organized and
easier to read:
Example
// Function declaration
void myFunction();
// The main method
int main()
{
myFunction(); // call the function
return 0;
}
// Function definition
void myFunction()
{
printf(“AISSMS IOIT!");
}
Example
// Function declaration
int myFunction(int, int);
// The main method
int main()
{
int result = myFunction(5, 3); // call the function
printf("Result is = %d", result);
return 0;
}
// Function definition
int myFunction(int x, int y)
{
return x + y;
}

You might also like