0% found this document useful (0 votes)
85 views26 pages

8 User Defined Functions

The document outlines the course structure for 'Logical Thinking & Problem Solving' in the Bachelor of Engineering program, focusing on C programming. It includes course objectives, outcomes, evaluation schemes, and detailed explanations of user-defined functions and parameter passing techniques. Additionally, it provides examples and frequently asked questions to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views26 pages

8 User Defined Functions

The document outlines the course structure for 'Logical Thinking & Problem Solving' in the Bachelor of Engineering program, focusing on C programming. It includes course objectives, outcomes, evaluation schemes, and detailed explanations of user-defined functions and parameter passing techniques. Additionally, it provides examples and frequently asked questions to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Academic Session 2025-26

ODD Semester Jul-Dec 2025

INSTITUTE - UIE
Department of Engineering Foundations
Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Logical Thinking & Problem Solving
Code:25CSH-107

Unit No. 2 Chapter No. 3 Lecture No.2

Topic : Loops

Faculty Name-Dr. Sheenam

E_Code - E6717

Designation-Assistant Professor
Logical Thinking
& Problem
Solving
Course Objectives
The course aims to provide a strong foundation
in problem-solving using the C programming
language.
It enhances students' programming skills by
developing logical thinking and algorithmic
problem-solving abilities
By mastering C programming concepts, students
will be able to design and implement efficient
solutions for real-world problems 2
Course Outcomes

CO No Statement

To remember the concepts related to fundamentals of C language, Algorithm/


CO1
pseudocode, Flowcharts and Problem solving

CO2 To understand the way of execution and debug programs in C language.

To apply various constructs, loops, functions to solve mathematical and scientific


CO3
problem.

CO4 To analyse the dynamic behaviour of memory by the use of pointers.

To design and develop modular programs for real world problems using control
CO5
structure and selection structure.
Scheme of Evaluation
Frequency of Final Weightage
Direct Evaluation Weightage of actual
S. No. Assessment tool in Internal
Instruments conduct Task Assessment
Unit-1 Practical 15 Marks Code-Blocks & Dev C
1 Evaluation
15 Marks Hacker Rank
2 Unit-2 Practical
Evaluation
3
Unit-3 Practical 15 Marks Code Fix problems
Evaluation / Mini
3 Project 45

Lab MST 15 Marks for one Online GDB compiler 1 per semester
4 MST 15

External Exam 40 Marks Hacker Rank 1 per semester


5
40
6 Attendance NA NA NA NA
4
• User Defined Functions and its
types
• Parameter Types, Parameter
Passing
CONTENTS • Calling a Function
• Examples

5
USER DEFINED FUNCTION
DEFINITION: A large C program is divided into basic building blocks called C
function. C function contains set of instructions enclosed by “{ }” which performs
specific operation in a C program. The main components of a function includes
return type, function name and argument list.

6
USER DEFINED FUNCTION
COMPONENTS
• Return-type: This specifies the data type of the value being returned by the function. A
function may or may not return a value. If the function does not return a value, then the
return type is void. In this case, the return value is an integer value c, which means the return
type would be int.

• Parameter list: The list of formal parameters being passed onto the function. In this case,
there are two parameters of type int passed to the function.

• Local variables or local declarations: The variables that are declared inside the function are
called local variables. The scope of these variables lies within the function and they are not
accessible outside the function.

• Function body: Comprised of everything inside the curly brackets { and } following the return
type, function name and the parameter list.
7
USER DEFINED FUNCTION
COMPONENTS
• Function name: The name of the function can be anything that you want. The
standard is to make it something related to what it's supposed to do. The naming
convention follows the same rule as that of variable naming convention in C.

• Function declaration: Tells the compiler all about the function. These include the
function's name, the return type, and the number and types of parameters. The
body of the function having the function definition can be defined somewhere
else.

8
FUNCTION DECLARATION
A function declaration has the following parts:
return_type function_name( parameter list );
The example here can be declared as follows:
int sum(int num1, int num2);
OR
return_type function_name(datatypes)
int sum(int,int);

9
TYPES OF USER DEFINED FUNCTION
There can be 4 different types of user-defined functions, they are:

1. Function with no arguments and no return value.


2. Function with no arguments and a return value
3. Function with arguments and no return value
4. Function with arguments and a return value

10
Function with no arguments and no
return value
Such functions can either be used to display information or they are completely dependent on user inputs.
Example: It takes 2 numbers as input from user, and display which is the greater number.
#include<stdio.h>
void greatNum(); // function declarationint
main()
{
greatNum(); // function call
return 0;
}
void greatNum() // function definition
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
if(i > j)
{
printf("The greater number is: %d", i);
}
else
{
printf("The greater number is: %d", j);
}
}
11
Function with no arguments and a
return value
We have modified the above example to make the function greatNum() return the number which is greater amongst the 2 input numbers.
#include<stdio.h>
int greatNum(); // function declaration
int main()
{
int result;
result = greatNum(); // function call
printf("The greater number is: %d", result);
return 0;}int greatNum() // function definition
{
int i, j, greaterNum;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j); if(i > j)
{
greaterNum = i;
}
else
{
greaterNum = j;
} // returning the result
return greaterNum;
}

