Unit No 05 New
Unit No 05 New
Defined function
Syllabus :
• User Defined Functions: Need for User-defined Functions, A Multi-Function
Program, Elements of User defined Functions, Definition of Functions, Return
Values and their Types, Function Calls, Function Declaration, Category of
Functions: No Arguments and no Return Values, Arguments but No Return
Values, Arguments with Return values, No Arguments but Returns a Value,
Functions that Return Multiple Values, Nesting of Functions, Recursion
• Structures : What is a Structure? Structure Type Declarations, Structure
Declarations, Referencing Structure Members, Referencing Whole Structures,
Initialization of Structures.
Function in c
• Function is basically a set of statements that takes inputs, perform
some computation and produce output.
• Syntax:
Library Function :
Need for User-defined Functions :
• 1. Code reusability .
• 2.Modularity.
• 3.Error detection.
• 4.Flexibility.
A Multi-Function Program
• Program Explanation: in the program given above we have declared and
defined 3 functions happyNewYear(), happyDiwali() and friendShipDay()
respectively.
after that in the main() function we have called the declared and defined function
happyNewYear(); ,happyDiwali(); ,friendShipDay() one by one respectively.
Upon successful execution and completion of the program the called function
gives the
Happy new Year! Happy Diwali! Happy friendship Day! Result
• Output:
Happy new Year!
Happy Diwali!
Happy friendship Day!
C program to demonstrate multiple function inside the main() function.
#include<stdio.h>
void happyDiwali()
{
printf("\n Happy Diwali!");
}
void friendShipDay()
{
printf("\n Happy friendship Day!");
}
void happyNewYear()
{
printf("\n Happy new Year!");
}
int main()
{
// calling function
happyNewYear();
happyDiwali();
friendShipDay();
return(0);
}
Definition of Functions
Syntax :
Function Calls
Function Calls :
Function Declaration
Return values and their types
Return values and their types
• Here are the three primary forms of the return statement in C programming:
• 1. Return with an Expression:
• return expression;
• In this form, the expression represents the value that the C function returns to the calling code. This value
could be a constant, a variable, or the result of a computation.
• 2. Return with a Value:
• return;
• When a function has no return value or is used to perform a task without providing any output, the return
statement can be used without an expression. This form simply indicates the end of the function's execution.
Return values and their types
• 3. No Return Statement:
• void myFunction() {
• // Function logic without a return statement
•}
• If a function does not require a return statement, it can be defined
with a return type of void, indicating that the function does not return
any value
Category of Functions:
•e
Sample program :
2) No Arguments but Returns a Value.
Sample program :
3) Arguments but No Return Values
Sample program :
4) Arguments with Return values.
Sample program :
Functions that Return Multiple
Values
Nesting of Functions
• Nested function is not supported by C because we
cannot define a function within another function in C.
We can declare a function inside a function, but it’s not
a nested function.
Because nested functions definitions can not
access local variables of the surrounding blocks, they
can access only global variables of the containing
module. This is done so that lookup of global variables
doesn’t have to go through the directory. As in C, there
are two nested scopes: local and global (and beyond
this, built-ins). Therefore, nested functions have only a
limited use. If we try to approach nested function in C,
then we will get compile time error.
// C program to illustrate the
// concept of Nested function.
Output:
#include <stdio.h> Compile time error: undefined reference to `view'
int main(void) An extension of the GNU C Compiler allows the declarations of nested functions.
{ The declarations of nested functions under GCC’s extension need to be
prefix/start with the auto keyword.
printf("Main");
int fun()
{
printf("fun");
• syntax:
• struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
C Structure Definition
To use structure in our program, we have to define its instance. We
can do that by creating variables of the structure type.
We can define structure variables using two methods:
1. Structure Variable Declaration with Structure Template
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;