← Back to R Course | Chapter 5: Functions | Lesson 1 of 7

Function के Basics

एक function एक named recipe है जो आप एक बार लिखते हैं और बार-बार इस्तेमाल करते हैं। आप इसे एक नाम देते हैं, steps list करते हैं, और जब ज़रूरत हो इसे call करते हैं।

In this page:

  1. Function Basics
Syntax
r
function_name <- function(arg1, arg2) {
  # body
  result
}

function_name(value1, value2)

Function Basics

R में एक function function(args) { body } से बनता है और किसी भी दूसरी value की तरह एक नाम को assign होता है, क्योंकि R में functions first-class objects हैं।

इसे call करना किसी भी built-in function को call करने जैसा दिखता है। Logic को functions में तोड़ना R code organize और reuse करने का primary तरीका है।

Note: R में Functions ordinary values हैं -- आप इन्हें दूसरे functions को arguments की तरह pass कर सकते हैं, numbers या strings की तरह।

उदाहरण: Function Basics

markup
greet <- function() {
  cat("Hello from a function!\n")
}

greet()
greet()
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. call करते वक्त parentheses भूल जाना, जैसे greet, जो इसे चलाने की बजाय function का code print करता है।
  2. दूसरी languages की तरह function greet() { ... } लिखना, जब R को greet <- function() { ... } चाहिए।
  3. एक script में उसे call करने वाले code के बाद एक function define करना, इसलिए call could not find function के साथ fail होती है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.