12
Function with arguments and no
return value
We are using the same function as example again and again, to demonstrate that to solve a problem there can be many different ways.
This time, we have modified the above example to make the function greatNum() take two int values as arguments, but it will not be returning
anything.
#include<stdio.h>
void greatNum(int a, int b); // function declarationint main()
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
greatNum(i, j); // function call
return 0;
}
void greatNum(int x, int y) // function definition
{
if(x > y)
{
printf("The greater number is: %d", x);
}
else
{
printf("The greater number is: %d", y);
}
13
Function with arguments and a
return value
It makes the function completely independent of inputs and outputs, and only the logic is defined inside the function body.
#include<stdio.h>
int greatNum(int a, int b); // function declaration
int main()
{
int i, j, result;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
result = greatNum(i, j); // function call
printf("The greater number is: %d", result);
return 0;
}
int greatNum(int x, int y) // function definition
{
if(x > y)
{
return x;
}
else {
return y; }
}
14
Passing Parameters
Parameter Passing Techniques in C: There are different ways in which
parameter data can be passed into and out of methods and functions.
Formal Parameter : A variable and its type as they appear in the
prototype of the function or method.
Actual Parameter : The variable or expression corresponding to a
formal parameter that appears in the function or method call in the
calling environment.
Examples of Parameter Passing
EXAMPLE: PROGRAM TO FIND SQUARE OF A NUMBER
#include<stdio.h>
float square ( float x ); //function declaration
int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
n = square ( m ) ; // function call
printf ( "\nSquare of the given number %f is %f”,m,n );
}
float square ( float x ) // function definition
{
float p ;
p=x*x;
return ( p ) ;
} 16
How to Call C Functions in a
Program
There are two ways that a C function can be called from a program. Functions can be invoked in
two ways:
• Call by Value
• Call by Reference.

These two ways are generally differentiated by the type of values passed to them as parameters.

Call By Value: In this parameter passing method, values of actual parameters are copied to
function’s formal parameters and the two types of parameters are stored in different memory
locations. So any changes made inside functions are not reflected in actual parameters of
caller.

Call by Reference: Both the actual and formal parameters refer to same locations, so any
changes made inside the function are actually reflected in actual parameters of caller.
17
CALL BY VALUE EXAMPLE
Example: Swap two numbers using
Call by Value
#include <stdio.h>
void swap(int x, int y);
int main()
{
int a = 2, b = 3;
swap(a, b);
printf("a=%d b=%d\n", a, b);
return 0;
}
void swap(int x, int y)
{
int t;

t = x;
x = y;
y = t;

printf("x=%d y=%d\n", x, y);


}

18
CALL BY REFERENCE EXAMPLE
Example: Swap two numbers using Call by Reference
#include <stdio.h>
void swap(int*, int*);
int main()
{
int a =2, b = 3;
swap(&a, &b);
printf("a=%d b=%d\n", a, b);
return 0;
}
void swap(int* c, int* d)
{
int t;
t = *x;
*x = *y;
*y = t;
printf("x=%d y=%d\n", *x, *y);
}
19
SUMMARY
We have learned the following topics:-
User- defined Functions
Parameter Passing
Call by Value and Call by Reference

20
FREQUENTLY ASKED QUESTIONS
PROGRAMS
1. Write a program in C to check a given number is even or odd using the function.
2. Write a program in C to check whether a number is a prime number or not using the function.
3. Write a program in C to check armstrong numbers using the function.
4. Write a program in C to find the sum of the series 1!/1+2!/2+3!/3+4!/4+5!/5 using the function.
5. Write a program to display fibonacci series upto user required number using function.

21
UTILIZE YOUR KNOWLEDGE TO
ANSWER
1. Choose correct statement about Functions in C Language.
A) A Function is a group of c statements which can be reused any number of times.
B) Every Function has a return type.
C) Every Function may no may not return a value.
D) All the above.

2 What is the output of C Program with functions.? void show(); int main() { show(); printf("ARGENTINA "); return
0; } void show() { printf("AFRICA "); }
A) ARGENTINA AFRICA
B) AFRICA ARGENTINA
C) ARGENTINA
D) Compiler error

3) How many values can a C Function return at a time.?


A) Only One Value
B) Maximum of two values
C) Maximum of three values
D) Maximum of 8 values
22
UTILIZE YOUR KNOWLEDGE TO
ANSWER(CONTD.)
4 What are types of Functions in C Language.?
A) Library Functions
B) User Defined Functions
C) Both Library and User Defined
D) None of the above

5. Which of the following statement is true about a function with an argument?


A. No value is pass to the function during function call.
B. function with an argument must not have return type
C. function with an argument is declared and define with parameter list
D. none of the above

23
REFERENCES
Video Lectures:
1.https://nptel.ac.in/courses/106/106/106106127/
2.https://www.youtube.com/watch?v=4-xX9vmPDsc
3.https://www.youtube.com/watch?v=Dt9q3qiaqiA

Websites:
1.https://www.tutorialspoint.com/cprogramming/c_functions.htm
2.https://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functions.html
3.https://beginnersbook.com/2014/01/c-function-call-by-value-example/

24
Class-wise feedback
THANK YOU

You might also like